java - Adding a statement using Javassist -


system.out.println("a read operation on field encountered ");  

how can add statement, lets , above statement , whenever read operation has been performed on non-local field ? , need know details of field read , set of details should correspond uniqueness of field

example (to remove abstraction in question):

public class greet{    int knowncount;    public greet()    {       system.out.println("hello");       knowncount++;    }       public greet(string language)    {      if(string.equals("english"))  {system.out.println("hello"); knowncount++; }      else  if(string.equals("spanish")) {system.out.println("hola"); knowncount++;}      else  system.out.println("language not recognized");    }     public void showcount()     {      system.out.println("count : "+knowncount);    }  } 

and user class test is:

class test{   public static void main(string[] args){     greet g("spanish");     g.showcount();    }  } 

in above example after using javassist our code should output :

a read operation on field encountered 1 

you can ask using javassist's expreditor. expreditor allows edit done in fieldaccess, implement request can create injector following:

classpool classpool = classpool.getdefault(); ctclass greetctclass = classpool.get(greet.class.getname());  greetctclass.instrument(new expreditor() {         @override         public void edit(fieldaccess fieldaccess)                 throws cannotcompileexception {             if (fieldaccess.getfieldname().equals("knowncount")) {                 fieldaccess                         .replace(" { system.out.println(\"a read operation on field encountered \"); $_ = $proceed($$); } ");             }         }     });      greetctclass             .writefile("<root directory classes are>"); 

best way explain parameter example, imagine greet class (that happens in greatpackage) in following path /home/user/dev/proj1/build/greetpackage/greet.class. in scenario root directory /home/user/dev/proj1/build/.

the line of interest in boiler plate above following:

{ system.out.println(\"a read operation on field encountered \"); $_ = $proceed($$); }

what happening here?

  • first notice all code between curly brackets, if know way in javassist trivial you, if not might make loose few minutes trying understand wrong.
  • then have system.out requested
  • finally have magic line: $_ = $proceed($$);. can find more information in javassist tutorial (search fieldaccess in section, since don't have direct anchor it, sorry!) line saying resulting value of field access the value of virtual method that's being invoked field access, in other words actual value of field.

keep in mind have rewrite greet class injector in separated jvm process , afterwards you'll able use class injected behavior, unless tricks classloading make sure load modified version. i'm not going topics because it's out of scope, if need please so. i'll gladly out.


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -