I have implement Peter Lawrey suggestion and its works well, under a condition that, the new class definition must be the first class that loaded to the classloader, for example:
A : a class which definition will be revised (mutated)
B : other class that will never be changed.
My program will be work if the call sequence was load A then B like below
for( somecondition){
String ASourceCode = mutation(); //some operation of generation/manipulation/mutation of ASourceCode
ClassLoader classLoader = new ClassLoader() { };
Class AClass = CompilerUtils.CACHED_COMPILER.loadFromJava( classLoader, "A" , ASourceCode);
Class BClass = CompilerUtils.CACHED_COMPILER.loadFromJava( classLoader, "B" , BSourceCode);
}
In that sequence, the AClass will be referenced to the new definition (source code) each time loaded (each iteration)
but if the load sequence was B then A, (because class A may need class B to compile), the AClass was still using the first loaded definition of class A, and will never be changed to its newly generated definition (source code).
I first suspect that i can make it work by commenting line 18-19 from loadFromJava class from your library (attached picture), so each time loading a class, it will recompile the class and other previously loaded class within the same classloader (which sacrifice efficiency), but it turn out giving a wrong result.
// if (clazz != null)
// return clazz;
Thank you very much for all your help.