-
March 13th, 2009, 03:19 PM
#1
Castable to anything or dynamically add interface to Dynamic Proxy
I have your typical creation of a DynamicProxy:
MyInterface handler = (Gra) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[] { MyInterface.class }, handler);
After this, now I want handler to be castable to a different interface, let's say Iface2.
If I try to do so:
Iface2 try = (Iface2)handler;
it will throw a class cast exception. I know I could create a new proxy, but that won't work for me because I may already have places in the code that have a reference to this specific instance.
So, either I want to be able to magically make the handler instance castable to a new type, or... is there some way in the initial creation of handler that I could make it castable to any anything to begin with?
I want a proxy that is castable to anything (or that I can add things at runtime)--any calls to it go to InvocationHandler.invoke anyway, so... casting doesn't matter.
Thanks!
-
March 13th, 2009, 08:29 PM
#2
Re: Castable to anything or dynamically add interface to Dynamic Proxy
Java doesn't have magic casting, and you can only cast to the original type or a superclass of it.
If you can explain what you are trying to achieve by this casting, maybe we can suggest an alternative.
The key to performance is elegance, not batallions of special cases...
J. Bently & D. McIlroy
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|