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

john
  • 203
  • 1
  • 2
  • 4
  • 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. – Drupad Panchal Aug 30 '11 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. – jefflunt Aug 30 '11 at 17:54

1 Answers1

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

Andreas Dolk
  • 113,398
  • 19
  • 180
  • 268