RFR: 8054213: Class name repeated in output of Type.toString()

Sergey Ustimenko merkel05 at gmail.com
Sat Mar 12 15:41:07 UTC 2016


Hi all,

Please review this patch. I'm also looking for sponsor for this one.

The unexpected duplication of owner type was produced by Method.toString()
for static
classes and interfaces. Problem is fixed by slightly changing the way of
obtaining
simple name of static nested type and concatenating it with owner type.


diff --git
a/src/java.base/share/classes/sun/reflect/generics/reflectiveObjects/ParameterizedTypeImpl.java
b/src/java.base/share/classes/sun/reflect/generics/reflectiveObjects/ParameterizedTypeImpl.java
---
a/src/java.base/share/classes/sun/reflect/generics/reflectiveObjects/ParameterizedTypeImpl.java
+++
b/src/java.base/share/classes/sun/reflect/generics/reflectiveObjects/ParameterizedTypeImpl.java
@@ -212,15 +212,14 @@
             else
                 sb.append(ownerType.toString());

-            sb.append(".");
-
-            if (ownerType instanceof ParameterizedTypeImpl) {
+            if (ownerType instanceof ParameterizedTypeImpl)
                 // Find simple name of nested type by removing the
                 // shared prefix with owner.
-                sb.append(rawType.getName().replace(
((ParameterizedTypeImpl)ownerType).rawType.getName() + "$",
-                                         ""));
-            } else
-                sb.append(rawType.getName());
+                sb.append(".").append(
+
 rawType.getName().replace(((ParameterizedTypeImpl)ownerType).rawType.getName()
+ "$", "")
+                );
+            else
+                sb.append("$").append(rawType.getSimpleName());
         } else
             sb.append(rawType.getName());

diff --git a/test/java/lang/reflect/Method/TestReturnTypeDuplication.java
b/test/java/lang/reflect/Method/TestReturnTypeDuplication.java
new file mode 100644
--- /dev/null
+++ b/test/java/lang/reflect/Method/TestReturnTypeDuplication.java
@@ -0,0 +1,72 @@
+/*
+ * 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.
+ */
+
+/*
+ * @test
+ * @bug 8054213
+ * @summary Check that Type.toString() generates valid
+ * output for static classes and interfaces
+ * @author Sergey Ustimenko
+ */
+public class TestReturnTypeDuplication {
+
+    private static final String[] SIGNATURES = {
+            "TestReturnTypeDuplication$Static",
+
 "TestReturnTypeDuplication$StaticParametrised<java.lang.String>",
+            "TestReturnTypeDuplication$Interface",
+
 "TestReturnTypeDuplication$InterfaceParametrised<java.lang.String>"
+    };
+
+
+    public static void main(String[] args) throws Exception {
+        boolean passed =
SIGNATURES[0].equals(getFooGenericReturnType("foo0")) &&
+                SIGNATURES[1].equals(getFooGenericReturnType("foo1")) &&
+                SIGNATURES[2].equals(getFooGenericReturnType("foo2")) &&
+                SIGNATURES[3].equals(getFooGenericReturnType("foo3"));
+
+        if(!passed) {
+            throw new RuntimeException("Generated type names are
invalid!");
+        }
+    }
+
+    private static String getFooGenericReturnType(String foo) throws
Exception {
+        return
TestReturnTypeDuplication.class.getMethod(foo).getGenericReturnType().getTypeName();
+    }
+
+    static class Static {}
+
+    static class StaticParametrised<T> {}
+
+    interface Interface {}
+
+    interface InterfaceParametrised<T> {}
+
+    public TestReturnTypeDuplication.Static foo0(){return null;}
+
+    public TestReturnTypeDuplication.StaticParametrised<String>
foo1(){return null;}
+
+    public TestReturnTypeDuplication.Interface foo2(){return null;}
+
+    public TestReturnTypeDuplication.InterfaceParametrised<String>
foo3(){return null;}
+
+}
-------------- next part --------------
diff --git a/src/java.base/share/classes/sun/reflect/generics/reflectiveObjects/ParameterizedTypeImpl.java b/src/java.base/share/classes/sun/reflect/generics/reflectiveObjects/ParameterizedTypeImpl.java
--- a/src/java.base/share/classes/sun/reflect/generics/reflectiveObjects/ParameterizedTypeImpl.java
+++ b/src/java.base/share/classes/sun/reflect/generics/reflectiveObjects/ParameterizedTypeImpl.java
@@ -212,15 +212,14 @@
             else
                 sb.append(ownerType.toString());
 
-            sb.append(".");
-
-            if (ownerType instanceof ParameterizedTypeImpl) {
+            if (ownerType instanceof ParameterizedTypeImpl)
                 // Find simple name of nested type by removing the
                 // shared prefix with owner.
-                sb.append(rawType.getName().replace( ((ParameterizedTypeImpl)ownerType).rawType.getName() + "$",
-                                         ""));
-            } else
-                sb.append(rawType.getName());
+                sb.append(".").append(
+                        rawType.getName().replace(((ParameterizedTypeImpl)ownerType).rawType.getName() + "$", "")
+                );
+            else
+                sb.append("$").append(rawType.getSimpleName());
         } else
             sb.append(rawType.getName());
 
diff --git a/test/java/lang/reflect/Method/TestReturnTypeDuplication.java b/test/java/lang/reflect/Method/TestReturnTypeDuplication.java
new file mode 100644
--- /dev/null
+++ b/test/java/lang/reflect/Method/TestReturnTypeDuplication.java
@@ -0,0 +1,72 @@
+/*
+ * 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.
+ */
+
+/*
+ * @test
+ * @bug 8054213
+ * @summary Check that Type.toString() generates valid
+ * output for static classes and interfaces
+ * @author Sergey Ustimenko
+ */
+public class TestReturnTypeDuplication {
+
+    private static final String[] SIGNATURES = {
+            "TestReturnTypeDuplication$Static",
+            "TestReturnTypeDuplication$StaticParametrised<java.lang.String>",
+            "TestReturnTypeDuplication$Interface",
+            "TestReturnTypeDuplication$InterfaceParametrised<java.lang.String>"
+    };
+
+
+    public static void main(String[] args) throws Exception {
+        boolean passed = SIGNATURES[0].equals(getFooGenericReturnType("foo0")) &&
+                SIGNATURES[1].equals(getFooGenericReturnType("foo1")) &&
+                SIGNATURES[2].equals(getFooGenericReturnType("foo2")) &&
+                SIGNATURES[3].equals(getFooGenericReturnType("foo3"));
+
+        if(!passed) {
+            throw new RuntimeException("Generated type names are invalid!");
+        }
+    }
+
+    private static String getFooGenericReturnType(String foo) throws Exception {
+        return TestReturnTypeDuplication.class.getMethod(foo).getGenericReturnType().getTypeName();
+    }
+
+    static class Static {}
+
+    static class StaticParametrised<T> {}
+
+    interface Interface {}
+
+    interface InterfaceParametrised<T> {}
+
+    public TestReturnTypeDuplication.Static foo0(){return null;}
+
+    public TestReturnTypeDuplication.StaticParametrised<String> foo1(){return null;}
+
+    public TestReturnTypeDuplication.Interface foo2(){return null;}
+
+    public TestReturnTypeDuplication.InterfaceParametrised<String> foo3(){return null;}
+
+}


More information about the core-libs-dev mailing list