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



我试图创建一个类的对象Test包含匿名内部类- ActionListener -通过反射。

类被正确加载。我可以列出任何字段,方法,构造器等。问题是,当我试图创建对象与构造函数包含actionlistener的一些按钮。当侦听器被注释掉时,一切正常。

当我使用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$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)。这是关于搞乱类,而不是反射。

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

您写的:

    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的JavaDoc。可用和FileInputStream。阅读应该揭示原因。

可能是残缺的类文件数据导致了这个异常

相关内容

  • 没有找到相关文章

最新更新