1
import org.aspectj.lang.reflect.*;
2
import java.lang.reflect.*;
3
import java.lang.annotation.*;
5
public aspect DeclareAnnotationTest {
7
declare @type : a.b.c..* : @MyAnnotation("ady 1");
9
declare @method : * *(String) : @MyAnnotation("ady 2");
11
declare @field : java.io.Serializable+ * : @MyClassRetentionAnnotation("ady 3");
13
declare @constructor : new(String,..) : @MyAnnotation;
15
public static void main(String[] args) throws ClassNotFoundException {
16
AjType<DeclareAnnotationTest> myType = AjTypeSystem.getAjType(DeclareAnnotationTest.class);
17
DeclareAnnotation[] decAs = myType.getDeclareAnnotations();
18
if (decAs.length != 4) throw new RuntimeException("Expecting 4 members, got " + decAs.length);
21
int atMethodIndex = -1;
22
int atFieldIndex = -1;
23
int atConstructorIndex = -1;
26
for (DeclareAnnotation da : decAs) {
28
switch (da.getKind()) {
30
atFieldIndex = index++;
33
atMethodIndex = index++;
36
atConstructorIndex = index++;
39
atTypeIndex = index++;
41
default: throw new RuntimeException ("unknown type");
46
checkAtType(decAs[atTypeIndex]);
47
checkAtMethod(decAs[atMethodIndex]);
48
checkAtField(decAs[atFieldIndex]);
49
checkAtConstructor(decAs[atConstructorIndex]);
53
private static void checkAtType(DeclareAnnotation da) {
54
if (da.getKind() != DeclareAnnotation.Kind.Type) throw new RuntimeException("expecting @type");
55
if (da.getSignaturePattern() != null) throw new RuntimeException("not expecting a signature pattern");
56
if (!da.getTypePattern().asString().equals("a.b.c..*")) throw new RuntimeException("expecting 'a.b.c..*' but got '" + da.getTypePattern().asString() + "'");
57
if (da.getDeclaringType().getJavaClass() != DeclareAnnotationTest.class) throw new RuntimeException("bad declaring type: " + da.getDeclaringType());
58
MyAnnotation ma = (MyAnnotation) da.getAnnotation();
59
if (!ma.value().equals("ady 1")) throw new RuntimeException("bad value: " + ma.value());
60
if (!da.getAnnotationAsText().equals("@MyAnnotation(\"ady 1\")")) throw new RuntimeException("bad annotation text: " + da.getAnnotationAsText());
63
private static void checkAtMethod(DeclareAnnotation da) {
64
if (da.getKind() != DeclareAnnotation.Kind.Method) throw new RuntimeException("expecting @method");
65
if (da.getTypePattern() != null) throw new RuntimeException("not expecting a type pattern");
66
if (!da.getSignaturePattern().asString().equals("* *(java.lang.String)")) throw new RuntimeException("expecting '* *(java.lang.String)' but got '" + da.getSignaturePattern().asString() + "'");
67
if (da.getDeclaringType().getJavaClass() != DeclareAnnotationTest.class) throw new RuntimeException("bad declaring type: " + da.getDeclaringType());
68
MyAnnotation ma = (MyAnnotation) da.getAnnotation();
69
if (!ma.value().equals("ady 2")) throw new RuntimeException("bad value: " + ma.value());
70
if (!da.getAnnotationAsText().equals("@MyAnnotation(\"ady 2\")")) throw new RuntimeException("bad annotation text: " + da.getAnnotationAsText());
73
private static void checkAtField(DeclareAnnotation da) {
74
if (da.getKind() != DeclareAnnotation.Kind.Field) throw new RuntimeException("expecting @field");
75
if (da.getTypePattern() != null) throw new RuntimeException("not expecting a type pattern");
76
if (!da.getSignaturePattern().asString().equals("java.io.Serializable+ *")) throw new RuntimeException("expecting 'java.io.Serializable+ *' but got '" + da.getSignaturePattern().asString() + "'");
77
if (da.getDeclaringType().getJavaClass() != DeclareAnnotationTest.class) throw new RuntimeException("bad declaring type: " + da.getDeclaringType());
78
if (da.getAnnotation() != null) throw new RuntimeException("expecting null annotation, but got " + da.getAnnotation());
79
if (!da.getAnnotationAsText().equals("@MyClassRetentionAnnotation(\"ady 3\")")) throw new RuntimeException("bad annotation text: " + da.getAnnotationAsText());
82
private static void checkAtConstructor(DeclareAnnotation da) {
83
if (da.getKind() != DeclareAnnotation.Kind.Constructor) throw new RuntimeException("expecting @constructor");
84
if (da.getTypePattern() != null) throw new RuntimeException("not expecting a type pattern");
85
if (!da.getSignaturePattern().asString().equals("new(java.lang.String, ..)")) throw new RuntimeException("expecting 'new(java.lang.String,..)' but got '" + da.getSignaturePattern().asString() + "'");
86
if (da.getDeclaringType().getJavaClass() != DeclareAnnotationTest.class) throw new RuntimeException("bad declaring type: " + da.getDeclaringType());
87
MyAnnotation ma = (MyAnnotation) da.getAnnotation();
88
if (!ma.value().equals("some value")) throw new RuntimeException("bad value: " + ma.value());
89
if (!da.getAnnotationAsText().equals("@MyAnnotation")) throw new RuntimeException("bad annotation text: " + da.getAnnotationAsText());
94
@Retention(RetentionPolicy.RUNTIME)
95
@interface MyAnnotation {
97
String value() default "some value";
101
@interface MyClassRetentionAnnotation {