java.lang.IllegalAccessError:尝试从类Entity访问字段ConcreteEntity.instance

前端之家收集整理的这篇文章主要介绍了java.lang.IllegalAccessError:尝试从类Entity访问字段ConcreteEntity.instance前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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清理帖子.

解决方法

参见 my answera similar question,除了你的情况,很明显你正在处理多个类加载器:

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.

原文链接:https://www.f2er.com/java/122191.html

猜你在找的Java相关文章