Interposing on Java Class Methods (without interfaces)

737 views Asked by At

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

There are 1 answers

1
Mark Bramnik On BEST ANSWER

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