We currently use premain to transform all classes before they have been loaded. But we want to make the transformation under control, Which means we can remove/re-add the code we inserted at the methods.
So we found the retransform, but i am not quite understand it.
- can we retransform classes loaded?
- if it can be done, will the instances of retransformed classes run the retransformed code?
- if it can't be done, is there any technology to change the code of some specified method at runtime.
UPDATED
For example, we have class A. Class A has two method foo() and bar():
void foo() {
while(true) {
bar();
}
}
void bar() {
System.out.println("a");
}
we call Instrument.retransformClasses(A.class). and change bar() to:
void bar() {
System.out.println("b");
}
If there is a thread already invoking foo(), can we have the output:
...
a
a
a
b
b
b
...
if not, is there any way to implement it?