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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
import java.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import java.lang.reflect.*;
/**
* test annotations on itds in various ways
* 1) annotated ITD
* 2) binding of an annotation on an ITD member
* 3) annotated ITD via declare @xxx
* 4) binding of annotated ITD via declare
*/
public aspect AnnotationsAndITDs {
// annotated ITD constructors
@SomeAnnotation(s="hello",clazz=AnnotationsAndITDs.class)
public ITDMe.new(String s) {}
@SomeAnnotation(s="goodbye",clazz=String.class)
private ITDMe.new(int x) {}
// annotated ITD methods
@SomeAnnotation(s="x",clazz=Object.class)
private void ITDMe.foo() {}
@SomeAnnotation(s="y",clazz=Integer.class)
public void ITDMe.bar(@ParamAnnotation int x) {}
// annotated ITD fields
@SomeAnnotation(s="d",clazz=Double.class)
public Double ITDMe.d;
@SomeAnnotation(s="f",clazz=Double.class)
private Float ITDMe.f;
// declare @xxx on ITD members
// =============================
// annotated ITD constructors
declare @constructor : ITDMe2.new(..) : @SomeAnnotation(s="@cons",clazz=String.class);
public ITDMe2.new(String s) {}
private ITDMe2.new(int x) {}
// annotated ITD methods
declare @method : * ITDMe2.*(..) : @SomeAnnotation(s="@method",clazz=ITDMe2.class);
private void ITDMe2.foo() {}
public void ITDMe2.bar(@ParamAnnotation int x) {}
// annotated ITD fields
declare @field : * ITDMe2.* : @SomeAnnotation(s="@field",clazz=ITDMe2.class);
public Double ITDMe2.d = 2d;
private Float ITDMe2.f = 1.0f;
declare @type : ITDMe* : @SomeAnnotation(s="@type",clazz=System.class);
public static void main(String[] args) {
ITDMe itdme1 = new ITDMe("foo");
ITDMe itdme2 = new ITDMe(5);
itdme1.foo();
itdme1.bar(6);
itdme1.d = 1.0d;
itdme1.f = 1.0f;
ITDMe2 itdme21 = new ITDMe2("foo");
ITDMe2 itdme22 = new ITDMe2(5);
itdme21.foo();
itdme21.bar(6);
itdme21.d = 1.0d;
itdme21.f = 1.0f;
}
}
aspect AnnotationTests {
// static tests
declare warning : execution(@SomeAnnotation * *(..)) : "execution(@SomeAnnotation ...)";
declare warning : execution(@SomeAnnotation new(..)) : "execution(@SomeAnnotation ...new(..)";
declare warning : set(@SomeAnnotation * *) : "set(@SomeAnnotation...)";
declare warning : staticinitialization(@SomeAnnotation *) : "si(@SomeAnnotation...)";
// binding tests
before(SomeAnnotation sa) : execution(public ITDMe.new(String)) && @annotation(sa) {
print(sa);
}
after(SomeAnnotation sa) : execution(private ITDMe.new(int)) && @annotation(sa) {
print(sa);
}
before(SomeAnnotation sa) : execution(private ITDMe.new(int)) && @annotation(sa) {
print(sa);
}
after(SomeAnnotation sa) : execution(public void ITDMe.bar(int)) && @annotation(sa) {
print(sa);
MethodSignature sig = (MethodSignature) thisJoinPoint.getSignature();
Method meth = sig.getMethod();
Annotation[][] anns = meth.getParameterAnnotations();
System.out.println("method bar has " + anns.length + " params, first param annotation is "
+ anns[0][0].toString());
}
before(SomeAnnotation sa) : set(public Double ITDMe.d) && @annotation(sa) {
print(sa);
}
after(SomeAnnotation sa) : set(private Float ITDMe.f) && @annotation(sa) {
print(sa);
}
after(SomeAnnotation sa) returning : staticinitialization(@SomeAnnotation *) && @annotation(sa){
print(sa);
}
// now repeat for the @declared versions
before(SomeAnnotation sa) : execution(public ITDMe2.new(String)) && @annotation(sa) {
print(sa);
}
after(SomeAnnotation sa) : execution(private ITDMe2.new(int)) && @annotation(sa) {
print(sa);
}
before(SomeAnnotation sa) : execution(private ITDMe2.new(int)) && @annotation(sa) {
print(sa);
}
after(SomeAnnotation sa) : execution(public void ITDMe2.bar(int)) && @annotation(sa) {
print(sa);
MethodSignature sig = (MethodSignature) thisJoinPoint.getSignature();
Method meth = sig.getMethod();
Annotation[][] anns = meth.getParameterAnnotations();
System.out.println("method bar has " + anns.length + " params, first param annotation is "
+ anns[0][0].toString());
}
before(SomeAnnotation sa) : set(public Double ITDMe2.d) && @annotation(sa) {
print(sa);
}
after(SomeAnnotation sa) : set(private Float ITDMe2.f) && @annotation(sa) {
print(sa);
}
private void print(SomeAnnotation sa) {
System.out.println(sa.s() + " " + sa.clazz().getName());
}
}
@Retention(RetentionPolicy.RUNTIME)
@interface SomeAnnotation {
String s();
Class clazz();
}
@Retention(RetentionPolicy.RUNTIME)
@interface ParamAnnotation{
String value() default "";
}
class ITDMe {
}
class ITDMe2 {
}
|