我正在尝试通过反射创建包含匿名内部类 ActionListener 的类 Test 的对象。
类已正确加载。我可以列出任何字段、方法、构造函数等。问题是,当我尝试使用包含某些按钮的 ActionListeners 的构造函数创建对象时。当听众被注释掉时,一切正常。
当我使用 Constructor.newInstance() 方法时,我得到:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at Engine.createObj(Engine.java:78)
at Main$3.actionPerformed(Main.java:107)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Caused by: java.lang.IllegalAccessError: tried to access class Test.Test$2 from class Test.Test
at Test.Test.<init>(Test.java:123)
... 42 more
Test.java:123 当然是实现监听器的地方。 Test.Test$2 是匿名内部类文件。
加载看起来工作正常的类代码:
public void loadClass(File file) {
reflectClass = null;
try {
FileInputStream fis = null;
fis = new FileInputStream(file);
byte[] bytes = new byte[fis.available()];
int read = fis.read(bytes);
fis.close();
if (read != bytes.length) {
return;
}
reflectClass = defineClass(null, bytes, 0, bytes.length);
resolveClass(reflectClass);
} catch (Exception e1) {
e1.printStackTrace();
}
}
和对象创建:
public void createObj(Constructor ct) {
Class[] types = ct.getParameterTypes();
//... some params generating code, not important, i'm using non-param constructor.
try {
object = ct.newInstance(oParams); // Here is the problem.
} catch (Exception e) {
e.printStackTrace();
}
}
最佳答案
我们再来一次...
所以这是一个IllegalAccessError
,不要与IllegalAccessException
(扩展Java SE 7 中的ReflectiveOperationException
)混淆。这是关于搞乱类(class),而不是反射(reflection)。
看起来好像您使用“自定义”类加载器加载了其中一个类,而另一个通过普通类加载器加载了。即使包名称匹配,从不同类加载器加载的类也会在不同的包中(来自 J2SE 1.2 的早期更新,IIRC)。一个类试图访问(链接到)另一个依赖于“包私有(private)”访问的类,但 JVM 拒绝它,因为包实际上是不同的。
关于java - 通过反射创建包含匿名内部类的类的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8248233/