Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Create Dynamic Proxy for class instead of interface (http://www.velocityreviews.com/forums/t749827-create-dynamic-proxy-for-class-instead-of-interface.html)

Stanimir Stamenkov 06-12-2011 02:15 PM

Create Dynamic Proxy for class instead of interface
 
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/...ion/proxy.html

--
Stanimir

Stanimir Stamenkov 06-12-2011 02:40 PM

Re: Create Dynamic Proxy for class instead of interface
 
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/~chib...xyFactory.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/~chib...e-summary.html

--
Stanimir

Robert Klemme 06-12-2011 05:07 PM

Re: Create Dynamic Proxy for class instead of interface
 
On 06/12/2011 04:40 PM, Stanimir Stamenkov wrote:
> 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/~chib...xyFactory.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.

>> Are there other similar tools?


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

Stanimir Stamenkov 06-12-2011 05:42 PM

Re: Create Dynamic Proxy for class instead of interface
 
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/ar...es-401775.html

--
Stanimir

Tom Anderson 06-13-2011 10:58 AM

Re: Create Dynamic Proxy for class instead of interface
 
On Sun, 12 Jun 2011, Stanimir Stamenkov wrote:

> 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_By...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/...e-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

--
FREQUENT VIOLENT BLOODY


All times are GMT. The time now is 04:57 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.