15d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhaopackage android.test.anno; 25d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 35d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhaoimport java.lang.annotation.Annotation; 45d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhaoimport java.lang.reflect.Constructor; 55d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhaoimport java.lang.reflect.Field; 65d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhaoimport java.lang.reflect.Method; 75d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhaoimport java.util.TreeMap; 85d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 95d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhaopublic class TestAnnotations { 105d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao /** 115d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * Print the annotations in sorted order, so as to avoid 125d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao * any (legitimate) non-determinism with regard to the iteration order. 135d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao */ 145d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao static private void printAnnotationArray(String prefix, Annotation[] arr) { 155d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao TreeMap<String, Annotation> sorted = 165d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao new TreeMap<String, Annotation>(); 175d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 185d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao for (Annotation a : arr) { 195d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao sorted.put(a.annotationType().getName(), a); 205d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 215d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 225d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao for (Annotation a : sorted.values()) { 235d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println(prefix + " " + a); 245d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println(prefix + " " + a.annotationType()); 255d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 265d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 275d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 285d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao static void printAnnotations(Class clazz) { 295d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao Annotation[] annos; 305d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao Annotation[][] parAnnos; 315d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 325d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao annos = clazz.getAnnotations(); 335d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println("annotations on TYPE " + clazz + 345d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao "(" + annos.length + "):"); 355d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao printAnnotationArray("", annos); 365d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println(); 375d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 385d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao for (Constructor c: clazz.getDeclaredConstructors()) { 395d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao annos = c.getDeclaredAnnotations(); 405d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println(" annotations on CTOR " + c + ":"); 415d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao printAnnotationArray(" ", annos); 425d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 435d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println(" constructor parameter annotations:"); 445d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao for (Annotation[] pannos: c.getParameterAnnotations()) { 455d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao printAnnotationArray(" ", pannos); 465d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 475d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 485d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 495d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao for (Method m: clazz.getDeclaredMethods()) { 505d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao annos = m.getDeclaredAnnotations(); 515d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println(" annotations on METH " + m + ":"); 525d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao printAnnotationArray(" ", annos); 535d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 545d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println(" method parameter annotations:"); 555d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao for (Annotation[] pannos: m.getParameterAnnotations()) { 565d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao printAnnotationArray(" ", pannos); 575d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 585d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 595d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 605d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao for (Field f: clazz.getDeclaredFields()) { 615d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao annos = f.getDeclaredAnnotations(); 625d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println(" annotations on FIELD " + f + ":"); 635d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao printAnnotationArray(" ", annos); 645d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 655d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao AnnoFancyField aff; 665d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao aff = (AnnoFancyField) f.getAnnotation(AnnoFancyField.class); 675d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao if (aff != null) { 685d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println(" aff: " + aff + " / " + aff.getClass()); 695d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println(" --> nombre is '" + aff.nombre() + "'"); 705d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 715d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 725d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println(); 735d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 745d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 755d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 765d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao @ExportedProperty(mapping = { 775d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao @IntToString(from = 0, to = "NORMAL_FOCUS"), 785d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao @IntToString(from = 2, to = "WEAK_FOCUS") 795d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao }) 805d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao public int getFocusType() { 815d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao return 2; 825d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 835d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 845d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 855d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao @AnnoArrayField 865d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao String thing1; 875d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 885d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao @AnnoArrayField( 895d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao zz = {true,false,true}, 905d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao bb = {-1,0,1}, 915d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao cc = {'Q'}, 925d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao ss = {12,13,14,15,16,17}, 935d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao ii = {1,2,3,4}, 945d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao ff = {1.1f,1.2f,1.3f}, 955d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao jj = {-5,0,5}, 965d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao dd = {0.3,0.6,0.9}, 975d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao str = {"hickory","dickory","dock"} 985d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao ) 995d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao String thing2; 1005d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 1015d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao public static void testArrays() { 1025d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao TestAnnotations ta = new TestAnnotations(); 1035d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao Field field; 1045d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao Annotation[] annotations; 1055d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 1065d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao try { 1075d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao field = TestAnnotations.class.getDeclaredField("thing1"); 1085d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao annotations = field.getAnnotations(); 1095d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println(field + ": " + annotations[0].toString()); 1105d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 1115d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao field = TestAnnotations.class.getDeclaredField("thing2"); 1125d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao annotations = field.getAnnotations(); 1135d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println(field + ": " + annotations[0].toString()); 1145d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } catch (NoSuchFieldException nsfe) { 1155d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao throw new RuntimeException(nsfe); 1165d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 1175d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 1185d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 1195d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao public static void testArrayProblem() { 1205d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao Method meth; 1215d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao ExportedProperty property; 1225d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao final IntToString[] mapping; 1235d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 1245d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao try { 1255d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao meth = TestAnnotations.class.getMethod("getFocusType", 1265d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao (Class[])null); 1275d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } catch (NoSuchMethodException nsme) { 1285d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao throw new RuntimeException(nsme); 1295d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 1305d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao property = meth.getAnnotation(ExportedProperty.class); 1315d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao mapping = property.mapping(); 1325d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 1335d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println("mapping is " + mapping.getClass() + 1345d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao "\n 0='" + mapping[0] + "'\n 1='" + mapping[1] + "'"); 1355d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 1365d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao /* while we're here, check isAnnotationPresent on Method */ 1375d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println("present(getFocusType, ExportedProperty): " + 1385d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao meth.isAnnotationPresent(ExportedProperty.class)); 1395d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println("present(getFocusType, AnnoSimpleType): " + 1405d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao meth.isAnnotationPresent(AnnoSimpleType.class)); 1415d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 1425d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println(""); 1435d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 1445d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 1455d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 1465d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 1475d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao public static void main(String[] args) { 1485d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println("TestAnnotations..."); 1495d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 1505d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao testArrays(); 1515d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao testArrayProblem(); 1525d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao //System.exit(0); 1535d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 1545d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println( 1555d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao "AnnoSimpleField " + AnnoSimpleField.class.isAnnotation() + 1565d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao ", SimplyNoted " + SimplyNoted.class.isAnnotation()); 1575d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 1585d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao Class clazz; 1595d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao clazz = SimplyNoted.class; 1605d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao printAnnotations(clazz); 1615d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao clazz = INoted.class; 1625d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao printAnnotations(clazz); 1635d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao clazz = SubNoted.class; 1645d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao printAnnotations(clazz); 1655d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao clazz = FullyNoted.class; 1665d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao printAnnotations(clazz); 1675d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 1685d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao Annotation anno; 1695d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao 1705d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao // this is expected to be non-null 1715d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao anno = SimplyNoted.class.getAnnotation(AnnoSimpleType.class); 1725d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println("SimplyNoted.get(AnnoSimpleType) = " + anno); 1735d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao // this is non-null if the @Inherited tag is present 1745d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao anno = SubNoted.class.getAnnotation(AnnoSimpleType.class); 1755d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao System.out.println("SubNoted.get(AnnoSimpleType) = " + anno); 176db1e390acea08b4afc280711b4aa9b39f44ecfb0Elliott Hughes 177db1e390acea08b4afc280711b4aa9b39f44ecfb0Elliott Hughes System.out.println(); 178db1e390acea08b4afc280711b4aa9b39f44ecfb0Elliott Hughes 179db1e390acea08b4afc280711b4aa9b39f44ecfb0Elliott Hughes // Package annotations aren't inherited, so getAnnotations and getDeclaredAnnotations are 180db1e390acea08b4afc280711b4aa9b39f44ecfb0Elliott Hughes // the same. 181db1e390acea08b4afc280711b4aa9b39f44ecfb0Elliott Hughes System.out.println("Package annotations:"); 182db1e390acea08b4afc280711b4aa9b39f44ecfb0Elliott Hughes printAnnotationArray(" ", TestAnnotations.class.getPackage().getAnnotations()); 183db1e390acea08b4afc280711b4aa9b39f44ecfb0Elliott Hughes System.out.println("Package declared annotations:"); 184db1e390acea08b4afc280711b4aa9b39f44ecfb0Elliott Hughes printAnnotationArray(" ", TestAnnotations.class.getPackage().getDeclaredAnnotations()); 1855d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao } 1865d1ac920fdaef5d4ec8f66bb734488cd9660b024jeffhao} 187