// Print associated annotations: // * A is directly present or repeatably present on an element E; // * No annotation of A is directly/repeatably present on an element // AND E is a class AND A's type is inheritable, AND A is associated with its superclass. // (Looks through subtypes recursively only if there's 0 result at each level, // and the annotation is @Inheritable).
printAnnotationsByType(Calendar.class, SingleUser.class);
printAnnotationsByType(Calendars.class, SingleUser.class);
// NOTE: // Order of outer-most annotations Calendars[C,C],S vs C,Calendars[C,C] is unspecified. // In particular it's the order of #getDeclaredAnnotations which is completely unmentioned. // The only requirement for #getAnnotationsByType is to have same ordering as // #getDeclaredAnnotations. // (Calendars[] itself has to maintain value() order).
printAnnotationsByType(Calendar.class, UserComplex.class); // Cs(C,C),C collapses into C,C,C.
printAnnotationsByType(Calendars.class, UserComplex.class);
printAnnotationsByType(Calendar.class, UserSub2.class); // The directly present "Calendar" annotation masks all the repeatably present // "Calendar" annotations coming from User.
printAnnotationsByType(Calendars.class, UserSub2.class); // Edge case: UserSub2 doesn't directly have a Calendars annotation, // so it doesn't mask the "User" Calendars annotation.
// Print directly present annotations: // // The element E has an annotation_item for it (accessible through an // annotations_directory_item) corresponding to an annotation A, // and A's type_idx must match that on the encoded_annotation (from the annotation_item). // (Does not look through the subtypes recursively)
printDeclaredAnnotation(SingleUser.class, Calendar.class);
printDeclaredAnnotation(SingleUser.class, Calendars.class);
publicstaticvoid testDeclaredAnnotationsByType() {
System.out.println("==============================");
System.out.println("Declared class annotations by type:");
System.out.println("==============================");
// A is directly present or repeatably present on an element E; // -- (does not do any recursion for classes regardless of @Inherited)
printDeclaredAnnotationsByType(Calendar.class, SingleUser.class);
printDeclaredAnnotationsByType(Calendars.class, SingleUser.class);
printDeclaredAnnotationsByType(Calendar.class, UserSub2.class); // The directly present "Calendar" annotation masks all the repeatably present "Calendar" // annotations coming from User.
printDeclaredAnnotationsByType(Calendars.class, UserSub2.class); // Edge case: UserSub2 doesn't directly have a Calendars annotation, // so it doesn't mask the "User" Calendars annotation.
// Print the annotation "annotationClass" that is associated with an element denoted by // "annotationUseClass." privatestatic <A extends Annotation> void printAnnotationsByType(Class<A> annotationClass, Class<?> annotationUseClass) {
A[] annotationsByType = annotationUseClass.getAnnotationsByType(annotationClass);
String msg = "Annotations by type, defined by class "
+ annotationUseClass.getName() + " with annotation " + annotationClass.getName() + ": "
+ asString(annotationsByType);
System.out.println(msg);
}
privatestatic <A extends Annotation> void printDeclaredAnnotation(Class<?> annotationUseClass, Class<A> annotationDefClass) {
A anno = annotationUseClass.getDeclaredAnnotation(annotationDefClass);
String msg = asString(anno);
System.out.println("Declared annotations by class " + annotationUseClass
+ ", annotation " + annotationDefClass + ": " + msg);
}
// Print the annotation "annotationClass" that is directly/indirectly present with an element // denoted by "annotationUseClass." privatestatic <A extends Annotation> void printDeclaredAnnotationsByType( Class<A> annotationClass, Class<?> annotationUseClass) {
A[] annotationsByType = annotationUseClass.getDeclaredAnnotationsByType(annotationClass);
String msg = "Declared annnotations by type, defined by class " + annotationUseClass.getName()
+ " with annotation " + annotationClass.getName() + ": "
+ asString(annotationsByType);
System.out.println(msg);
}
publicstaticvoid testMethodAnnotationsByType() {
System.out.println("==============================");
System.out.println("Method annotations by type:");
System.out.println("==============================");
// Print associated annotations: // * A is directly present or repeatably present on an element E; // * No annotation of A is directly/repeatably present on an element AND E is a class // AND A's type is inheritable, AND A is associated with its superclass. // (Looks through subtypes recursively only if there's 0 result at each level, // and the annotation is @Inheritable).
printMethodAnnotationsByType(Calendar.class, "singleUser", AnnotationTestFixture.class);
printMethodAnnotationsByType(Calendars.class, "singleUser", AnnotationTestFixture.class);
// Print the annotation "annotationClass" that is associated with an element denoted by // methodName in annotationUseClass. privatestatic <A extends Annotation> void printMethodDeclaredAnnotation(Class<A> annotationClass,
String methodName, Class<?> annotationUseClass) {
Method m = null; try {
m = annotationUseClass.getDeclaredMethod(methodName);
} catch (Throwable t) { thrownew AssertionError(t);
}
Annotation annotationsByType = m.getDeclaredAnnotation(annotationClass);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.