我正在开发一个 Java 库,它使用 cglib 创建库用户提供的抽象类的子类,并提供用户留下的抽象方法的自动生成的实现。
我的问题是,如果所讨论的方法具有包本地(即默认)可访问性,那么我生成的方法显然会被忽略,并且用户在调用它时会收到 AbstractMethodError 。
我生成的类与原始类位于同一包中(我生成一个名称为original.package.OriginalClassName_AutomaticImplementation的类),尽管它们当然是由不同的类加载器加载的(即加载由cglib生成的字节数组而不是而非磁盘文件);我怀疑这就是问题所在。如果是这样,有什么办法可以解决吗?
I have a Java library I'm working on that uses cglib to create subclasses of abstract classes provided by the library user, and provides automatically-generated implementations of abstract methods left in there by the user.
My problem is that if the method in question has package-local (i.e. default) accessibility, the method I produce is apparently ignored, and the user gets an AbstractMethodError when it is called.
The classes I generate are in the same package as the original class (I generate a class whose name is original.package.OriginalClassName_AutomaticImplementation), although they are of course loaded by a different classloader (i.e. one that loads the byte array generated by cglib rather than a disk file); my suspicion is that this is the problem. If so, is there any way around it?
发布评论
评论(1)
使用本地包时,类加载器和包名称定义了该方法是否可访问。这是为了阻止类未经授权访问 API 方法。您可以在 java.lang 包中创建一个类,并访问 java.lang 中的包本地方法。
您可以尝试调整类加载器,加载要扩展的类,然后使用该类加载器作为 cglib 加载器的父级加载 cglib 版本。不知道是否会起作用。
When working with package local it is the class loader and the package name that define whether the method is accessible or not. This is to stop classes getting unauthorised access to API methods. You could crate a class in the java.lang package and access the package local methods in java.lang.
You could try adjusting the class loader you load the class to be extended and then load the cglib version with that classloader as the cglib loader's parent. Don't know if it will work.