Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我需要在运行时扩展类,特别是检测对类状态所做的更改。如果可能的话,我希望能够通过拦截字段分配来检测更改,如果不拦截 setter 就可以了。我应该使用哪个库,为什么?javassist 或 cglib 还是?java.lang.reflect.Proxy 这可能吗?你能比较这些库吗?谢谢。
要拦截接口中定义的设置器,您可以使用代理。
要拦截方法调用,您可以使用 AOP,如 AspectJ。
但是,为了捕获字段分配,我会使用 ASM、BCEL、cglib 或 javassist 之类的字节码库。我更喜欢 ASM,它速度很快,并且有一些工具可以让编写字节码更容易。