5040830: (ann) please improve toString() for annotations containing exception proxies
authordarcy
Wed, 01 Jun 2016 11:16:58 -0700
changeset 38770 e8746fa36f1a
parent 38769 8046caf79f1c
child 38771 fdbc4de9dfc9
5040830: (ann) please improve toString() for annotations containing exception proxies Reviewed-by: psandoz
jdk/src/java.base/share/classes/sun/reflect/annotation/AnnotationInvocationHandler.java
jdk/src/java.base/share/classes/sun/reflect/annotation/AnnotationTypeMismatchExceptionProxy.java
jdk/src/java.base/share/classes/sun/reflect/annotation/EnumConstantNotPresentExceptionProxy.java
jdk/src/java.base/share/classes/sun/reflect/annotation/TypeNotPresentExceptionProxy.java
--- a/jdk/src/java.base/share/classes/sun/reflect/annotation/AnnotationInvocationHandler.java	Wed Jun 01 09:52:08 2016 -0700
+++ b/jdk/src/java.base/share/classes/sun/reflect/annotation/AnnotationInvocationHandler.java	Wed Jun 01 11:16:58 2016 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2003, 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,7 @@
 import java.lang.reflect.*;
 import java.io.Serializable;
 import java.util.*;
+import java.util.stream.Collectors;
 import java.security.AccessController;
 import java.security.PrivilegedAction;
 
@@ -158,13 +159,17 @@
     }
 
     /**
-     * Translates a member value (in "dynamic proxy return form") into a string
+     * Translates a member value (in "dynamic proxy return form") into a string.
      */
     private static String memberValueToString(Object value) {
         Class<?> type = value.getClass();
-        if (!type.isArray())    // primitive, string, class, enum const,
-                                // or annotation
-            return value.toString();
+        if (!type.isArray()) {   // primitive, string, class, enum const,
+                                 // or annotation
+            if (type == Class.class)
+                return classValueToString((Class<?>) value);
+            else
+                return value.toString();
+        }
 
         if (type == byte[].class)
             return Arrays.toString((byte[]) value);
@@ -182,10 +187,26 @@
             return Arrays.toString((short[]) value);
         if (type == boolean[].class)
             return Arrays.toString((boolean[]) value);
+        if (type == Class[].class)
+            return classArrayValueToString((Class<?>[])value);
         return Arrays.toString((Object[]) value);
     }
 
     /**
+     * Translates a Class value to a form suitable for use in the
+     * string representation of an annotation.
+     */
+    private static String classValueToString(Class<?> clazz) {
+        return clazz.getName() + ".class" ;
+    }
+
+    private static String classArrayValueToString(Class<?>[] classes) {
+        return Arrays.stream(classes)
+            .map(AnnotationInvocationHandler::classValueToString)
+            .collect(Collectors.joining(", ", "{", "}"));
+    }
+
+    /**
      * Implementation of dynamicProxy.equals(Object o)
      */
     private Boolean equalsImpl(Object o) {
--- a/jdk/src/java.base/share/classes/sun/reflect/annotation/AnnotationTypeMismatchExceptionProxy.java	Wed Jun 01 09:52:08 2016 -0700
+++ b/jdk/src/java.base/share/classes/sun/reflect/annotation/AnnotationTypeMismatchExceptionProxy.java	Wed Jun 01 11:16:58 2016 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2004, 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
@@ -56,4 +56,9 @@
     protected RuntimeException generateException() {
         return new AnnotationTypeMismatchException(member, foundType);
     }
+
+    @Override
+    public String toString() {
+        return "/* Warning type mismatch! \"" + foundType + "\" */" ;
+    }
 }
--- a/jdk/src/java.base/share/classes/sun/reflect/annotation/EnumConstantNotPresentExceptionProxy.java	Wed Jun 01 09:52:08 2016 -0700
+++ b/jdk/src/java.base/share/classes/sun/reflect/annotation/EnumConstantNotPresentExceptionProxy.java	Wed Jun 01 11:16:58 2016 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2004, 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
@@ -45,4 +45,9 @@
     protected RuntimeException generateException() {
         return new EnumConstantNotPresentException(enumType, constName);
     }
+
+    @Override
+    public String toString() {
+        return constName + " /* Warning: constant not present! */";
+    }
 }
--- a/jdk/src/java.base/share/classes/sun/reflect/annotation/TypeNotPresentExceptionProxy.java	Wed Jun 01 09:52:08 2016 -0700
+++ b/jdk/src/java.base/share/classes/sun/reflect/annotation/TypeNotPresentExceptionProxy.java	Wed Jun 01 11:16:58 2016 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2004, 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
@@ -45,4 +45,9 @@
     protected RuntimeException generateException() {
         return new TypeNotPresentException(typeName, cause);
     }
+
+    @Override
+    public String toString() {
+        return typeName + ".class /* Warning: type not present! */";
+    }
 }