通过反射创建包含匿名内部类的类的对象

时间:2011-11-23 19:50:36

标签: java reflection inner-classes actionlistener

我正在尝试创建一个包含匿名内部类 ActionListener 的类测试的对象 - 通过反射。

该类已正确加载。我可以列出任何字段,方法,构造函数等。问题是,当我尝试使用包含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();
    }
}

2 个答案:

答案 0 :(得分:2)

让我们再来一次......

所以这是IllegalAccessError,不要与IllegalAccessException混淆(在Java SE 7中扩展ReflectiveOperationException)。这是关于弄乱课程,而不是反思。

看起来好像你已经使用“自定义”类加载器加载了其中一个类,而另一个类通过普通的类加载器加载。即使包名匹配,从不同类加载器加载的类也在不同的包中(从早期更新的J2SE 1.2,IIRC)。一个类试图访问(链接)另一个依赖于“包私有”访问,但JVM否认它,因为包实际上是不同的。

答案 1 :(得分:0)

您写道:

    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();
    }
}

此代码无法可靠地运行;仔细阅读FileInputStream.available和FileInputStream.read的JavaDoc应该揭示原因。

可能是残缺的类文件数据导致该异常。