Note: Due to JVM optimizations, local variables inside the method cannot intercept and get the values through ASPECTJ, but the member variables can
In the reverse, we often want to track the value of the member variables of some classes, here to obtain the ZKM9 of the QS class member variable G as an example to illustrate
There's a question on StackOverflow: aspectj:how to get accessed field's value in a Get () pointcut
The code that overwrites the content with the QS class is as follows:
Private Pointcut Qsfiledmethod (): Get (* com.zelix.qs.*); after () Returning (Object field): Qsfiledmethod () { System.out.println (Thisjoinpoint.tolongstring ()); System.out.println ("" + thisjoinpoint.getsignature (). GetName ()); System.out.println ("" + field);}
However, this method is flawed and can only get public variables, which are obtained after running the member variables J and K of Qs.
The operation results are as follows
So blocked, then you need to find another way: reflection
A method in the Qs class called the Jj.a method, so call to find the caller, and then through the reflection to get Filed,talk is cheap,show you code?
Private pointcut jjamethod () :call (STRING&NBSP;COM.ZELIX.JJ.A (string, string, string, object, int)); before () : jjamethod () {system.out.println ("> " + Thisjoinpoint);if (Thisjoinpoint.getthis () != null) { system.out.println ("this " +thisjoinpoint.getthis (). GetClass (). GetName () + " " + thisjoinpoint.getsourcelocation ()); Object obj = Thisjoinpoint.getthis (); class Clazz = obj.getclass (); //Traversal Members Field[] fileds = clazz.getdEclaredfields (); for (field field : fileds) { system.out.println (field); } try { //get a single member private final java.lang.string[] com.zelix.qs.g // and outputs its value Field filed = clazz.getdeclaredfield ("G"); System.out.println (filed); Filed.setaccessible (true); string[] g= (string[]) filed.get (obj);for (int i = 0; i < g.length; i++) {system.out.println ("g[" +i+ "] =" +g[i]);}} catch (exception e) {e.printstacktrace ();} }else if (Thisjoinpoint.gettarget () != NULL) &NBSP;{&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SYSTEM.OUT.PRINTLN ("Target "+thisjoinpoint.gettarget (). GetClass (). GetName () + &NBsp;+ thisjoinpoint.getsourcelocation ()); }}
The operation results are as follows
The functions in the Before method are as follows
1. Print out the name and location of the caller
2. Traverse print all member names of the QS class
3. Gets the value of the member G, as this member is an array type, traversing the array to print the value
The aspectj of the Java Inverse Foundation gets the value of the member variable