Reputation: 6440
The following is an excerpt from an AspectJ example:
class Point {
int x, y;
public void setX(int x) { this.x = x; }
// ...
}
aspect PointAssertions {
private boolean Point.assertX(int x) {
return (x <= 100 && x >= 0);
}
// ...
before(Point p, int x): target(p) && args(x) && call(void setX(int)) {
if (!p.assertX(x)) {
System.out.println("Illegal value for x"); return;
}
}
// ...
}
Could someone please clarify for me what the return;
statement does in the before
advice - why it is (needs to be?) there and what would happen without it?
Will it basically "return form setX()
" before the method body is executed?
Is there a manual page explaining it?
Upvotes: 0
Views: 124
Reputation: 11715
To complete the answer by @LajosArpad, before
advices can only change the flow by throwing an exception. The only advice that can prevent calling the advised method while retaining the same flow semantics is around
, but it needs to return something itself in that case (if the return type is not void
, of course).
Upvotes: 2
Reputation: 76884
Your code checks this:
(!p.assertX(x))
whenever the logical expression above is true, the code in its block of { }
will be executed, including the return;
. The point of the return
statement is to make sure that the function stops and no further code will be executed. It is a common approach to do so in case of errors.
Upvotes: 1