Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/reflect/AnnotatedElement/TestAnnotatedElementDefaults.java
38828 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 800529426* @summary Check behavior of default methods of AnnotatedElement27* @author Joseph D. Darcy28*/2930import java.lang.annotation.*;31import java.lang.reflect.*;32import java.util.*;3334/**35* For annotation type tokens including, null, DirectlyPresent.class,36* IndirectlyPresent.class, etc. the behavior of37* AnnotedElementDelegate.foo(arg) is compared for equality to38* baseAnnotatedElement.foo(arg) on various kinds of annotated39* elements.40*/41public class TestAnnotatedElementDefaults {42public static void main(String... args) throws SecurityException {43int failures = 0;4445for (AnnotatedElement annotElement : elementsToTest()) {46System.out.println(annotElement);47AnnotatedElementDelegate delegate = new AnnotatedElementDelegate(annotElement);48failures += testNullHandling(delegate);49for (Class<? extends Annotation> annotType : annotationsToTest()) {50failures += AnnotatedElementDelegate.testDelegate(delegate, annotType);51}52}5354if (failures > 0) {55System.err.printf("%d failures%n", failures);56throw new RuntimeException();57}58}5960private static List<AnnotatedElement> elementsToTest() {61List<AnnotatedElement> annotatedElements = new ArrayList<>();62annotatedElements.add(TestClass1Super.class);63annotatedElements.add(TestClass1.class);64for (Method method : TestClass1.class.getDeclaredMethods()) {65annotatedElements.add(method);66}67return annotatedElements;68}6970private static List<Class<? extends Annotation>> annotationsToTest() {71List<Class<? extends Annotation>> annotations = new ArrayList<>();72annotations.add(Missing.class);7374annotations.add(MissingRepeatable.class);7576annotations.add(DirectlyPresent.class);7778annotations.add(IndirectlyPresent.class);79annotations.add(IndirectlyPresentContainer.class);8081annotations.add(DirectlyAndIndirectlyPresent.class);82annotations.add(DirectlyAndIndirectlyPresentContainer.class);8384annotations.add(AssociatedDirectOnSuperClass.class);85annotations.add(AssociatedDirectOnSuperClassContainer.class);8687annotations.add(AssociatedDirectOnSuperClassIndirectOnSubclass.class);88annotations.add(AssociatedDirectOnSuperClassIndirectOnSubclassContainer.class);8990annotations.add(AssociatedIndirectOnSuperClassDirectOnSubclass.class);91annotations.add(AssociatedIndirectOnSuperClassDirectOnSubclassContainer.class);92return annotations;93}9495private static int testNullHandling(AnnotatedElementDelegate delegate) {96int failures = 0;97try {98Object result = delegate.getDeclaredAnnotationsByType(null);99failures++;100} catch (NullPointerException npe) {101; // Expected102}103104try {105Object result = delegate.getAnnotationsByType(null);106failures++;107} catch (NullPointerException npe) {108; // Expected109}110111try {112Object result = delegate.getDeclaredAnnotation(null);113failures++;114} catch (NullPointerException npe) {115; // Expected116}117118return failures;119}120121}122123// -----------------------------------------------------124125@AssociatedDirectOnSuperClass(123)126@AssociatedIndirectOnSuperClass(234) @AssociatedIndirectOnSuperClass(345)127@AssociatedDirectOnSuperClassIndirectOnSubclass(987)128@AssociatedIndirectOnSuperClassDirectOnSubclass(1111) @AssociatedIndirectOnSuperClassDirectOnSubclass(2222)129class TestClass1Super {}130131@DirectlyPresent(1)132@IndirectlyPresent(10) @IndirectlyPresent(11)133@AssociatedDirectOnSuperClassIndirectOnSubclass(876) @AssociatedDirectOnSuperClassIndirectOnSubclass(765)134@AssociatedIndirectOnSuperClassDirectOnSubclass(3333)135class TestClass1 extends TestClass1Super {136137@DirectlyPresent(2)138@IndirectlyPresentContainer({@IndirectlyPresent(12)})139@DirectlyAndIndirectlyPresentContainer({@DirectlyAndIndirectlyPresent(84), @DirectlyAndIndirectlyPresent(96)})140public void foo() {return ;}141142@IndirectlyPresentContainer({})143@DirectlyAndIndirectlyPresentContainer({@DirectlyAndIndirectlyPresent(11), @DirectlyAndIndirectlyPresent(22)})144@DirectlyAndIndirectlyPresent(33)145public void bar() {return ;}146}147148// -----------------------------------------------------149150@Retention(RetentionPolicy.RUNTIME)151@interface Missing {152int value();153}154155// -----------------------------------------------------156157@Retention(RetentionPolicy.RUNTIME)158@Repeatable(MissingRepeatableContainer.class)159@interface MissingRepeatable {160int value();161}162163@Retention(RetentionPolicy.RUNTIME)164@interface MissingRepeatableContainer {165MissingRepeatable[] value();166}167168// -----------------------------------------------------169170@Retention(RetentionPolicy.RUNTIME)171@interface DirectlyPresent {172int value();173}174175// -----------------------------------------------------176177@Retention(RetentionPolicy.RUNTIME)178@Repeatable(IndirectlyPresentContainer.class)179@interface IndirectlyPresent {180int value();181}182183@Retention(RetentionPolicy.RUNTIME)184@interface IndirectlyPresentContainer {185IndirectlyPresent[] value();186}187188// -----------------------------------------------------189190@Retention(RetentionPolicy.RUNTIME)191@Repeatable(DirectlyAndIndirectlyPresentContainer.class)192@interface DirectlyAndIndirectlyPresent {193int value();194195}196197@Retention(RetentionPolicy.RUNTIME)198@interface DirectlyAndIndirectlyPresentContainer {199DirectlyAndIndirectlyPresent[] value();200}201202// -----------------------------------------------------203204@Retention(RetentionPolicy.RUNTIME)205@Repeatable(AssociatedDirectOnSuperClassContainer.class)206@interface AssociatedDirectOnSuperClass {207int value();208}209210@Retention(RetentionPolicy.RUNTIME)211@interface AssociatedDirectOnSuperClassContainer {212AssociatedDirectOnSuperClass[] value();213}214215// -----------------------------------------------------216217@Retention(RetentionPolicy.RUNTIME)218@Repeatable(AssociatedIndirectOnSuperClassContainer.class)219@interface AssociatedIndirectOnSuperClass {220int value();221}222223@Retention(RetentionPolicy.RUNTIME)224@interface AssociatedIndirectOnSuperClassContainer {225AssociatedIndirectOnSuperClass[] value();226}227228// -----------------------------------------------------229230@Retention(RetentionPolicy.RUNTIME)231@Repeatable(AssociatedDirectOnSuperClassIndirectOnSubclassContainer.class)232@interface AssociatedDirectOnSuperClassIndirectOnSubclass {233int value();234}235236@Retention(RetentionPolicy.RUNTIME)237@interface AssociatedDirectOnSuperClassIndirectOnSubclassContainer {238AssociatedDirectOnSuperClassIndirectOnSubclass[] value();239}240241// -----------------------------------------------------242243@Retention(RetentionPolicy.RUNTIME)244@Repeatable(AssociatedIndirectOnSuperClassDirectOnSubclassContainer.class)245@interface AssociatedIndirectOnSuperClassDirectOnSubclass {246int value();247}248249@Retention(RetentionPolicy.RUNTIME)250@interface AssociatedIndirectOnSuperClassDirectOnSubclassContainer {251AssociatedIndirectOnSuperClassDirectOnSubclass[] value();252}253254// -----------------------------------------------------255256/**257* Helper class to ease calling the default methods of {@code258* AnnotatedElement} and comparing the results to other259* implementation.260*/261class AnnotatedElementDelegate implements AnnotatedElement {262private AnnotatedElement base;263264public AnnotatedElementDelegate(AnnotatedElement base) {265Objects.requireNonNull(base);266this.base = base;267}268269// Delegate to base implemenetation of AnnotatedElement methods270// without defaults.271@Override272public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {273return base.getAnnotation(annotationClass);274}275276@Override277public Annotation[] getAnnotations() {278return base.getAnnotations();279}280281@Override282public Annotation[] getDeclaredAnnotations() {283return base.getDeclaredAnnotations();284}285286public AnnotatedElement getBase() {287return base;288}289290static int testDelegate(AnnotatedElementDelegate delegate,291Class<? extends Annotation> annotationClass) {292int failures = 0;293AnnotatedElement base = delegate.getBase();294295// System.out.println("\tTesting " + delegate + "\ton\t" + annotationClass);296297// <T extends Annotation> T[] getDeclaredAnnotationsByType(Class<T> annotationClass)298failures += annotationArrayCheck(delegate.getDeclaredAnnotationsByType(annotationClass),299base.getDeclaredAnnotationsByType(annotationClass),300annotationClass,301"Equality failure on getDeclaredAnnotationsByType(%s) on %s)%n");302303// <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass)304failures += annotationArrayCheck(delegate.getAnnotationsByType(annotationClass),305base.getAnnotationsByType(annotationClass),306annotationClass,307"Equality failure on getAnnotationsByType(%s) on %s)%n");308309// <T extends Annotation> T getDeclaredAnnotation(Class<T> annotationClass)310if (!Objects.equals(delegate.getDeclaredAnnotation(annotationClass),311base.getDeclaredAnnotation(annotationClass))) {312failures++;313System.err.printf("Equality failure on getDeclaredAnnotation(%s) on %s)%n",314annotationClass, delegate);315}316return failures;317}318private static <T extends Annotation> int annotationArrayCheck(T[] delegate,319T[] base,320Class<? extends Annotation> annotationClass,321String message) {322int failures = 0;323324if (!Objects.deepEquals(delegate,base)) {325failures = 1;326327System.err.printf(message,328annotationClass,329delegate);330331System.err.println("Base result:\t" + Arrays.toString(base));332System.err.println("Delegate result:\t " + Arrays.toString(delegate));333System.err.println();334}335336return failures;337}338}339340341