Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Extending Class At Runtime

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 already instantiated.

This should be possible in theory because superclass constructors are already called before the subclass constructor.

I do not have access to the program sufficiently to change the instantiation to my subclass nor to interrupt the original instantiation.

Use Case: There is an existing array of instances of class X. My code is loaded in after. I need to override one of the methods of one of the instances X with my loaded subclass Y extends X. The parent program accesses the objects only through that array, so I want to replace that array element with my Y instance, but it needs to behave as if it were instantiated originally into that array. I cannot just enclose the superclass instance and forward calls, and there are difficult complications with reinstantiating the superclass.

I hope that is more clear.

like image 371
JAKJ Avatar asked Apr 06 '12 16:04

JAKJ


People also ask

How do you extend a class dynamically in Java?

Java has two ways to do dynamic extension: forName() , and class loaders. forName() , a static method in java. lang. Class , is the simple, straightforward way to do dynamic extension.

Can a class be extended in Java?

The extends keyword extends a class (indicates that a class is inherited from another class). In Java, it is possible to inherit attributes and methods from one class to another.

When should you extend a class?

You extend a class when you want the new class to have all the same features of the original, and something more. The child class may then either add new functionalities, or override some funcionalities of the parent class.

Can I extend a class in different package?

There is nothing wrong with extending a class from some other package. That the class is a private inner class makes this even less of an issue (as it is hiding the implementation details within itself). Dependencies are a nightmare - and one that we have to live with. There are two parts to this.


2 Answers

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"

like image 193
Alex Paransky Avatar answered Oct 21 '22 15:10

Alex Paransky


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.

like image 41
Andrew T Finnell Avatar answered Oct 21 '22 16:10

Andrew T Finnell