I'm writing my master thesis and the title is "Aspect-oriented software development". I have to do a simple project in AspectJ so I set up Eclipse with AspectJ.
I'm having trouble understanding the AROUND advice. I created 3 files:
1) message_out . java
2) message_test . java
3) simple_aspect . aj
In the simple_aspect file:
1) BEFORE and AFTER advice are working as (I think) expected - so, I "commented" them out.
2) The around advice works only for MESSAGE_ONE() method.
My questions are:
1) Can anyone please explain me (from simple_aspect file) the logic behind naming the parameters in the around advice (WHYTHIS, WHYTHAT)??
2) How to make the aspect accept and change TWO arugments (messages) from MESSAGE_TWO() method.
Thanks :)
message_out.java:
package mypackage;
public class message_out {
public static void message_one(String message) {
System.out.println(message);
}
public static void message_two(String message1, String message2) {
System.out.println(message1 + ", " + message2);
}
}
message_test.java:
package mypackage;
public class message_test {
public static void main(String[] args) {
message_out.message_one("AAAAA");
message_out.message_two("BBBBB", "CCCCCC");
}
}
simple_aspect.aj:
package mypackage;
public aspect simple_aspect {
/*pointcut message_before() : call (* message_out.message_one(..));
before() : message_before() {
System.out.println("BEFORE");
}
pointcut message_after() : call (* message_out.message_one(..));
after() : message_after() {
System.out.println("AFTER");
}*/
pointcut message_around(String WHYTHIS)
: call(* message_out.message_one(String)) && args (WHYTHIS);
void around (String WHYTHAT) : message_around(WHYTHAT) {
WHYTHAT = "CHANGED";
proceed (WHYTHAT);
}
}
So, found a book: "Mastering AspectJ" from Gradecki J. and Lasiecki N. Took me a few hours to understand the logic behind the around advice. I still don't get it completely, but I got an idea how it should work.
The below extract helped me understand a "one argument" and thus apply the logic to "two arguments".
Extract from the book:
1) ARGS designator provides the associated advice code with access to the parameters originally passed to the helloWorldUnique() method.
2) The single parameter type in the method signature - only calls with a single parameter should be considered.
3) ARGS designator has a single parameter. This parameter is directly related to the single parameter passed to the method defined as part of our join point.
4) The pointcut completely defines a single parameter based on a combination of the join point and the args designator.
5) Therefore, if the pointcut is expecting a String called s, there should be a join point defined with a String parameter and an args designator with a variable defined as s.
6) When the pointcut uniqueLog is triggered, its String parameter defined by the s variable is made available to the before() advice body.