什么是GeneratedMethodAccessor1,2等,为什么不能找到它们?

时间:2009-06-21 16:04:39

标签: java reflection bytecode-manipulation

我正在获得这样的堆栈跟踪:

java.lang.NoClassDefFoundError: sun/reflect/GeneratedMethodAccessor1
    at sun.reflect.GeneratedMethodAccessor1.<clinit>(Unknown Source)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
    at java.lang.Class.newInstance0(Class.java:355)
    at java.lang.Class.newInstance(Class.java:308)
    at sun.reflect.MethodAccessorGenerator$1.run(MethodAccessorGenerator.java:381)
    at java.security.AccessController.doPrivileged(Native Method)
    at sun.reflect.MethodAccessorGenerator.generate(MethodAccessorGenerator.java:377)
    at sun.reflect.MethodAccessorGenerator.generateMethod(MethodAccessorGenerator.java:59)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:28)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at edu.tufts.cs.testsim.LogicalProcess.dispatchMessage(LogicalProcess.java:214)
    at edu.tufts.cs.testsim.LogicalProcess.processForward(LogicalProcess.java:287)
    at edu.tufts.cs.testsim.LogicalProcess.doOperation(LogicalProcess.java:423)
    at edu.tufts.cs.testsim.LogicalProcess.run(LogicalProcess.java:434)
    at java.lang.Thread.run(Thread.java:637)
Caused by: java.lang.ClassNotFoundException: sun.reflect.GeneratedMethodAccessor1
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:316)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:288)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:374)
    ... 19 more

什么是GeneratedMethodAccessor1,GeneratedMethodAccessor2,GeneratedMethodAccessorN以及可能导致它们无法找到的原因?我在运行时进行一些字节码重写,但只在加载类之前,前几次通过反射调用工作正常。我想知道在JIT编译器获取代码之后是否会发生这种情况,但我甚至不知道如何开始调试它。

1 个答案:

答案 0 :(得分:9)

GeneratedMethodAccessor###是运行时由反射实现生成的类,用于调用方法和构造函数。这形成了从MethodConstructor的实例到实际方法或构造函数的字节码桥。源代码中提供了更多信息。

反序列化也做了类似的事情,共享一些相同的机制,以调用派生最多的非Serializable构造函数。