Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 340
Carlos Eduardo Melo Avatar asked Nov 05 '09 18:11

Carlos Eduardo Melo


1 Answers

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.

like image 175
Keith Randall Avatar answered Nov 19 '22 22:11

Keith Randall