blob: 520c5eb96f2ae91899db75fc4e14b897d7918ab9 (
plain)
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
|
import org.aspectj.lang.reflect.*;
import java.lang.reflect.*;
import java.lang.annotation.*;
public aspect DeclareAnnotationTest {
declare @type : a.b.c..* : @MyAnnotation("ady 1");
declare @method : * *(String) : @MyAnnotation("ady 2");
declare @field : java.io.Serializable+ * : @MyClassRetentionAnnotation("ady 3");
declare @constructor : new(String,..) : @MyAnnotation;
public static void main(String[] args) throws ClassNotFoundException {
AjType<DeclareAnnotationTest> myType = AjTypeSystem.getAjType(DeclareAnnotationTest.class);
DeclareAnnotation[] decAs = myType.getDeclareAnnotations();
if (decAs.length != 4) throw new RuntimeException("Expecting 4 members, got " + decAs.length);
int atTypeIndex = -1;
int atMethodIndex = -1;
int atFieldIndex = -1;
int atConstructorIndex = -1;
int index = 0;
for (DeclareAnnotation da : decAs) {
switch (da.getKind()) {
case Field:
atFieldIndex = index++;
break;
case Method:
atMethodIndex = index++;
break;
case Constructor:
atConstructorIndex = index++;
break;
case Type:
atTypeIndex = index++;
break;
default: throw new RuntimeException ("unknown type");
}
}
checkAtType(decAs[atTypeIndex]);
checkAtMethod(decAs[atMethodIndex]);
checkAtField(decAs[atFieldIndex]);
checkAtConstructor(decAs[atConstructorIndex]);
}
private static void checkAtType(DeclareAnnotation da) {
if (da.getKind() != DeclareAnnotation.Kind.Type) throw new RuntimeException("expecting @type");
if (da.getSignaturePattern() != null) throw new RuntimeException("not expecting a signature pattern");
if (!da.getTypePattern().asString().equals("a.b.c..*")) throw new RuntimeException("expecting 'a.b.c..*' but got '" + da.getTypePattern().asString() + "'");
if (da.getDeclaringType().getJavaClass() != DeclareAnnotationTest.class) throw new RuntimeException("bad declaring type: " + da.getDeclaringType());
MyAnnotation ma = (MyAnnotation) da.getAnnotation();
if (!ma.value().equals("ady 1")) throw new RuntimeException("bad value: " + ma.value());
if (!da.getAnnotationAsText().equals("@MyAnnotation(\"ady 1\")")) throw new RuntimeException("bad annotation text: " + da.getAnnotationAsText());
}
private static void checkAtMethod(DeclareAnnotation da) {
if (da.getKind() != DeclareAnnotation.Kind.Method) throw new RuntimeException("expecting @method");
if (da.getTypePattern() != null) throw new RuntimeException("not expecting a type pattern");
if (!da.getSignaturePattern().asString().equals("* *(java.lang.String)")) throw new RuntimeException("expecting '* *(java.lang.String)' but got '" + da.getSignaturePattern().asString() + "'");
if (da.getDeclaringType().getJavaClass() != DeclareAnnotationTest.class) throw new RuntimeException("bad declaring type: " + da.getDeclaringType());
MyAnnotation ma = (MyAnnotation) da.getAnnotation();
if (!ma.value().equals("ady 2")) throw new RuntimeException("bad value: " + ma.value());
if (!da.getAnnotationAsText().equals("@MyAnnotation(\"ady 2\")")) throw new RuntimeException("bad annotation text: " + da.getAnnotationAsText());
}
private static void checkAtField(DeclareAnnotation da) {
if (da.getKind() != DeclareAnnotation.Kind.Field) throw new RuntimeException("expecting @field");
if (da.getTypePattern() != null) throw new RuntimeException("not expecting a type pattern");
if (!da.getSignaturePattern().asString().equals("java.io.Serializable+ *")) throw new RuntimeException("expecting 'java.io.Serializable+ *' but got '" + da.getSignaturePattern().asString() + "'");
if (da.getDeclaringType().getJavaClass() != DeclareAnnotationTest.class) throw new RuntimeException("bad declaring type: " + da.getDeclaringType());
if (da.getAnnotation() != null) throw new RuntimeException("expecting null annotation, but got " + da.getAnnotation());
if (!da.getAnnotationAsText().equals("@MyClassRetentionAnnotation(\"ady 3\")")) throw new RuntimeException("bad annotation text: " + da.getAnnotationAsText());
}
private static void checkAtConstructor(DeclareAnnotation da) {
if (da.getKind() != DeclareAnnotation.Kind.Constructor) throw new RuntimeException("expecting @constructor");
if (da.getTypePattern() != null) throw new RuntimeException("not expecting a type pattern");
if (!da.getSignaturePattern().asString().equals("new(java.lang.String, ..)")) throw new RuntimeException("expecting 'new(java.lang.String,..)' but got '" + da.getSignaturePattern().asString() + "'");
if (da.getDeclaringType().getJavaClass() != DeclareAnnotationTest.class) throw new RuntimeException("bad declaring type: " + da.getDeclaringType());
MyAnnotation ma = (MyAnnotation) da.getAnnotation();
if (!ma.value().equals("some value")) throw new RuntimeException("bad value: " + ma.value());
if (!da.getAnnotationAsText().equals("@MyAnnotation")) throw new RuntimeException("bad annotation text: " + da.getAnnotationAsText());
}
}
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
String value() default "some value";
}
@interface MyClassRetentionAnnotation {
String value();
}
|