1 /*
   2  * Copyright (c) 2005, 2016, 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 5041778
  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 MissingDefault.java
  30  * @clean Missing
  31  * @run main MissingTest
  32  */
  33 
  34 import java.lang.reflect.*;
  35 import java.lang.annotation.*;
  36 
  37 /**
  38  * This test verifies that a missing annotation class leads to the
  39  * expected exceptional behavior; a missing directly applied
  40  * annotation is currently ignored but a missing annotation value
  41  * inside another annotation throws an exception.
  42  *
  43  * To be run as intended, the annotation type Missing should *not* be
  44  * on the classpath when the test is run; with jtreg, it is deleted by
  45  * the @clean directive.
  46  */
  47 public class MissingTest {
  48     /**
  49      * For the annotated element argument, get all its annotations and
  50      * see whether or not an exception is throw upon reading the
  51      * annotations.  Additionally, verify at least one annotation is
  52      * present.
  53      */
  54     private static void testAnnotation(AnnotatedElement element,
  55                                 boolean exceptionExpected) {
  56         java.lang.annotation.Annotation[] annotations;
  57         try {
  58             annotations = element.getAnnotations();
  59             if (exceptionExpected) {
  60                 System.err.println("Error: Did not get an exception reading annotations on "
  61                                    + element);
  62                 System.err.println("Annotations found: "
  63                                    + java.util.Arrays.toString(annotations));
  64                 throw new RuntimeException();
  65             }
  66             if (annotations.length == 0) {
  67                 System.err.println("Error: no annotations found on " + element);
  68                 throw new RuntimeException();
  69             }
  70         } catch (Throwable t) {
  71             if (!exceptionExpected) {
  72                 System.err.println("Error: Got an unexpected exception reading annotations on "
  73                                    + element);
  74                 throw new RuntimeException(t);
  75             }
  76         }
  77     }
  78 
  79     /**
  80      * For the annotated element argument, get all its annotations and
  81      * see whether or not an exception is throw upon reading the
  82      * annotations.  Additionally, verify at least one annotation is
  83      * present.
  84      */
  85     private static void testParameterAnnotation(Method m,
  86                                                 boolean exceptionExpected) {
  87         java.lang.annotation.Annotation[][] annotationsArray;
  88         try {
  89             annotationsArray = m.getParameterAnnotations();
  90             if (exceptionExpected) {
  91                 System.err.println("Error: Did not get an exception reading annotations on method"
  92                                    + m);
  93                 System.err.println("Annotations found: "
  94                                    + java.util.Arrays.toString(annotationsArray));
  95                 throw new RuntimeException();
  96             }
  97             if (annotationsArray.length == 0 ) {
  98                 System.err.println("Error: no parameters for " + m);
  99                 throw new RuntimeException();
 100             } else {
 101                 java.lang.annotation.Annotation[] annotations = annotationsArray[0];
 102                 if (annotations.length == 0) {
 103                     System.err.println("Error: no annotations on " + m);
 104                     throw new RuntimeException();
 105                 }
 106             }
 107         } catch (Throwable t) {
 108             if (!exceptionExpected) {
 109                 System.err.println("Error: Got an unexpected exception reading annotations on "
 110                                    + m);
 111                 throw new RuntimeException(t);
 112             }
 113         }
 114     }
 115 
 116     private static void testMethodGetDefaultValue(Class<?> clazz) throws Exception{
 117         Method m = clazz.getMethod("value", (Class<?>[])null);
 118 
 119         try {
 120             System.out.println(m.getDefaultValue());
 121             throw new RuntimeException("Expected exception not thrown");
 122         } catch (TypeNotPresentException tnpe) {
 123             ; // Expected
 124         } catch (AnnotationFormatError afe) {
 125             throw new RuntimeException(afe);
 126         }
 127     }
 128 
 129     public static void main(String... args) throws Exception {
 130         // Class A has a directly applied annotation whose class is
 131         // missing.
 132         testAnnotation(A.class, false);
 133 
 134         // Class B has a directly applied annotation whose value
 135         // includes to an annotation class that is missing.
 136         testAnnotation(B.class, true);
 137 
 138 
 139         // Class C has a directly applied parameter annotation whose
 140         // class is missing.
 141         testParameterAnnotation(C.class.getDeclaredMethod("method1", Object.class),
 142                                 false);
 143 
 144         // Class D has a directly applied parameter annotation whose value
 145         // includes to an annotation class that is missing.
 146         testParameterAnnotation(D.class.getDeclaredMethod("method1", Object.class),
 147                                 true);
 148         // The MissingDefault annotation type has a default value of the Missing class.
 149         testMethodGetDefaultValue(MissingDefault.class);
 150     }
 151 }