< prev index next >

test/java/lang/annotation/Missing/MissingTest.java

Print this page


   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) {


  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 }
   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) {


  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 }
< prev index next >