CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Mar 2009
    Posts
    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!

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    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 &#91;CODE]...your code here...&#91;/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
  •  





Click Here to Expand Forum to Full Width

Featured