java - 插入 Java 类方法(无接口(interface))

标签 java reflection interposing

我想在类方法之间插入以动态扩展对象。

我已经知道 java.lang.reflect.Proxy 的东西,但是它太有限了,无法进行真正的插入。

来自 Using java.lang.reflect.Proxy to Interpose on Java Class Methods ,第一个限制是:

(...) the method must be called through an instance of the proxy class. So nested methods calls, for instance, would not be intercepted.

最糟糕的是:

(...) the method must have been defined in an Interface that is implemented by the object being proxied. It can not be called through an instance of a class that does not implement an interface.

我想在运行时扩展的对象没有实现任何接口(interface),最糟糕的是,我需要覆盖的方法是嵌套的和私有(private)的。

我知道这在 Python 和 C 中相当容易,上面引用的文章说这是可能的:

The next article in this series will illustrate some techniques for overcoming these limitations.

很遗憾,我找不到这篇文章。

最佳答案

尝试使用 CGLIB:

这是我几年前写的一个教程,它非常简单地介绍了 CGLIB:CGLIB intro

如果您需要更多电量,请考虑使用 AspectJ

希望对你有帮助

关于java - 插入 Java 类方法(无接口(interface)),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9061478/

相关文章:

reflection - 使用反射调用 Scala 构造函数的参数数量错误

java - 如何通过反射从xml文件读取字段名称和类型来创建java对象?

c - 在 OSX 上干预,我的函数没有被调用

java - 如何将 Angular 6 和 Spring MVC 与单个服务器集成?

java - 使用 WebLogic 获取当前 EAR 路径

java - 如何不给Guava的LoadingCache添加空值?

Java : DataInputStream replacement for endianness

java - 反射:创建实例-java

c - 是否可以覆盖目标模块(gcc、ld、x86、objcopy)中的静态函数?