Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/annotation/Missing/MissingTest.java
38828 views
/*1* Copyright (c) 2005, 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 632230126* @summary Verify when missing annotation classes cause exceptions27* @author Joseph D. Darcy28* @compile MissingTest.java A.java B.java C.java D.java Marker.java Missing.java MissingWrapper.java29* @clean Missing30* @run main MissingTest31*/3233import java.lang.reflect.*;3435/**36* This test verifies that a missing annotation class leads to the37* expected exceptional behavior; a missing directly applied38* annotation is currently ignored but a missing annotation value39* inside another annotation throws an exception.40*41* To be run as intended, the annotation type Missing should *not* be42* on the classpath when the test is run; with jtreg, it is deleted by43* the @clean directive.44*/45public class MissingTest {46/**47* For the annotated element argument, get all its annotations and48* see whether or not an exception is throw upon reading the49* annotations. Additionally, verify at least one annotation is50* present.51*/52private static void testAnnotation(AnnotatedElement element,53boolean exceptionExpected) {54java.lang.annotation.Annotation[] annotations;55try {56annotations = element.getAnnotations();57if (exceptionExpected) {58System.err.println("Error: Did not get an exception reading annotations on "59+ element);60System.err.println("Annotations found: "61+ java.util.Arrays.toString(annotations));62throw new RuntimeException();63}64if (annotations.length == 0) {65System.err.println("Error: no annotations found on " + element);66throw new RuntimeException();67}68} catch (Throwable t) {69if (!exceptionExpected) {70System.err.println("Error: Got an unexpected exception reading annotations on "71+ element);72throw new RuntimeException(t);73}74}75}7677/**78* For the annotated element argument, get all its annotations and79* see whether or not an exception is throw upon reading the80* annotations. Additionally, verify at least one annotation is81* present.82*/83private static void testParameterAnnotation(Method m,84boolean exceptionExpected) {85java.lang.annotation.Annotation[][] annotationsArray;86try {87annotationsArray = m.getParameterAnnotations();88if (exceptionExpected) {89System.err.println("Error: Did not get an exception reading annotations on method"90+ m);91System.err.println("Annotations found: "92+ java.util.Arrays.toString(annotationsArray));93throw new RuntimeException();94}95if (annotationsArray.length == 0 ) {96System.err.println("Error: no parameters for " + m);97throw new RuntimeException();98} else {99java.lang.annotation.Annotation[] annotations = annotationsArray[0];100if (annotations.length == 0) {101System.err.println("Error: no annotations on " + m);102throw new RuntimeException();103}104}105} catch (Throwable t) {106if (!exceptionExpected) {107System.err.println("Error: Got an unexpected exception reading annotations on "108+ m);109throw new RuntimeException(t);110}111}112}113114public static void main(String argv[]) throws Exception {115// Class A has a directly applied annotation whose class is116// missing.117testAnnotation(A.class, false);118119// Class B has a directly applied annotation whose value120// includes to an annotation class that is missing.121testAnnotation(B.class, true);122123124// Class C has a directly applied parameter annotation whose125// class is missing.126testParameterAnnotation(C.class.getDeclaredMethod("method1", Object.class),127false);128129// Class D has a directly applied parameter annotation whose value130// includes to an annotation class that is missing.131testParameterAnnotation(D.class.getDeclaredMethod("method1", Object.class),132true);133}134}135136137