The aspectj of the Java Inverse Foundation gets the value of the member variable

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.