I am hot-deploying code of methods in my development-environment.
The code is affected immediately unless the code change the signature of methods or the structure of the class.
This works fine, I have no problem with that.
Unfortunately I must trigger the execution of the method again in order to have this code executed. Can I register a Listener that get notified when the JVM recieves a JPDA-HotCodeReplace?
Whatever IDE/debugger you use, Hot Code Replace eventually ends up in calling either RedefineClasses or RetransformClasses JVM TI function.
So, the idea is to intercept these two functions and call listeners upon their invocation. This can be achieved with a native (JNI) library.
HotCodeReplaceListener.c
Now in Java code we just need to load this JNI library.
fireListenersmethod will be called from the native code using JNI.HotCodeReplace.java
How to use
Update
There is no pure Java API to get notifications for class redifinition, except for
jdk.ClassRedefinitionJFR event appeared in JDK 15:The closest JDK 8 alternative is to attach a Java agent that registers dummy ClassFileTransformer to receive callbacks for every loaded or redefined class. The downside of this approach is that the callback fires for every single class, but before the actual redefinition. However, if immediate synchronous notification is not required, you can workaround it with a background timer.
Note: in order to use Instrumentation API, you'll have to start the JVM with
-javaagentoption or use Dynamic Attach to attach an agent in runtime.