1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
package answers;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import figures.*;
import java.awt.Rectangle;
@Aspect
public class Answer2h {
@Pointcut("call(public void figures.FigureElement+.move" +
"()) && target(fe)")
void movingFigureElement(Point fe) {}
@Around("movingFigureElement(fe)")
public void checkIfBoundsMovedSame(ProceedingJoinPoint thisJoinPoint,
Point fe) throws Throwable {
/*
@Pointcut("call(public void figures.FigureElement+.move" +
"(int, int)) && target(fe) && args(dx, dy)")
void movingFigureElement(FigureElement fe, int dx, int dy) {}
@Around("movingFigureElement(fe, dx, dy)")
public void checkIfBoundsMovedSame(ProceedingJoinPoint thisJoinPoint,
FigureElement fe, int dx, int dy) throws Throwable {
*/
Rectangle rectangleBefore = new Rectangle(fe.getBounds());
//thisJoinPoint.proceed(new Object[]{fe, dx, dy});
thisJoinPoint.proceed(new Object[]{fe});
// rectangleBefore.translate(dx, dy);
if(!rectangleBefore.equals(fe.getBounds()))
throw new IllegalStateException("move() invariant violation");
// IF THE THREE LINES BELOW ARE UN-COMMENTED, THE EXCEPTION
// ISN'T THROWN!?
// Note: The three lines can be located anywhere inside the advice.
// for(Object o: thisJoinPoint.getArgs()) {
// System.out.print(o+" ");
// }
}
}
|