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