开发者

Substitute (override) class implementation at Runtime (Java)

Is there any way of substituting (overriding) a Java class implementation, which is already loaded by the System class loader by another implementation (available as an array of bytes)?

To illustrate my doubt, follows this code:

public class Main {
    public static void main(String ... args) {
        Foo foo = new Foo();
        foo.print();

        ClassLoader cl = ...

        Foo foo2 = (Foo) cl.newInstance();
        foo2.print();
    }
}

The print() method of the first Foo prints "Implementation 1", as the second one prints "Implementation 2". The second instance of foo is retrieved by the class loader from an array of bytes (which can be stored in a file, 开发者_开发百科or got from any stream...)

PS: Is a requirement that Foo is a class, not an interface, and cannot be extended, i.e., the actual bytes (inside the VM) that defines the class implementation are overrided.


Yes, this is no problem. You should use a java.net.URLClassLoader. For example, you could give it a url which is a directory where your overriding Foo.class file lives.

Edit: Then the call you want is cl.loadClass("Foo").newInstance(). You can't cast the result to a Foo, but you can use reflection to call its print method. Or make Foo a subclass (or interface implementation) of something you won't be reimplementing which defines a print method, and cast to that.


CGLib does things like this. It is used in Spring & Hibernate for this purpose.


You can implement an instrumentation agent and use java.lang.instrument.Instrumentation#redefineClasses(...) to replace the bytecode of already loaded classes.


I use JMX to undeploy and the redeploy classes.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜