1*795d594fSAndroid Build Coastguard Worker /* 2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2016 The Android Open Source Project 3*795d594fSAndroid Build Coastguard Worker * 4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*795d594fSAndroid Build Coastguard Worker * 8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*795d594fSAndroid Build Coastguard Worker * 10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*795d594fSAndroid Build Coastguard Worker * limitations under the License. 15*795d594fSAndroid Build Coastguard Worker */ 16*795d594fSAndroid Build Coastguard Worker 17*795d594fSAndroid Build Coastguard Worker import java.lang.annotation.Annotation; 18*795d594fSAndroid Build Coastguard Worker import java.lang.reflect.Method; 19*795d594fSAndroid Build Coastguard Worker 20*795d594fSAndroid Build Coastguard Worker public class AnnotationTest extends AnnotationTestHelpers { testAnnotationsByType()21*795d594fSAndroid Build Coastguard Worker public static void testAnnotationsByType() { 22*795d594fSAndroid Build Coastguard Worker System.out.println("=============================="); 23*795d594fSAndroid Build Coastguard Worker System.out.println("Class annotations by type:"); 24*795d594fSAndroid Build Coastguard Worker System.out.println("=============================="); 25*795d594fSAndroid Build Coastguard Worker 26*795d594fSAndroid Build Coastguard Worker // Print associated annotations: 27*795d594fSAndroid Build Coastguard Worker // * A is directly present or repeatably present on an element E; 28*795d594fSAndroid Build Coastguard Worker // * No annotation of A is directly/repeatably present on an element 29*795d594fSAndroid Build Coastguard Worker // AND E is a class AND A's type is inheritable, AND A is associated with its superclass. 30*795d594fSAndroid Build Coastguard Worker // (Looks through subtypes recursively only if there's 0 result at each level, 31*795d594fSAndroid Build Coastguard Worker // and the annotation is @Inheritable). 32*795d594fSAndroid Build Coastguard Worker printAnnotationsByType(Calendar.class, SingleUser.class); 33*795d594fSAndroid Build Coastguard Worker printAnnotationsByType(Calendars.class, SingleUser.class); 34*795d594fSAndroid Build Coastguard Worker 35*795d594fSAndroid Build Coastguard Worker printAnnotationsByType(Calendar.class, User.class); 36*795d594fSAndroid Build Coastguard Worker printAnnotationsByType(Calendars.class, User.class); 37*795d594fSAndroid Build Coastguard Worker 38*795d594fSAndroid Build Coastguard Worker printAnnotationsByType(Calendar.class, User2.class); // Enforce ordering 'z,x,y' 39*795d594fSAndroid Build Coastguard Worker printAnnotationsByType(Calendars.class, User2.class); 40*795d594fSAndroid Build Coastguard Worker 41*795d594fSAndroid Build Coastguard Worker // NOTE: 42*795d594fSAndroid Build Coastguard Worker // Order of outer-most annotations Calendars[C,C],S vs C,Calendars[C,C] is unspecified. 43*795d594fSAndroid Build Coastguard Worker // In particular it's the order of #getDeclaredAnnotations which is completely unmentioned. 44*795d594fSAndroid Build Coastguard Worker // The only requirement for #getAnnotationsByType is to have same ordering as 45*795d594fSAndroid Build Coastguard Worker // #getDeclaredAnnotations. 46*795d594fSAndroid Build Coastguard Worker // (Calendars[] itself has to maintain value() order). 47*795d594fSAndroid Build Coastguard Worker printAnnotationsByType(Calendar.class, UserComplex.class); // Cs(C,C),C collapses into C,C,C. 48*795d594fSAndroid Build Coastguard Worker printAnnotationsByType(Calendars.class, UserComplex.class); 49*795d594fSAndroid Build Coastguard Worker 50*795d594fSAndroid Build Coastguard Worker printAnnotationsByType(Calendar.class, UserSub.class); 51*795d594fSAndroid Build Coastguard Worker printAnnotationsByType(Calendars.class, UserSub.class); 52*795d594fSAndroid Build Coastguard Worker 53*795d594fSAndroid Build Coastguard Worker printAnnotationsByType(Calendar.class, UserSub2.class); 54*795d594fSAndroid Build Coastguard Worker // The directly present "Calendar" annotation masks all the repeatably present 55*795d594fSAndroid Build Coastguard Worker // "Calendar" annotations coming from User. 56*795d594fSAndroid Build Coastguard Worker printAnnotationsByType(Calendars.class, UserSub2.class); 57*795d594fSAndroid Build Coastguard Worker // Edge case: UserSub2 doesn't directly have a Calendars annotation, 58*795d594fSAndroid Build Coastguard Worker // so it doesn't mask the "User" Calendars annotation. 59*795d594fSAndroid Build Coastguard Worker 60*795d594fSAndroid Build Coastguard Worker System.out.println("-----------------------------"); 61*795d594fSAndroid Build Coastguard Worker System.out.println("-----------------------------"); 62*795d594fSAndroid Build Coastguard Worker 63*795d594fSAndroid Build Coastguard Worker } 64*795d594fSAndroid Build Coastguard Worker testDeclaredAnnotation()65*795d594fSAndroid Build Coastguard Worker public static void testDeclaredAnnotation() { 66*795d594fSAndroid Build Coastguard Worker System.out.println("=============================="); 67*795d594fSAndroid Build Coastguard Worker System.out.println("Class declared annotation:"); 68*795d594fSAndroid Build Coastguard Worker System.out.println("=============================="); 69*795d594fSAndroid Build Coastguard Worker 70*795d594fSAndroid Build Coastguard Worker // Print directly present annotations: 71*795d594fSAndroid Build Coastguard Worker // 72*795d594fSAndroid Build Coastguard Worker // The element E has an annotation_item for it (accessible through an 73*795d594fSAndroid Build Coastguard Worker // annotations_directory_item) corresponding to an annotation A, 74*795d594fSAndroid Build Coastguard Worker // and A's type_idx must match that on the encoded_annotation (from the annotation_item). 75*795d594fSAndroid Build Coastguard Worker // (Does not look through the subtypes recursively) 76*795d594fSAndroid Build Coastguard Worker printDeclaredAnnotation(SingleUser.class, Calendar.class); 77*795d594fSAndroid Build Coastguard Worker printDeclaredAnnotation(SingleUser.class, Calendars.class); 78*795d594fSAndroid Build Coastguard Worker 79*795d594fSAndroid Build Coastguard Worker printDeclaredAnnotation(User.class, Calendar.class); 80*795d594fSAndroid Build Coastguard Worker printDeclaredAnnotation(User.class, Calendars.class); 81*795d594fSAndroid Build Coastguard Worker 82*795d594fSAndroid Build Coastguard Worker printDeclaredAnnotation(UserComplex.class, Calendar.class); 83*795d594fSAndroid Build Coastguard Worker printDeclaredAnnotation(UserComplex.class, Calendars.class); 84*795d594fSAndroid Build Coastguard Worker 85*795d594fSAndroid Build Coastguard Worker printDeclaredAnnotation(UserSub.class, Calendar.class); 86*795d594fSAndroid Build Coastguard Worker printDeclaredAnnotation(UserSub.class, Calendars.class); 87*795d594fSAndroid Build Coastguard Worker 88*795d594fSAndroid Build Coastguard Worker printDeclaredAnnotation(UserSub2.class, Calendar.class); 89*795d594fSAndroid Build Coastguard Worker printDeclaredAnnotation(UserSub2.class, Calendars.class); 90*795d594fSAndroid Build Coastguard Worker 91*795d594fSAndroid Build Coastguard Worker System.out.println("-----------------------------"); 92*795d594fSAndroid Build Coastguard Worker System.out.println("-----------------------------"); 93*795d594fSAndroid Build Coastguard Worker } 94*795d594fSAndroid Build Coastguard Worker testDeclaredAnnotationsByType()95*795d594fSAndroid Build Coastguard Worker public static void testDeclaredAnnotationsByType() { 96*795d594fSAndroid Build Coastguard Worker System.out.println("=============================="); 97*795d594fSAndroid Build Coastguard Worker System.out.println("Declared class annotations by type:"); 98*795d594fSAndroid Build Coastguard Worker System.out.println("=============================="); 99*795d594fSAndroid Build Coastguard Worker 100*795d594fSAndroid Build Coastguard Worker // A is directly present or repeatably present on an element E; 101*795d594fSAndroid Build Coastguard Worker // -- (does not do any recursion for classes regardless of @Inherited) 102*795d594fSAndroid Build Coastguard Worker printDeclaredAnnotationsByType(Calendar.class, SingleUser.class); 103*795d594fSAndroid Build Coastguard Worker printDeclaredAnnotationsByType(Calendars.class, SingleUser.class); 104*795d594fSAndroid Build Coastguard Worker 105*795d594fSAndroid Build Coastguard Worker printDeclaredAnnotationsByType(Calendar.class, User.class); 106*795d594fSAndroid Build Coastguard Worker printDeclaredAnnotationsByType(Calendars.class, User.class); 107*795d594fSAndroid Build Coastguard Worker 108*795d594fSAndroid Build Coastguard Worker printDeclaredAnnotationsByType(Calendar.class, User2.class); // Enforce ordering 'z,x,y' 109*795d594fSAndroid Build Coastguard Worker printDeclaredAnnotationsByType(Calendars.class, User2.class); 110*795d594fSAndroid Build Coastguard Worker 111*795d594fSAndroid Build Coastguard Worker printDeclaredAnnotationsByType(Calendar.class, UserComplex.class); 112*795d594fSAndroid Build Coastguard Worker printDeclaredAnnotationsByType(Calendars.class, UserComplex.class); 113*795d594fSAndroid Build Coastguard Worker 114*795d594fSAndroid Build Coastguard Worker printDeclaredAnnotationsByType(Calendar.class, UserSub.class); 115*795d594fSAndroid Build Coastguard Worker printDeclaredAnnotationsByType(Calendars.class, UserSub.class); 116*795d594fSAndroid Build Coastguard Worker 117*795d594fSAndroid Build Coastguard Worker printDeclaredAnnotationsByType(Calendar.class, UserSub2.class); 118*795d594fSAndroid Build Coastguard Worker // The directly present "Calendar" annotation masks all the repeatably present "Calendar" 119*795d594fSAndroid Build Coastguard Worker // annotations coming from User. 120*795d594fSAndroid Build Coastguard Worker printDeclaredAnnotationsByType(Calendars.class, UserSub2.class); 121*795d594fSAndroid Build Coastguard Worker // Edge case: UserSub2 doesn't directly have a Calendars annotation, 122*795d594fSAndroid Build Coastguard Worker // so it doesn't mask the "User" Calendars annotation. 123*795d594fSAndroid Build Coastguard Worker 124*795d594fSAndroid Build Coastguard Worker System.out.println("-----------------------------"); 125*795d594fSAndroid Build Coastguard Worker System.out.println("-----------------------------"); 126*795d594fSAndroid Build Coastguard Worker } 127*795d594fSAndroid Build Coastguard Worker 128*795d594fSAndroid Build Coastguard Worker // Print the annotation "annotationClass" that is associated with an element denoted by 129*795d594fSAndroid Build Coastguard Worker // "annotationUseClass." printAnnotationsByType(Class<A> annotationClass, Class<?> annotationUseClass)130*795d594fSAndroid Build Coastguard Worker private static <A extends Annotation> void printAnnotationsByType(Class<A> annotationClass, 131*795d594fSAndroid Build Coastguard Worker Class<?> annotationUseClass) { 132*795d594fSAndroid Build Coastguard Worker A[] annotationsByType = annotationUseClass.getAnnotationsByType(annotationClass); 133*795d594fSAndroid Build Coastguard Worker 134*795d594fSAndroid Build Coastguard Worker String msg = "Annotations by type, defined by class " 135*795d594fSAndroid Build Coastguard Worker + annotationUseClass.getName() + " with annotation " + annotationClass.getName() + ": " 136*795d594fSAndroid Build Coastguard Worker + asString(annotationsByType); 137*795d594fSAndroid Build Coastguard Worker 138*795d594fSAndroid Build Coastguard Worker 139*795d594fSAndroid Build Coastguard Worker System.out.println(msg); 140*795d594fSAndroid Build Coastguard Worker } 141*795d594fSAndroid Build Coastguard Worker printDeclaredAnnotation(Class<?> annotationUseClass, Class<A> annotationDefClass)142*795d594fSAndroid Build Coastguard Worker private static <A extends Annotation> void printDeclaredAnnotation(Class<?> annotationUseClass, 143*795d594fSAndroid Build Coastguard Worker Class<A> annotationDefClass) { 144*795d594fSAndroid Build Coastguard Worker A anno = annotationUseClass.getDeclaredAnnotation(annotationDefClass); 145*795d594fSAndroid Build Coastguard Worker 146*795d594fSAndroid Build Coastguard Worker String msg = asString(anno); 147*795d594fSAndroid Build Coastguard Worker 148*795d594fSAndroid Build Coastguard Worker System.out.println("Declared annotations by class " + annotationUseClass 149*795d594fSAndroid Build Coastguard Worker + ", annotation " + annotationDefClass + ": " + msg); 150*795d594fSAndroid Build Coastguard Worker } 151*795d594fSAndroid Build Coastguard Worker 152*795d594fSAndroid Build Coastguard Worker // Print the annotation "annotationClass" that is directly/indirectly present with an element 153*795d594fSAndroid Build Coastguard Worker // denoted by "annotationUseClass." printDeclaredAnnotationsByType( Class<A> annotationClass, Class<?> annotationUseClass)154*795d594fSAndroid Build Coastguard Worker private static <A extends Annotation> void printDeclaredAnnotationsByType( 155*795d594fSAndroid Build Coastguard Worker Class<A> annotationClass, Class<?> annotationUseClass) { 156*795d594fSAndroid Build Coastguard Worker A[] annotationsByType = annotationUseClass.getDeclaredAnnotationsByType(annotationClass); 157*795d594fSAndroid Build Coastguard Worker 158*795d594fSAndroid Build Coastguard Worker String msg = "Declared annnotations by type, defined by class " + annotationUseClass.getName() 159*795d594fSAndroid Build Coastguard Worker + " with annotation " + annotationClass.getName() + ": " 160*795d594fSAndroid Build Coastguard Worker + asString(annotationsByType); 161*795d594fSAndroid Build Coastguard Worker 162*795d594fSAndroid Build Coastguard Worker System.out.println(msg); 163*795d594fSAndroid Build Coastguard Worker } 164*795d594fSAndroid Build Coastguard Worker testMethodAnnotationsByType()165*795d594fSAndroid Build Coastguard Worker public static void testMethodAnnotationsByType() { 166*795d594fSAndroid Build Coastguard Worker System.out.println("=============================="); 167*795d594fSAndroid Build Coastguard Worker System.out.println("Method annotations by type:"); 168*795d594fSAndroid Build Coastguard Worker System.out.println("=============================="); 169*795d594fSAndroid Build Coastguard Worker 170*795d594fSAndroid Build Coastguard Worker // Print associated annotations: 171*795d594fSAndroid Build Coastguard Worker // * A is directly present or repeatably present on an element E; 172*795d594fSAndroid Build Coastguard Worker // * No annotation of A is directly/repeatably present on an element AND E is a class 173*795d594fSAndroid Build Coastguard Worker // AND A's type is inheritable, AND A is associated with its superclass. 174*795d594fSAndroid Build Coastguard Worker // (Looks through subtypes recursively only if there's 0 result at each level, 175*795d594fSAndroid Build Coastguard Worker // and the annotation is @Inheritable). 176*795d594fSAndroid Build Coastguard Worker printMethodAnnotationsByType(Calendar.class, "singleUser", AnnotationTestFixture.class); 177*795d594fSAndroid Build Coastguard Worker printMethodAnnotationsByType(Calendars.class, "singleUser", AnnotationTestFixture.class); 178*795d594fSAndroid Build Coastguard Worker 179*795d594fSAndroid Build Coastguard Worker printMethodAnnotationsByType(Calendar.class, "user", AnnotationTestFixture.class); 180*795d594fSAndroid Build Coastguard Worker printMethodAnnotationsByType(Calendars.class, "user", AnnotationTestFixture.class); 181*795d594fSAndroid Build Coastguard Worker 182*795d594fSAndroid Build Coastguard Worker printMethodAnnotationsByType(Calendar.class, "user2", AnnotationTestFixture.class); 183*795d594fSAndroid Build Coastguard Worker printMethodAnnotationsByType(Calendars.class, "user2", AnnotationTestFixture.class); 184*795d594fSAndroid Build Coastguard Worker 185*795d594fSAndroid Build Coastguard Worker printMethodAnnotationsByType(Calendar.class, "userComplex", AnnotationTestFixture.class); 186*795d594fSAndroid Build Coastguard Worker printMethodAnnotationsByType(Calendars.class, "userComplex", AnnotationTestFixture.class); 187*795d594fSAndroid Build Coastguard Worker 188*795d594fSAndroid Build Coastguard Worker System.out.println("-----------------------------"); 189*795d594fSAndroid Build Coastguard Worker System.out.println("-----------------------------"); 190*795d594fSAndroid Build Coastguard Worker } 191*795d594fSAndroid Build Coastguard Worker 192*795d594fSAndroid Build Coastguard Worker // Print the annotation "annotationClass" that is associated with an element denoted by 193*795d594fSAndroid Build Coastguard Worker // "annotationUseClass" method methodName. printMethodAnnotationsByType(Class<A> annotationClass, String methodName, Class<?> annotationUseClass)194*795d594fSAndroid Build Coastguard Worker private static <A extends Annotation> void printMethodAnnotationsByType(Class<A> annotationClass, 195*795d594fSAndroid Build Coastguard Worker String methodName, Class<?> annotationUseClass) { 196*795d594fSAndroid Build Coastguard Worker Method m = null; 197*795d594fSAndroid Build Coastguard Worker try { 198*795d594fSAndroid Build Coastguard Worker m = annotationUseClass.getDeclaredMethod(methodName); 199*795d594fSAndroid Build Coastguard Worker } catch (Throwable t) { 200*795d594fSAndroid Build Coastguard Worker throw new AssertionError(t); 201*795d594fSAndroid Build Coastguard Worker } 202*795d594fSAndroid Build Coastguard Worker A[] annotationsByType = m.getAnnotationsByType(annotationClass); 203*795d594fSAndroid Build Coastguard Worker 204*795d594fSAndroid Build Coastguard Worker String msg = "Annotations by type, defined by method " + m.getName() + " with annotation " + 205*795d594fSAndroid Build Coastguard Worker annotationClass.getName() + ": " + 206*795d594fSAndroid Build Coastguard Worker asString(annotationsByType); 207*795d594fSAndroid Build Coastguard Worker 208*795d594fSAndroid Build Coastguard Worker System.out.println(msg); 209*795d594fSAndroid Build Coastguard Worker } 210*795d594fSAndroid Build Coastguard Worker testMethodDeclaredAnnotations()211*795d594fSAndroid Build Coastguard Worker public static void testMethodDeclaredAnnotations() { 212*795d594fSAndroid Build Coastguard Worker System.out.println("=============================="); 213*795d594fSAndroid Build Coastguard Worker System.out.println("Declared method annotations:"); 214*795d594fSAndroid Build Coastguard Worker System.out.println("=============================="); 215*795d594fSAndroid Build Coastguard Worker 216*795d594fSAndroid Build Coastguard Worker printMethodDeclaredAnnotation(Calendar.class, "singleUser", AnnotationTestFixture.class); 217*795d594fSAndroid Build Coastguard Worker printMethodDeclaredAnnotation(Calendars.class, "singleUser", AnnotationTestFixture.class); 218*795d594fSAndroid Build Coastguard Worker 219*795d594fSAndroid Build Coastguard Worker printMethodDeclaredAnnotation(Calendar.class, "user", AnnotationTestFixture.class); 220*795d594fSAndroid Build Coastguard Worker printMethodDeclaredAnnotation(Calendars.class, "user", AnnotationTestFixture.class); 221*795d594fSAndroid Build Coastguard Worker 222*795d594fSAndroid Build Coastguard Worker printMethodDeclaredAnnotation(Calendar.class, "user2", AnnotationTestFixture.class); 223*795d594fSAndroid Build Coastguard Worker printMethodDeclaredAnnotation(Calendars.class, "user2", AnnotationTestFixture.class); 224*795d594fSAndroid Build Coastguard Worker 225*795d594fSAndroid Build Coastguard Worker printMethodDeclaredAnnotation(Calendar.class, "userComplex", AnnotationTestFixture.class); 226*795d594fSAndroid Build Coastguard Worker printMethodDeclaredAnnotation(Calendars.class, "userComplex", AnnotationTestFixture.class); 227*795d594fSAndroid Build Coastguard Worker 228*795d594fSAndroid Build Coastguard Worker System.out.println("-----------------------------"); 229*795d594fSAndroid Build Coastguard Worker System.out.println("-----------------------------"); 230*795d594fSAndroid Build Coastguard Worker } 231*795d594fSAndroid Build Coastguard Worker 232*795d594fSAndroid Build Coastguard Worker // Print the annotation "annotationClass" that is associated with an element denoted by 233*795d594fSAndroid Build Coastguard Worker // methodName in annotationUseClass. printMethodDeclaredAnnotation(Class<A> annotationClass, String methodName, Class<?> annotationUseClass)234*795d594fSAndroid Build Coastguard Worker private static <A extends Annotation> void printMethodDeclaredAnnotation(Class<A> annotationClass, 235*795d594fSAndroid Build Coastguard Worker String methodName, Class<?> annotationUseClass) { 236*795d594fSAndroid Build Coastguard Worker Method m = null; 237*795d594fSAndroid Build Coastguard Worker try { 238*795d594fSAndroid Build Coastguard Worker m = annotationUseClass.getDeclaredMethod(methodName); 239*795d594fSAndroid Build Coastguard Worker } catch (Throwable t) { 240*795d594fSAndroid Build Coastguard Worker throw new AssertionError(t); 241*795d594fSAndroid Build Coastguard Worker } 242*795d594fSAndroid Build Coastguard Worker Annotation annotationsByType = m.getDeclaredAnnotation(annotationClass); 243*795d594fSAndroid Build Coastguard Worker 244*795d594fSAndroid Build Coastguard Worker String msg = "Annotations declared by method " + m.getName() + " with annotation " 245*795d594fSAndroid Build Coastguard Worker + annotationClass.getName() + ": " 246*795d594fSAndroid Build Coastguard Worker + asString(annotationsByType); 247*795d594fSAndroid Build Coastguard Worker 248*795d594fSAndroid Build Coastguard Worker System.out.println(msg); 249*795d594fSAndroid Build Coastguard Worker } 250*795d594fSAndroid Build Coastguard Worker testMethodDeclaredAnnotationsByType()251*795d594fSAndroid Build Coastguard Worker public static void testMethodDeclaredAnnotationsByType() { 252*795d594fSAndroid Build Coastguard Worker System.out.println("=============================="); 253*795d594fSAndroid Build Coastguard Worker System.out.println("Declared method annotations by type:"); 254*795d594fSAndroid Build Coastguard Worker System.out.println("=============================="); 255*795d594fSAndroid Build Coastguard Worker 256*795d594fSAndroid Build Coastguard Worker printMethodDeclaredAnnotationByType(Calendar.class, "singleUser", AnnotationTestFixture.class); 257*795d594fSAndroid Build Coastguard Worker printMethodDeclaredAnnotationByType(Calendars.class, "singleUser", AnnotationTestFixture.class); 258*795d594fSAndroid Build Coastguard Worker 259*795d594fSAndroid Build Coastguard Worker printMethodDeclaredAnnotationByType(Calendar.class, "user", AnnotationTestFixture.class); 260*795d594fSAndroid Build Coastguard Worker printMethodDeclaredAnnotationByType(Calendars.class, "user", AnnotationTestFixture.class); 261*795d594fSAndroid Build Coastguard Worker 262*795d594fSAndroid Build Coastguard Worker printMethodDeclaredAnnotationByType(Calendar.class, "user2", AnnotationTestFixture.class); 263*795d594fSAndroid Build Coastguard Worker printMethodDeclaredAnnotationByType(Calendars.class, "user2", AnnotationTestFixture.class); 264*795d594fSAndroid Build Coastguard Worker 265*795d594fSAndroid Build Coastguard Worker printMethodDeclaredAnnotationByType(Calendar.class, "userComplex", AnnotationTestFixture.class); 266*795d594fSAndroid Build Coastguard Worker printMethodDeclaredAnnotationByType(Calendars.class, "userComplex", 267*795d594fSAndroid Build Coastguard Worker AnnotationTestFixture.class); 268*795d594fSAndroid Build Coastguard Worker 269*795d594fSAndroid Build Coastguard Worker System.out.println("-----------------------------"); 270*795d594fSAndroid Build Coastguard Worker System.out.println("-----------------------------"); 271*795d594fSAndroid Build Coastguard Worker } 272*795d594fSAndroid Build Coastguard Worker 273*795d594fSAndroid Build Coastguard Worker // Print the annotation "annotationClass" that is associated with an element denoted by 274*795d594fSAndroid Build Coastguard Worker // methodName in annotationUseClass. printMethodDeclaredAnnotationByType( Class<A> annotationClass, String methodName, Class<?> annotationUseClass)275*795d594fSAndroid Build Coastguard Worker private static <A extends Annotation> void printMethodDeclaredAnnotationByType( 276*795d594fSAndroid Build Coastguard Worker Class<A> annotationClass, String methodName, Class<?> annotationUseClass) { 277*795d594fSAndroid Build Coastguard Worker Method m = null; 278*795d594fSAndroid Build Coastguard Worker try { 279*795d594fSAndroid Build Coastguard Worker m = annotationUseClass.getDeclaredMethod(methodName); 280*795d594fSAndroid Build Coastguard Worker } catch (Throwable t) { 281*795d594fSAndroid Build Coastguard Worker throw new AssertionError(t); 282*795d594fSAndroid Build Coastguard Worker } 283*795d594fSAndroid Build Coastguard Worker A[] annotationsByType = m.getDeclaredAnnotationsByType(annotationClass); 284*795d594fSAndroid Build Coastguard Worker 285*795d594fSAndroid Build Coastguard Worker String msg = "Annotations by type, defined by method " + m.getName() + " with annotation " 286*795d594fSAndroid Build Coastguard Worker + annotationClass.getName() + ": " 287*795d594fSAndroid Build Coastguard Worker + asString(annotationsByType); 288*795d594fSAndroid Build Coastguard Worker 289*795d594fSAndroid Build Coastguard Worker System.out.println(msg); 290*795d594fSAndroid Build Coastguard Worker } 291*795d594fSAndroid Build Coastguard Worker } 292