简体   繁体   中英

how to record/monitor java field assignment operation

how to record/monitor java field assignment operation ; for example , i want to add some function before or after the assignment operation;

original code :

class Test{
 public String name;

 public void operation{
             .... some code ...
         name="stackoverflow";
            .... some code ...
  }
}

After changing the code :

class Test{
 public String name;
 public void operation{
          .... some code ...
      [[ my instrument code ]] 
         name="stackoverflow";
      [[ my instrument code ]]
           .... some code ...
  }
}

NOTE: I want to instrument before or after every filed assignment line ; so it can not mannually point out which line should be instrument

is it possible using JAVA DEBUG INTERFACE ?? or is there any way to reach it INStrument or Dynamic Proxy

You can do that using javassist. Works for fields, but not arrays. I have done something like that in my fast serialization struct simulation:

method.instrument( new ExprEditor() {
                        @Override
                        public void edit(FieldAccess f) throws CannotCompileException {
                            try {
                                if ( ! f.isStatic() ) {
                                    CtClass type = null;
                                    type = f.getField().getType();
                                    FSTClazzInfo.FSTFieldInfo fieldInfo = clInfo.getFieldInfo(f.getFieldName(), null);
                                    if ( fieldInfo == null ) {
                                        return;
                                    }
                                    if ( f.isReader() ) {
                                        structGen.defineStructReadAccess(f, type, fieldInfo);
                                    } else if ( f.isWriter() ) {
                                        structGen.defineStructWriteAccess(f, type, fieldInfo);
                                    }
                                }
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });

from https://code.google.com/p/fast-serialization/source/browse/trunk/src/main/java/de/ruedigermoeller/heapoff/structs/unsafeimpl/FSTStructFactory.java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM