Create Dynamic Proxy for class instead of interface

Discussion in 'Java' started by Stanimir Stamenkov, Jun 12, 2011.

  1. 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

    --
    Stanimir
     
    Stanimir Stamenkov, Jun 12, 2011
    #1
    1. Advertising

  2. 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

    --
    Stanimir
     
    Stanimir Stamenkov, Jun 12, 2011
    #2
    1. Advertising

  3. 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/~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.

    >> 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
     
    Robert Klemme, Jun 12, 2011
    #3
  4. 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

    --
    Stanimir
     
    Stanimir Stamenkov, Jun 12, 2011
    #4
  5. Stanimir Stamenkov

    Tom Anderson Guest

    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_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

    --
    FREQUENT VIOLENT BLOODY
     
    Tom Anderson, Jun 13, 2011
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.microsoft.com
    Replies:
    0
    Views:
    457
    news.microsoft.com
    Apr 12, 2006
  2. Francisco Garcia
    Replies:
    2
    Views:
    495
    Fran Garcia
    Apr 13, 2006
  3. Matt
    Replies:
    10
    Views:
    634
    Chris Smith
    Apr 7, 2004
  4. news.microsoft.com
    Replies:
    0
    Views:
    207
    news.microsoft.com
    Apr 12, 2006
  5. Francisco Garcia
    Replies:
    3
    Views:
    273
    vincent
    Apr 13, 2006
Loading...

Share This Page