java.lang.IllegalAccessError: tried to access field ConcreteEntity.instance from class Entity
好的,所以这里是交易.我正在尝试访问ConcreteEntity.instance,这是一个默认的ClassLoader中存在的访问类型默认的字段,而Entity.getInstance是存在于一个小孩ClassLoader中的一种方法.
请记住,它们都在同一个包中,但是会抛出一个IllegalAccessError.有没有解决这个问题,不涉及我实际将Entity类加载到与ConcreteEntity相同的ClassLoader中?
0 new #14 <Entity> 3 dup 4 aload_0 5 invokevirtual #18 <Adapter.getInstance> 8 checkcast #20 <sl> 11 getfield #24 <sl.d> 14 invokespecial #25 <Entity.<init>> 17 areturn
通过jclasslib检索的字节码是异常生成“编译后”.
谢谢Gamb清理帖子.
解决方法
jvm认为从不同类加载器加载的类可以在不同的“运行时包”中,即使它们具有相同的包名称.从the jvm spceification起,第5.3节:
At run time,a class or interface is determined not by its name alone,but by a pair: its fully qualified name and its defining class loader. Each such class or interface belongs to a single runtime package. The runtime package of a class or interface is determined by the package name and defining class loader of the class or interface.
在第5.4.4节中:
A field or method R is accessible to a class or interface D if and only if any of the following conditions is true:
…
R is either protected or package private (that is,neither public nor protected nor private),and is declared by a class in the same runtime package as D.