Java: Extending Class At Runtime

前端 未结 4 804
北恋
北恋 2020-12-29 10:55

I have the capability to extend a class at compile time, but I need to be able to create an instance of this subclass at runtime using an instance of the superclass that was

相关标签:
4条回答
  • 2020-12-29 11:20

    I don't know if it's a sollution but if you have

    public static Foo foo = new Foo();
    

    and you want to replace it with Bar which extends Foo make Bar an Wrapper for of Foo and use reflection to let foo point to your Bar instance.

    public class Foo extends Bar {
      private bar; 
      public Foo(Bar oldInstance) {
         this.bar = oldInstance;
      }
    }
    

    and

    // exception handling ommitted
    public static void onStartup() {
       Class fooRefClass = FooRef.class;
       Field fooRef = fooRefClass.getDeclaredField("foo");
    
       Foo foo = (Foo)fooRef.get(null);
       Bar bar = new Bar(foo);
       fooRef.set(null, bar);
    

    }

    As told I don't know if this is possible in your case.

    0 讨论(0)
  • 2020-12-29 11:27

    To reiterate what you are trying to do..

    Within the JVM, there exists an instance of ClassA. You would like to dynamically modify the class heiarchy of ClassA, such that a new class exists called ClassB which derives from ClassA. Then you would like to instantiate an instance of ClassB but have it's subclass implementation be that of the existing instance of ClassA. Something like a memory replacement.

    You might want to look into http://www.jboss.org/javassist . What you would need to do is replace the ClassLoader, then determine when ClassA is being loaded, then instantiated. You'd then need to construct ClassB and return that instead.

    Update

    After a little more research there is still the possibility you can do what you want. IDE's like Eclipse support HotSwap'ing method implementations while debugging. They use the Instrumentation API.

    http://zeroturnaround.com/blog/reloading_java_classes_401_hotswap_jrebel/

    You can replace method bodies but not add or remove methods themselves. So while you won't be able to change the type to your new type, you can completely replace the method implementation with your new implementation.

    0 讨论(0)
  • 2020-12-29 11:27

    Have you looked at Java Proxies?

    Here is a snippet from Javadoc:

    "A dynamic proxy class (simply referred to as a proxy class below) is a class that implements a list of interfaces specified at runtime when the class is created"

    0 讨论(0)
  • I would suggest using cglib:

    cglib is a powerful, high performance and quality Code Generation Library, It is used to extend JAVA classes and implements interfaces at runtime

    You may find some examples here: https://github.com/cglib/cglib/wiki

    0 讨论(0)
提交回复
热议问题