Path: blob/master/test/jdk/java/lang/annotation/Missing/MissingTest.java
41152 views
/*1* Copyright (c) 2005, 2016, 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 6322301 504177826* @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.java MissingDefault.java29* @clean Missing30* @run main MissingTest31*/3233import java.lang.reflect.*;34import java.lang.annotation.*;3536/**37* This test verifies that a missing annotation class leads to the38* expected exceptional behavior; a missing directly applied39* annotation is currently ignored but a missing annotation value40* inside another annotation throws an exception.41*42* To be run as intended, the annotation type Missing should *not* be43* on the classpath when the test is run; with jtreg, it is deleted by44* the @clean directive.45*/46public class MissingTest {47/**48* For the annotated element argument, get all its annotations and49* see whether or not an exception is throw upon reading the50* annotations. Additionally, verify at least one annotation is51* present.52*/53private static void testAnnotation(AnnotatedElement element,54boolean exceptionExpected) {55java.lang.annotation.Annotation[] annotations;56try {57annotations = element.getAnnotations();58if (exceptionExpected) {59System.err.println("Error: Did not get an exception reading annotations on "60+ element);61System.err.println("Annotations found: "62+ java.util.Arrays.toString(annotations));63throw new RuntimeException();64}65if (annotations.length == 0) {66System.err.println("Error: no annotations found on " + element);67throw new RuntimeException();68}69} catch (Throwable t) {70if (!exceptionExpected) {71System.err.println("Error: Got an unexpected exception reading annotations on "72+ element);73throw new RuntimeException(t);74}75}76}7778/**79* For the annotated element argument, get all its annotations and80* see whether or not an exception is throw upon reading the81* annotations. Additionally, verify at least one annotation is82* present.83*/84private static void testParameterAnnotation(Method m,85boolean exceptionExpected) {86java.lang.annotation.Annotation[][] annotationsArray;87try {88annotationsArray = m.getParameterAnnotations();89if (exceptionExpected) {90System.err.println("Error: Did not get an exception reading annotations on method"91+ m);92System.err.println("Annotations found: "93+ java.util.Arrays.toString(annotationsArray));94throw new RuntimeException();95}96if (annotationsArray.length == 0 ) {97System.err.println("Error: no parameters for " + m);98throw new RuntimeException();99} else {100java.lang.annotation.Annotation[] annotations = annotationsArray[0];101if (annotations.length == 0) {102System.err.println("Error: no annotations on " + m);103throw new RuntimeException();104}105}106} catch (Throwable t) {107if (!exceptionExpected) {108System.err.println("Error: Got an unexpected exception reading annotations on "109+ m);110throw new RuntimeException(t);111}112}113}114115private static void testMethodGetDefaultValue(Class<?> clazz) throws Exception{116Method m = clazz.getMethod("value", (Class<?>[])null);117118try {119System.out.println(m.getDefaultValue());120throw new RuntimeException("Expected exception not thrown");121} catch (TypeNotPresentException tnpe) {122; // Expected123} catch (AnnotationFormatError afe) {124throw new RuntimeException(afe);125}126}127128public static void main(String... args) throws Exception {129// Class A has a directly applied annotation whose class is130// missing.131testAnnotation(A.class, false);132133// Class B has a directly applied annotation whose value134// includes to an annotation class that is missing.135testAnnotation(B.class, true);136137138// Class C has a directly applied parameter annotation whose139// class is missing.140testParameterAnnotation(C.class.getDeclaredMethod("method1", Object.class),141false);142143// Class D has a directly applied parameter annotation whose value144// includes to an annotation class that is missing.145testParameterAnnotation(D.class.getDeclaredMethod("method1", Object.class),146true);147// The MissingDefault annotation type has a default value of the Missing class.148testMethodGetDefaultValue(MissingDefault.class);149}150}151152153