Given an class hierarchy:
A -> B -> C -> instanceOfC
is it possible (and how) to insert a class, temporarily, at runtime, like so:
A -> B -> B' -> C -> instanceOfC?
It's possible if you use AspectJ. AspectJ has a
Inheritance vs Aggregation
But methinks you are trying to solve the wrong problem here. I'd say try to replace inheritance with aggregation, let
The API that allows the most invasive changes into a running JVM is probably the JVM Tool Interface. It is designed to develop debuggers that can dynamically change methods during runtime to facilitate fix-and-continue debugging.
The relevant call for replacing the definition of a class would be
Therefore I'd say that you can't sanely modify the type hierarchy of already-loaded classes in an unmodified JVM. Either change the class hierarchy during loading (with a custom
No, you can not.
You can do:
Losing some good manners, you could do
If instance of
I'm not 100%, but I think this would only be possible if both B' and C were dynamically loaded.