0

I am aware that using proxy capable libraries (javassist, JDK dynamic proxies, etc) that it is possible to enhance a class to implement an interface at runtime.

My question is: is it possible to enhance an instantiated object to implement an interface at runtime (and likewise provide appropriate method handlers).

2
  • 1
    Could you describe a use-case where doing this helps? My suggestion would be to re-consider your design if you have to do this. Commented Aug 30, 2011 at 17:46
  • Agreed - if, for example, you're trying to write an app that can be upgraded in-place (or something similar) without needing to be restarted, there are better ways to handle this. Commented Aug 30, 2011 at 17:54

1 Answer 1

2

No, we can't change a class that has already been loaded. And we can't replace a loaded class (within a classloader).

There may be a chance if you

  • load a class with a custom classloader
  • unload the classloader (should unload the class aswell)
  • load the modified class again with a new classloader instance

But that is very, very black magic...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.