Create Dynamic Proxy for class instead of interface

S

Stanimir Stamenkov

The Dynamic Proxy API [1] allows one to create proxy classes
implementing number of specified interfaces. I've wondered if it is
possible and how to create such a proxy class for a specified class
and not interface. I've found Javassit should provide such means,
but haven't really tried it yet:

http://en.wikipedia.org/wiki/Javassist

I basically want to create a proxy augmenting an existing object
with additional interface. Is the Javassist library the right tool
for doing this? Are there other similar tools?

[1]
http://download.oracle.com/javase/6/docs/technotes/guides/reflection/proxy.html
 
S

Stanimir Stamenkov

Sun, 12 Jun 2011 17:15:17 +0300, /Stanimir Stamenkov/:
http://en.wikipedia.org/wiki/Javassist

I basically want to create a proxy augmenting an existing object
with additional interface. Is the Javassist library the right tool
for doing this?

I've found javassist.util.proxy.ProxyFactory does exactly what I want:

http://www.csg.is.titech.ac.jp/~chiba/javassist/html/javassist/util/proxy/ProxyFactory.html
Are there other similar tools?

The javassist.util.proxy package documentation [1] mentions cglib:

http://cglib.sourceforge.net/

Does anyone have experience with Javassis and/or cglib - is the one
or the other better somehow?

[1]
http://www.csg.is.titech.ac.jp/~chiba/javassist/html/javassist/util/proxy/package-summary.html
 
R

Robert Klemme

Sun, 12 Jun 2011 17:15:17 +0300, /Stanimir Stamenkov/:


I've found javassist.util.proxy.ProxyFactory does exactly what I want:

http://www.csg.is.titech.ac.jp/~chiba/javassist/html/javassist/util/proxy/ProxyFactory.html

I do not think you can change the behavior of an existing _object_ -
even for changing behavior of an existing _class_ you would have to
resort to manipulating a class's bytecode.

If you read closely what the Javadoc of ProxyFactory say, you will
notice that you get a _new_ class which is a _subclass_ of the class
that you want to augment. For that you do not need proxy mechanisms.
In fact it is much simpler to inherit the class and make the new class
implement additional interfaces.

The hard bit though (and that's where also the proxy approach fails) is
to manipulate the code which creates instances to no longer create
instances of the given class but instances of the new class (be it via
proxy or plain inheritance). Again, proxy does not help you here - you
will need to find all places with Class.forName("the.original.Class")
and replace them with Class.forName("the.new.Class"). If your given
class is created via some kind of factory mechanism (or the name is
configurable somewhere) it will be much simpler to do the exchange.

Why do you need to manipulate an existing class?

Kind regards

robert
 
S

Stanimir Stamenkov

Sun, 12 Jun 2011 19:07:41 +0200, /Robert Klemme/:
If you read closely what the Javadoc of ProxyFactory say, you will
notice that you get a _new_ class which is a _subclass_ of the class
that you want to augment. For that you do not need proxy mechanisms.
In fact it is much simpler to inherit the class and make the new
class implement additional interfaces.
[...]
Why do you need to manipulate an existing class?

Though I don't know how useful it might be, I've played a bit at
implementing the try-with-resources [1] (the easier part) and
exception masking Java 7 features using Java 6 facilities. For the
later I needed to create proxy exceptions augmented with interface
allowing to attach 'suppressed' exceptions information.

[1]
http://www.oracle.com/technetwork/articles/java/trywithresources-401775.html
 
T

Tom Anderson

The Dynamic Proxy API [1] allows one to create proxy classes
implementing number of specified interfaces. I've wondered if it is
possible and how to create such a proxy class for a specified class and
not interface. I've found Javassit should provide such means, but
haven't really tried it yet:

http://en.wikipedia.org/wiki/Javassist

I basically want to create a proxy augmenting an existing object with
additional interface. Is the Javassist library the right tool for doing
this? Are there other similar tools?

A number - i wrote a little list at one point:

http://urchin.earth.li/~twic/Java_Bytecode_Libraries.html

Having said that, while all of these can be used to do the bytecode
editing you would need to construct such a proxy, i don't know if all of
them include ready-made tools to do it. Probably not.

Something else you probably want to know about is the instrumentation API:

http://download.oracle.com/javase/6/docs/api/java/lang/instrument/package-summary.html

Essentially, you can use this to get all classes loaded by the JVM to go
through some of your code to be modified before loading. That lets you
actually rewrite existing classes as they load - no need for proxies, you
can change the actual classes. This is commonly used by things like JPA
and AOP implementations.

tom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top