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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
import java.lang.reflect.Method;
import org.aspectj.lang.annotation.*;
public aspect BasicAdvice {
@SuppressAjWarnings
before() : execution(* *.*(..)) {
//
}
@SuppressAjWarnings
after() : call(* Integer.*(..)) {
}
@SuppressAjWarnings
after() returning : get(String *) {
}
@SuppressAjWarnings
after() returning(String strVal) : get(String *) {
}
@SuppressAjWarnings
after() throwing : execution(* *.*(..)) {
}
@SuppressAjWarnings
after() throwing(RuntimeException ex) : execution(* *.*(..)) {
}
@SuppressAjWarnings
void around() : set(* foo) {
proceed();
}
private static int adviceCount = 0;
public static void main(String[] args) {
Method[] methods = BasicAdvice.class.getDeclaredMethods();
for (Method method : methods) {
adviceCount++;
if (method.getName().startsWith("ajc$before$")) {
checkBefore(method);
} else if (method.getName().startsWith("ajc$after$")) {
checkAfter(method);
} else if (method.getName().startsWith("ajc$afterReturning$")) {
checkAfterReturning(method);
} else if (method.getName().startsWith("ajc$afterThrowing$")) {
checkAfterThrowing(method);
} else if (method.getName().startsWith("ajc$around$")) {
if (!method.getName().endsWith("proceed")) {
checkAround(method);
} else {
adviceCount--;
}
} else {
adviceCount--;
}
}
if (adviceCount != 7) throw new RuntimeException("Expected 7 advice methods, found " + adviceCount);
}
private static void checkBefore(Method method) {
assertTrue("expecting 2 annotations on before",method.getAnnotations().length == 2);
Before beforeAnnotation = method.getAnnotation(Before.class);
assertTrue("expecting execution(* *.*(..))",beforeAnnotation.value().equals("execution(* *.*(..))"));
}
private static void checkAfter(Method method) {
assertTrue("expecting 2 annotations on after",method.getAnnotations().length == 2);
After afterAnnotation = method.getAnnotation(After.class);
assertTrue("expecting call(* Integer.*(..))",afterAnnotation.value().equals("call(* Integer.*(..))"));
}
private static void checkAfterReturning(Method method) {
assertTrue("expecting 2 annotations on after returning",method.getAnnotations().length == 2);
AfterReturning afterAnnotation = method.getAnnotation(AfterReturning.class);
if (method.getParameterTypes().length == 1) {
// form with returning arg
assertTrue("expecting get(String *)",afterAnnotation.pointcut().equals("get(String *)"));
assertTrue("expecting empty",afterAnnotation.value().equals(""));
assertTrue("expecting strVal",afterAnnotation.returning().equals("strVal"));
} else {
// form without returning arg
assertTrue("expecting get(String *)",afterAnnotation.pointcut().equals("get(String *)"));
assertTrue("expecting empty",afterAnnotation.value().equals(""));
assertTrue("expecting empty returning",afterAnnotation.returning().equals(""));
}
}
private static void checkAfterThrowing(Method method) {
assertTrue("expecting 2 annotations on after throwing",method.getAnnotations().length == 2);
AfterThrowing afterAnnotation = method.getAnnotation(AfterThrowing.class);
if (method.getParameterTypes().length == 1) {
// form with returning arg
assertTrue("expecting execution(* *.*(..))",afterAnnotation.pointcut().equals("execution(* *.*(..))"));
assertTrue("expecting empty",afterAnnotation.value().equals(""));
assertTrue("expecting ex",afterAnnotation.throwing().equals("ex"));
} else {
// form without returning arg
assertTrue("expecting execution(* *.*(..))",afterAnnotation.pointcut().equals("execution(* *.*(..))"));
assertTrue("expecting empty",afterAnnotation.value().equals(""));
assertTrue("expecting empty throwing",afterAnnotation.throwing().equals(""));
}
}
private static void checkAround(Method method) {
assertTrue("expecting 2 annotations on around",method.getAnnotations().length == 2);
Around aroundAnnotation = method.getAnnotation(Around.class);
assertTrue("expecting set(* foo)",aroundAnnotation.value().equals("set(* foo)"));
}
private static void assertTrue(String msg, boolean expr) {
if (!expr) throw new RuntimeException(msg);
}
}
|