Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #5246
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail |
|---|---|
| From | Robert Klemme <shortcutter@googlemail.com> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: Create Dynamic Proxy for class instead of interface |
| Date | Sun, 12 Jun 2011 19:07:41 +0200 |
| Lines | 39 |
| Message-ID | <95ka2uFjg5U1@mid.individual.net> (permalink) |
| References | <it2hld$h99$1@dont-email.me> <it2j4j$q7m$1@dont-email.me> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=ISO-8859-1; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-Trace | individual.net wHuYxNC54iZjPYFUMW+h+AXrzu2PfOq7ppAcQNU7VATHYvz80= |
| Cancel-Lock | sha1:z2IrwQem/jXxqeHkFqUk8F9MDWI= |
| User-Agent | Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.17) Gecko/20110516 Lightning/1.0b2 Thunderbird/3.1.10 |
| In-Reply-To | <it2j4j$q7m$1@dont-email.me> |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:5246 |
Show key headers only | View raw
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
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Next in thread | Find similar
Create Dynamic Proxy for class instead of interface Stanimir Stamenkov <s7an10@netscape.net> - 2011-06-12 17:15 +0300
Re: Create Dynamic Proxy for class instead of interface Stanimir Stamenkov <s7an10@netscape.net> - 2011-06-12 17:40 +0300
Re: Create Dynamic Proxy for class instead of interface Robert Klemme <shortcutter@googlemail.com> - 2011-06-12 19:07 +0200
Re: Create Dynamic Proxy for class instead of interface Stanimir Stamenkov <s7an10@netscape.net> - 2011-06-12 20:42 +0300
Re: Create Dynamic Proxy for class instead of interface Tom Anderson <twic@urchin.earth.li> - 2011-06-13 11:58 +0100
csiph-web