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