开发者

Enhancing a java object at runtime

开发者 https://www.devze.com 2023-03-31 15:45 出处:网络
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.

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消