1 Reply Latest reply on Feb 22, 2014 2:57 PM by kjkrum

    [solved] First time with Javassist - my SSCCE doesn't work as I expected


      (Originally posted here)


      I took my very first step with Javassist by writing a SSCCE that tries to modify a field access.  It compiles and runs without error, but the output is not what I expected.



      package test;


      import javassist.CannotCompileException;

      import javassist.ClassPool;

      import javassist.CtClass;

      import javassist.CtMethod;

      import javassist.NotFoundException;

      import javassist.expr.ExprEditor;

      import javassist.expr.FieldAccess;


      public class Test1 {

          public static void main(String[] args) {

              try {

                  // instrument the class

                  final CtClass ctClass = ClassPool.getDefault().get(Foo.class.getName());

                  final CtMethod ctMethod = ctClass.getDeclaredMethod("setValue");

                  ctMethod.instrument(new ExprEditor() {


                      public void edit(FieldAccess f) throws CannotCompileException {

                          //System.out.printf("instrumenting field access in %s, line %d\n", f.getFileName(), f.getLineNumber());





                  // test the instrumentation

                  Foo foo = new Foo();




              catch (NotFoundException | CannotCompileException e) {








      package test;


      public class Foo {

          private int value;


          public int getValue() {

              return value;



          public void setValue(int value) {

              this.value = value;





      I expected the output to be:





      ...because I expected that the field assignment would be replaced by the code that prints "fnord", and the value would remain uninitialized.  But instead, the output is:




      The code that attempts to replace the field assignment definitely runs and operates on the correct line, as I can see if I uncomment the print statement there.  What am I doing wrong?