~ubuntu-branches/ubuntu/natty/aspectj/natty

« back to all changes in this revision

Viewing changes to org.aspectj/modules/tests/java5/ataspectj/annotationGen/DeclareAnnotationTest.aj

  • Committer: Bazaar Package Importer
  • Author(s): Damien Raude-Morvan
  • Date: 2009-10-04 16:37:23 UTC
  • mfrom: (1.1.3 upstream) (3.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20091004163723-ck4y7j7fhjxskkie
Tags: 1.6.6+dfsg-1
* New upstream release.
  - Update 02_use_gjdoc.diff patch
* Update my email address

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import org.aspectj.lang.reflect.*;
2
 
import java.lang.reflect.*;
3
 
import java.lang.annotation.*;
4
 
 
5
 
public aspect DeclareAnnotationTest {
6
 
        
7
 
        declare @type : a.b.c..* : @MyAnnotation("ady 1");
8
 
        
9
 
        declare @method : * *(String) : @MyAnnotation("ady 2");
10
 
        
11
 
        declare @field : java.io.Serializable+ * : @MyClassRetentionAnnotation("ady 3");
12
 
        
13
 
        declare @constructor : new(String,..) : @MyAnnotation;
14
 
        
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);
19
 
                
20
 
                int atTypeIndex = -1;
21
 
                int atMethodIndex = -1;
22
 
                int atFieldIndex = -1;
23
 
                int atConstructorIndex = -1;
24
 
                
25
 
                int index = 0;
26
 
                for (DeclareAnnotation da : decAs) {
27
 
                        
28
 
                        switch (da.getKind()) {
29
 
                                case Field:
30
 
                                        atFieldIndex = index++;
31
 
                                        break;
32
 
                                case Method:
33
 
                                        atMethodIndex = index++;
34
 
                                        break;
35
 
                                case Constructor:
36
 
                                        atConstructorIndex = index++;
37
 
                                        break;
38
 
                                case Type:
39
 
                                        atTypeIndex = index++;
40
 
                                        break;
41
 
                                default: throw new RuntimeException ("unknown type");
42
 
                        }
43
 
                }
44
 
                
45
 
                
46
 
                checkAtType(decAs[atTypeIndex]);
47
 
                checkAtMethod(decAs[atMethodIndex]);
48
 
                checkAtField(decAs[atFieldIndex]);
49
 
                checkAtConstructor(decAs[atConstructorIndex]);
50
 
        }
51
 
                
52
 
        
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());
61
 
        }
62
 
 
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());
71
 
        }
72
 
 
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());         
80
 
        }
81
 
 
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());          
90
 
        }
91
 
 
92
 
}
93
 
 
94
 
@Retention(RetentionPolicy.RUNTIME)
95
 
@interface MyAnnotation {
96
 
        
97
 
        String value() default "some value";
98
 
        
99
 
}
100
 
 
101
 
@interface MyClassRetentionAnnotation {
102
 
        String value();
103
 
}