使用 cglib 覆盖包本地方法

发布于 2024-09-29 21:14:23 字数 324 浏览 1 评论 0原文

我正在开发一个 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

失而复得 2024-10-06 21:14:23

使用本地包时,类加载器和包名称定义了该方法是否可访问。这是为了阻止类未经授权访问 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文