开发者

Using cglib to override package-local methods

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-loca开发者_运维技巧l (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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜