5041778: (ann) AnnotationFormatError if "default" Class type not found
authordarcy
Sun, 12 Jun 2016 12:14:52 -0700
changeset 38882 efb06b82374b
parent 38881 6c614e2375c8
child 38883 d5de564f8089
5041778: (ann) AnnotationFormatError if "default" Class type not found Reviewed-by: jfranck
jdk/src/java.base/share/classes/java/lang/reflect/Method.java
jdk/src/java.base/share/classes/sun/reflect/annotation/TypeNotPresentExceptionProxy.java
jdk/test/java/lang/annotation/Missing/MissingDefault.java
jdk/test/java/lang/annotation/Missing/MissingTest.java
--- a/jdk/src/java.base/share/classes/java/lang/reflect/Method.java	Sun Jun 12 10:48:19 2016 +0800
+++ b/jdk/src/java.base/share/classes/java/lang/reflect/Method.java	Sun Jun 12 12:14:52 2016 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1996, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -30,6 +30,8 @@
 import jdk.internal.reflect.CallerSensitive;
 import jdk.internal.reflect.MethodAccessor;
 import jdk.internal.reflect.Reflection;
+import sun.reflect.annotation.ExceptionProxy;
+import sun.reflect.annotation.TypeNotPresentExceptionProxy;
 import sun.reflect.generics.repository.MethodRepository;
 import sun.reflect.generics.factory.CoreReflectionFactory;
 import sun.reflect.generics.factory.GenericsFactory;
@@ -641,8 +643,13 @@
             SharedSecrets.getJavaLangAccess().
                 getConstantPool(getDeclaringClass()),
             getDeclaringClass());
-        if (result instanceof sun.reflect.annotation.ExceptionProxy)
+        if (result instanceof ExceptionProxy) {
+            if (result instanceof TypeNotPresentExceptionProxy) {
+                TypeNotPresentExceptionProxy proxy = (TypeNotPresentExceptionProxy)result;
+                throw new TypeNotPresentException(proxy.typeName(), proxy.getCause());
+            }
             throw new AnnotationFormatError("Invalid default: " + this);
+        }
         return result;
     }
 
--- a/jdk/src/java.base/share/classes/sun/reflect/annotation/TypeNotPresentExceptionProxy.java	Sun Jun 12 10:48:19 2016 +0800
+++ b/jdk/src/java.base/share/classes/sun/reflect/annotation/TypeNotPresentExceptionProxy.java	Sun Jun 12 12:14:52 2016 -0700
@@ -46,6 +46,14 @@
         return new TypeNotPresentException(typeName, cause);
     }
 
+    public String typeName() {
+        return typeName;
+    }
+
+    public Throwable getCause() {
+        return cause;
+    }
+
     @Override
     public String toString() {
         return typeName + ".class /* Warning: type not present! */";
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/lang/annotation/Missing/MissingDefault.java	Sun Jun 12 12:14:52 2016 -0700
@@ -0,0 +1,34 @@
+/*
+ * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+import java.lang.annotation.Retention;
+import static java.lang.annotation.RetentionPolicy.*;
+
+/**
+ * Annotation type with a default value whose class will be missing
+ * when MissingTest is run.
+ */
+@Retention(RUNTIME)
+public @interface MissingDefault {
+    Class<?> value() default Missing.class;
+}
--- a/jdk/test/java/lang/annotation/Missing/MissingTest.java	Sun Jun 12 10:48:19 2016 +0800
+++ b/jdk/test/java/lang/annotation/Missing/MissingTest.java	Sun Jun 12 12:14:52 2016 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2005, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -23,15 +23,16 @@
 
 /*
  * @test
- * @bug 6322301
+ * @bug 6322301 5041778
  * @summary Verify when missing annotation classes cause exceptions
  * @author Joseph D. Darcy
- * @compile MissingTest.java A.java B.java C.java D.java Marker.java Missing.java MissingWrapper.java
+ * @compile MissingTest.java A.java B.java C.java D.java Marker.java Missing.java MissingWrapper.java MissingDefault.java
  * @clean Missing
  * @run main MissingTest
  */
 
 import java.lang.reflect.*;
+import java.lang.annotation.*;
 
 /**
  * This test verifies that a missing annotation class leads to the
@@ -112,7 +113,20 @@
         }
     }
 
-    public static void main(String argv[]) throws Exception {
+    private static void testMethodGetDefaultValue(Class<?> clazz) throws Exception{
+        Method m = clazz.getMethod("value", (Class<?>[])null);
+
+        try {
+            System.out.println(m.getDefaultValue());
+            throw new RuntimeException("Expected exception not thrown");
+        } catch (TypeNotPresentException tnpe) {
+            ; // Expected
+        } catch (AnnotationFormatError afe) {
+            throw new RuntimeException(afe);
+        }
+    }
+
+    public static void main(String... args) throws Exception {
         // Class A has a directly applied annotation whose class is
         // missing.
         testAnnotation(A.class, false);
@@ -131,5 +145,7 @@
         // includes to an annotation class that is missing.
         testParameterAnnotation(D.class.getDeclaredMethod("method1", Object.class),
                                 true);
+        // The MissingDefault annotation type has a default value of the Missing class.
+        testMethodGetDefaultValue(MissingDefault.class);
     }
 }