1 /* 2 * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 6322301 27 * @summary Verify when missing annotation classes cause exceptions 28 * @author Joseph D. Darcy 29 * @compile MissingTest.java A.java B.java C.java D.java Marker.java Missing.java MissingWrapper.java 30 * @clean Missing 31 * @run main MissingTest 32 */ 33 34 import java.lang.reflect.*; 35 36 /** 37 * This test verifies that a missing annotation class leads to the 38 * expected exceptional behavior; a missing directly applied 39 * annotation is currently ignored but a missing annotation value 40 * inside another annotation throws an exception. 41 * 42 * To be run as intended, the annotation type Missing should *not* be 43 * on the classpath when the test is run; with jtreg, it is deleted by 44 * the @clean directive. 45 */ 46 public class MissingTest { 47 /** 48 * For the annotated element argument, get all its annotations and 49 * see whether or not an exception is throw upon reading the 50 * annotations. Additionally, verify at least one annotation is 51 * present. 52 */ 53 private static void testAnnotation(AnnotatedElement element, 54 boolean exceptionExpected) { 55 java.lang.annotation.Annotation[] annotations; 56 try { 57 annotations = element.getAnnotations(); 58 if (exceptionExpected) { 59 System.err.println("Error: Did not get an exception reading annotations on " 60 + element); 61 System.err.println("Annotations found: " 62 + java.util.Arrays.toString(annotations)); 63 throw new RuntimeException(); 64 } 65 if (annotations.length == 0) { 66 System.err.println("Error: no annotations found on " + element); 67 throw new RuntimeException(); 68 } 69 } catch (Throwable t) { 70 if (!exceptionExpected) { 71 System.err.println("Error: Got an unexpected exception reading annotations on " 72 + element); 73 throw new RuntimeException(t); 74 } 75 } 76 } 77 78 /** 79 * For the annotated element argument, get all its annotations and 80 * see whether or not an exception is throw upon reading the 81 * annotations. Additionally, verify at least one annotation is 82 * present. 83 */ 84 private static void testParameterAnnotation(Method m, 85 boolean exceptionExpected) { 86 java.lang.annotation.Annotation[][] annotationsArray; 87 try { 88 annotationsArray = m.getParameterAnnotations(); 89 if (exceptionExpected) { 90 System.err.println("Error: Did not get an exception reading annotations on method" 91 + m); 92 System.err.println("Annotations found: " 93 + java.util.Arrays.toString(annotationsArray)); 94 throw new RuntimeException(); 95 } 96 if (annotationsArray.length == 0 ) { 97 System.err.println("Error: no parameters for " + m); 98 throw new RuntimeException(); 99 } else { 100 java.lang.annotation.Annotation[] annotations = annotationsArray[0]; 101 if (annotations.length == 0) { 102 System.err.println("Error: no annotations on " + m); 103 throw new RuntimeException(); 104 } 105 } 106 } catch (Throwable t) { 107 if (!exceptionExpected) { 108 System.err.println("Error: Got an unexpected exception reading annotations on " 109 + m); 110 throw new RuntimeException(t); 111 } 112 } 113 } 114 115 public static void main(String argv[]) throws Exception { 116 // Class A has a directly applied annotation whose class is 117 // missing. 118 testAnnotation(A.class, false); 119 120 // Class B has a directly applied annotation whose value 121 // includes to an annotation class that is missing. 122 testAnnotation(B.class, true); 123 124 125 // Class C has a directly applied parameter annotation whose 126 // class is missing. 127 testParameterAnnotation(C.class.getDeclaredMethod("method1", Object.class), 128 false); 129 130 // Class D has a directly applied parameter annotation whose value 131 // includes to an annotation class that is missing. 132 testParameterAnnotation(D.class.getDeclaredMethod("method1", Object.class), 133 true); 134 } 135 }