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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
import java.lang.annotation.*;
import java.lang.reflect.*;
import org.aspectj.lang.*;
import org.aspectj.lang.reflect.*;
public aspect RuntimeTypeMatching {
public static void main(String[] args) {
A a = new A();
B b = new B();
a.a();
b.b();
b.callA(a);
ByeByeEJB pojo = new ByeByeEJB();
pojo.method1();
pojo.method2();
pojo.method3();
}
after() returning : @this(Foo) && execution(* A+.*(..)) {
System.out.println("@this(Foo) at " + thisJoinPoint.toString());
}
after() returning : call(* *(..)) && @target(Classified) {
System.out.println("@target(Classified) at " + thisJoinPoint.toString());
}
pointcut callToClassifiedObject(Classified classificationInfo) :
call(* *(..)) && @target(classificationInfo);
before(Classified classification) : callToClassifiedObject(classification) {
System.out.println("This information is " + classification.classification());
}
pointcut txRequiredMethod(Tx transactionAnnotation) :
execution(* *(..)) && @this(transactionAnnotation)
&& if(transactionAnnotation.policy() == TxPolicy.REQUIRED);
before() : txRequiredMethod(Tx) {
System.out.println("(Class) Transaction required at " + thisJoinPoint);
}
before(Tx tranAnn) : execution(* *(..)) && @annotation(tranAnn) && if(tranAnn.policy()==TxPolicy.REQUIRED) {
System.out.println("(Method) Transaction required at " + thisJoinPoint);
}
/**
* matches any join point with at least one argument, and where the
* type of the first argument has the @Classified annotation
*/
pointcut classifiedArgument() : @args(Classified,..);
before() : classifiedArgument() {
System.out.println("Classified data being passed at " + thisJoinPoint);
}
/**
* matches any join point with three arguments, where the third
* argument has an annotation of type @Untrusted.
*/
pointcut untrustedData(Untrusted untrustedDataSource) :
@args(*,*,untrustedDataSource);
before(Untrusted source) : untrustedData(source) {
System.out.println("Untrusted data being passed at " + thisJoinPoint);
if (source == null) System.out.println("FAIL");
}
before() : execution(* callA(..)) {
Annotation[] thisAnnotations = thisJoinPoint.getThis().getClass().getAnnotations();
Annotation[] targetAnnotations = thisJoinPoint.getTarget().getClass().getAnnotations();
Annotation[] firstParamAnnotations = thisJoinPoint.getArgs()[0].getClass().getAnnotations();
System.out.println(thisAnnotations.length + " " + thisAnnotations[0].toString());
System.out.println(targetAnnotations.length + " " + targetAnnotations[0].toString());
System.out.println(firstParamAnnotations.length + " " + firstParamAnnotations[0].toString());
}
// up to @within and @withincode examples
declare warning : @within(Foo) && execution(* *(..)) : "@within(Foo)";
pointcut insideCriticalMethod(Critical c) : @withincode(c);
before(Critical c) : insideCriticalMethod(c) {
System.out.println("Entering critical join point with priority " + c.priority());
}
before() : insideCriticalMethod(Critical) {
Signature sig = thisEnclosingJoinPointStaticPart.getSignature();
AnnotatedElement declaringTypeAnnotationInfo = sig.getDeclaringType();
if (sig instanceof MethodSignature) {
// this must be a call or execution join point.
Method method = ((MethodSignature)sig).getMethod();
Critical c = method.getAnnotation(Critical.class);
System.out.println("Entering critical join point with reflectively obtained priority " + c.priority());
}
}
}
@Retention(RetentionPolicy.RUNTIME) @interface Foo {}
@Retention(RetentionPolicy.RUNTIME) @interface Classified {
String classification() default "TOP-SECRET";
}
@Retention(RetentionPolicy.RUNTIME) @interface Untrusted {}
@Retention(RetentionPolicy.RUNTIME) @interface Critical {
int priority() default 5;
}
enum TxPolicy { REQUIRED, REQUIRESNEW }
@Retention(RetentionPolicy.RUNTIME) @interface Tx {
TxPolicy policy() default TxPolicy.REQUIRED;
}
@Classified class A {
void a() {};
}
@Foo class B extends A {
void b() {};
@Critical(priority=3) void callA(A a) { a.a(); }
}
@Tx(policy=TxPolicy.REQUIRED)
class ByeByeEJB {
@Tx void method1() {}
@Tx(policy=TxPolicy.REQUIRED) void method2() {}
@Tx(policy=TxPolicy.REQUIRESNEW) void method3() {}
}
@Untrusted class Dodgy {}
class ToTrustOrNot {
void a() {
b(5,2,new Dodgy());
b(5,2,new String());
}
void b(int x, int y, Object o) {}
}
|