尝试从类Entity访问字段ConcreteEntity

尝试从类Entity访问字段ConcreteEntity

本文介绍了java.lang.IllegalAccessError:尝试从类Entity访问字段ConcreteEntity.instance的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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认为从不同的类加载器加载的类在不同的运行时包中,即使它们具有相同的包名。引自,部分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:

在部分中5.4.4:

...

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 19:16