views:

352

answers:

4

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.

Best Regards,

Carlos Eduardo Melo

A: 

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

an0nym0usc0ward
A: 

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

jarnbjo
+2  A: 

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.

Keith Randall
A: 

I use JMX to undeploy and the redeploy classes.

Javamann