The method of modifying the aspectj of Java Inverse Foundation by around method

Source: Internet
Author: User

In the reverse, we often achieve the goal by modifying a method, there is insertbefore,insertafter,setbody in the Javaassist, in the ASPECTJ can also achieve similar function through around.

To see a simple example

Java file Main.java

Main.javapackage Com.vvvtimes;public class Main {public int add (int x, int y) {return x + y;} public int Add (int x, int y, int z) {return x + y + z;} public static void Main (string[] args) {main m = new main (); System.out.println (M.add (1, 2)); System.out.println (M.add (1, 2, 3));}}

AJ File Tracing.aj

Tracing.ajpublic aspect tracing {private pointcut mainmethod ():             execution (Public static void main (String[ ]); before ():  mainmethod ()  {system.out.println ("> "  + thisjoinpoint); After ():  mainmethod ()  {system.out.println ("< "  + thisjoinpoint); Pointcut addmethodone ()  : call  (Public int add (Int,int)) Int around ()   :  addmethodone ()  {system.out.println ("> "  + thisjoinpoint) Object[] args  = thisjoinpoint.getargs ();for  (int i = 0; i < args.length;  i++)  { //output parameter if ( args[i]!=null)  {system.out.println ("args["  + i +   "]: "  + args[i].tostring ());}} Int result = proceed ();//Here executes the original method body System.out.println ("original return value: " & NBsP;+ result);//output original return value return 777; //specify new value return}pointcut addmethodtwo (int a, int  B,&NBSP;INT&NBSP;C)  : call  (Public int add (int,int,int))  && args   (a, b, c); Int around (int a, int b, int c)  :  addmethodtwo  (a, b, c) {System.out.println ("> "  + thisjoinpoint); System.out.println ("1st passed value: "  + a); System.out.println ("2nd passed value: "  + b); System.out.println ("3rd passed value: "  + c); a = 6;b = 6;c =  6;int result = proceed (a, b, c);//Modify the passed parameter value Return result;}}

The results after the run are as follows

Proceed is used to execute the original method body, return method is used to change the return value

In the first modification we output the parameters, and after executing the original method, we directly specify a return value of 777

In the second modification, we specify the parameters by && args, modify their parameters and then execute the original method body and return.


The method of modifying the aspectj of Java Inverse Foundation by around method

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.