使用反射从 ClassLoader 获取 defineClass 时出现 NoSuchMethodException

NoSuchMethodException when using reflection to get defineClass from ClassLoader

我正在尝试使用反射从 ClassLoader

获取 defineClass 方法
ClassLoader cl = this.getClass().getClassLoader();
Method m = cl.getClass().getMethod("defineClass", new Class[] { String.class, Array.class, int.class, int.class });

但这失败了 NoSuchMethodException: sun.misc.Launcher$AppClassLoader.defineClass(java.lang.String, java.lang.reflect.Array, int, int)

它不喜欢 Array.class 似乎是合理的,但如果是这样,我不知道那里需要什么。

ClassLoader.defineClassbyte 数组作为其第二个参数,而不是 Array 类型的对象。这就是你需要的:

Method m = ClassLoader.class.getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class);