java.lang.IllegalAccessError:尝试从类Entity访问字段ConcreteEntity.instance [英] java.lang.IllegalAccessError: tried to access field ConcreteEntity.instance from class Entity
问题描述
java.lang.IllegalAccessError:尝试从类实体访问字段ConcreteEntity.instance
java.lang.IllegalAccessError: tried to access field ConcreteEntity.instance from class Entity
Ok所以这里是交易。我正在尝试访问 ConcreteEntity.instance
这是一个具有默认访问类型的字段,该字段存在于默认的 ClassLoader
中,并且 Entity.getInstance
是一个存在于子 ClassLoader
中的方法。
Ok so here is the deal. I am trying to access ConcreteEntity.instance
which is a field with the access type default that exists inside the default ClassLoader
and the Entity.getInstance
is a method that exist in a child ClassLoader
.
现在请记住,它们都在同一个包中,但是抛出了 IllegalAccessError
。是否有一个解决这个问题的方法,不涉及我实际在同一个 ClassLoader
中加载Entity类为 ConcreteEntity
?
Now keep in mind they're both in the same package, however an IllegalAccessError
is being thrown. Is there a solution to this problem that doesn't involve me actually loading the Entity class inside the same ClassLoader
as ConcreteEntity
?
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 at检索的字节码是生成的异常编译后。
The bytecode retrieved via jclasslib at were the exception is generated "After being compiled".
感谢Gamb清理帖子。
推荐答案
请参阅我的回答 一个类似的问题,除了你的情况很明显你正在处理多个类加载器:
See my answer to a similar question, except in your case it is clear that you are dealing with multiple classloaders:
jvm认为从不同的类加载器加载的类在不同的运行时包中,即使它们具有相同的包名。引自 jvm spceification ,部分5.3:
The jvm considers classes loaded from different classloaders to be in different "runtime packages", even if they have the same package name. Quoting from the jvm spceification, section 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:
当且仅当以下任何条件为真时,类或接口D才能访问字段或方法R. :
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受保护或包私有(即既不公开也不保护也不私有),并由与
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.
这篇关于java.lang.IllegalAccessError:尝试从类Entity访问字段ConcreteEntity.instance的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!