1 package android.test.anno;
2 
3 import java.lang.annotation.Annotation;
4 import java.lang.reflect.Constructor;
5 import java.lang.reflect.Field;
6 import java.lang.reflect.Method;
7 import java.util.TreeMap;
8 
9 public class TestAnnotations {
10     /**
11      * Print the annotations in sorted order, so as to avoid
12      * any (legitimate) non-determinism with regard to the iteration order.
13      */
printAnnotationArray(String prefix, Annotation[] arr)14     static private void printAnnotationArray(String prefix, Annotation[] arr) {
15         TreeMap<String, Annotation> sorted =
16             new TreeMap<String, Annotation>();
17 
18         for (Annotation a : arr) {
19             sorted.put(a.annotationType().getName(), a);
20         }
21 
22         for (Annotation a : sorted.values()) {
23             System.out.println(prefix + "  " + a);
24             System.out.println(prefix + "    " + a.annotationType());
25         }
26     }
27 
printAnnotations(Class clazz)28     static void printAnnotations(Class clazz) {
29         Annotation[] annos;
30         Annotation[][] parAnnos;
31 
32         annos = clazz.getAnnotations();
33         System.out.println("annotations on TYPE " + clazz +
34             "(" + annos.length + "):");
35         printAnnotationArray("", annos);
36         System.out.println();
37 
38         for (Constructor c: clazz.getDeclaredConstructors()) {
39             annos = c.getDeclaredAnnotations();
40             System.out.println("  annotations on CTOR " + c + ":");
41             printAnnotationArray("  ", annos);
42 
43             System.out.println("    constructor parameter annotations:");
44             for (Annotation[] pannos: c.getParameterAnnotations()) {
45                 printAnnotationArray("    ", pannos);
46             }
47         }
48 
49         for (Method m: clazz.getDeclaredMethods()) {
50             annos = m.getDeclaredAnnotations();
51             System.out.println("  annotations on METH " + m + ":");
52             printAnnotationArray("  ", annos);
53 
54             System.out.println("    method parameter annotations:");
55             for (Annotation[] pannos: m.getParameterAnnotations()) {
56                 printAnnotationArray("    ", pannos);
57             }
58         }
59 
60         for (Field f: clazz.getDeclaredFields()) {
61             annos = f.getDeclaredAnnotations();
62             System.out.println("  annotations on FIELD " + f + ":");
63             printAnnotationArray("  ", annos);
64 
65             AnnoFancyField aff;
66             aff = (AnnoFancyField) f.getAnnotation(AnnoFancyField.class);
67             if (aff != null) {
68                 System.out.println("    aff: " + aff + " / " + aff.getClass());
69                 System.out.println("    --> nombre is '" + aff.nombre() + "'");
70             }
71         }
72         System.out.println();
73     }
74 
75 
76     @ExportedProperty(mapping = {
77         @IntToString(from = 0, to = "NORMAL_FOCUS"),
78         @IntToString(from = 2, to = "WEAK_FOCUS")
79     })
getFocusType()80     public int getFocusType() {
81         return 2;
82     }
83 
84 
85     @AnnoArrayField
86     String thing1;
87 
88     @AnnoArrayField(
89             zz = {true,false,true},
90             bb = {-1,0,1},
91             cc = {'Q'},
92             ss = {12,13,14,15,16,17},
93             ii = {1,2,3,4},
94             ff = {1.1f,1.2f,1.3f},
95             jj = {-5,0,5},
96             dd = {0.3,0.6,0.9},
97             str = {"hickory","dickory","dock"}
98             )
99     String thing2;
100 
testArrays()101     public static void testArrays() {
102         TestAnnotations ta = new TestAnnotations();
103         Field field;
104         Annotation[] annotations;
105 
106         try {
107             field = TestAnnotations.class.getDeclaredField("thing1");
108             annotations = field.getAnnotations();
109             System.out.println(field + ": " + annotations[0].toString());
110 
111             field = TestAnnotations.class.getDeclaredField("thing2");
112             annotations = field.getAnnotations();
113             System.out.println(field + ": " + annotations[0].toString());
114         } catch (NoSuchFieldException nsfe) {
115             throw new RuntimeException(nsfe);
116         }
117     }
118 
testArrayProblem()119     public static void testArrayProblem() {
120         Method meth;
121         ExportedProperty property;
122         final IntToString[] mapping;
123 
124         try {
125             meth = TestAnnotations.class.getMethod("getFocusType",
126                     (Class[])null);
127         } catch (NoSuchMethodException nsme) {
128             throw new RuntimeException(nsme);
129         }
130         property = meth.getAnnotation(ExportedProperty.class);
131         mapping = property.mapping();
132 
133         System.out.println("mapping is " + mapping.getClass() +
134             "\n  0='" + mapping[0] + "'\n  1='" + mapping[1] + "'");
135 
136         /* while we're here, check isAnnotationPresent on Method */
137         System.out.println("present(getFocusType, ExportedProperty): " +
138             meth.isAnnotationPresent(ExportedProperty.class));
139         System.out.println("present(getFocusType, AnnoSimpleType): " +
140             meth.isAnnotationPresent(AnnoSimpleType.class));
141 
142         System.out.println("");
143     }
144 
145 
146 
main(String[] args)147     public static void main(String[] args) {
148         System.out.println("TestAnnotations...");
149 
150         testArrays();
151         testArrayProblem();
152 
153         System.out.println(
154             "AnnoSimpleField " + AnnoSimpleField.class.isAnnotation() +
155             ", SimplyNoted " + SimplyNoted.class.isAnnotation());
156 
157         printAnnotations(SimplyNoted.class);
158         printAnnotations(INoted.class);
159         printAnnotations(SubNoted.class);
160         printAnnotations(FullyNoted.class);
161 
162         try {
163             ClassWithInnerAnnotationClass.class.getDeclaredClasses();
164             throw new AssertionError();
165         } catch (NoClassDefFoundError expected) {
166         }
167 
168         // this is expected to be non-null
169         Annotation anno = SimplyNoted.class.getAnnotation(AnnoSimpleType.class);
170         System.out.println("SimplyNoted.get(AnnoSimpleType) = " + anno);
171         // this is non-null if the @Inherited tag is present
172         anno = SubNoted.class.getAnnotation(AnnoSimpleType.class);
173         System.out.println("SubNoted.get(AnnoSimpleType) = " + anno);
174 
175         System.out.println();
176 
177         // Package annotations aren't inherited, so getAnnotations and getDeclaredAnnotations are
178         // the same.
179         System.out.println("Package annotations:");
180         printAnnotationArray("    ", TestAnnotations.class.getPackage().getAnnotations());
181         System.out.println("Package declared annotations:");
182         printAnnotationArray("    ", TestAnnotations.class.getPackage().getDeclaredAnnotations());
183     }
184 }
185