Interposing on Java Class Methods (without interfaces)

情到浓时终转凉″ 提交于 2020-01-24 19:56:08

问题


I'd like to interpose between class methods to dynamically extends an object.

I already know about the java.lang.reflect.Proxy stuff, but it's way too limited to do real interposing.

From Using java.lang.reflect.Proxy to Interpose on Java Class Methods, the first limitation is :

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

And the worst one :

(...) 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.

The object I'd like to extends at run-time doesn't implement any interface, and worst, the methods I need to override are nested and private.

I know this is fairly easy in Python & C, and the article cited above says that it could be possible :

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

Unfortunately, I'm unable to find this article.


回答1:


Try using CGLIB:

Here is a tutorial I wrote a couple of years ago, its quite simple introduction to CGLIB: CGLIB intro

If you need even more power, consider to use AspectJ

Hope this helps



来源:https://stackoverflow.com/questions/9061478/interposing-on-java-class-methods-without-interfaces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!