I'm playing with javassist (to use it later on a project) but I don't manage to make a simple update to a class. I try to insert code before a method but its not being executed.
I've a gradle project and I'm using javassist version: '3.27.0-GA'.
Given the following class:
public class Dummy{
public int dummy(){
return 5;
}
}
The following test fails, so the class is not being modified:
@Test
public void modifyReturnValueTest() throws NotFoundException, CannotCompileException, IOException {
ClassPool pool = ClassPool.getDefault();
CtClass cc = pool.get("Dummy");
CtMethod m = cc.getDeclaredMethod("dummy");
m.insertBefore("{ if(true) return 3; }");
cc.writeFile();
assertEquals(3, new Dummy().dummy());
}
I'm missing something?