8222151: refactoring: enhancements to java.lang.Class::methodToString and java.lang.Class::getTypeName
authorvromero
Thu, 11 Apr 2019 22:56:11 -0400
changeset 54520 f48312257bc6
parent 54519 a2795025f417
child 54521 8de62c4af8c7
8222151: refactoring: enhancements to java.lang.Class::methodToString and java.lang.Class::getTypeName Reviewed-by: darcy Contributed-by: sergei.tsypanov@yandex.ru
src/java.base/share/classes/java/lang/Class.java
--- a/src/java.base/share/classes/java/lang/Class.java	Thu Apr 11 19:36:00 2019 -0400
+++ b/src/java.base/share/classes/java/lang/Class.java	Thu Apr 11 22:56:11 2019 -0400
@@ -277,8 +277,7 @@
                           .collect(Collectors.joining(",", "<", ">")));
             }
 
-            for (int i = 0; i < arrayDepth; i++)
-                sb.append("[]");
+            if (arrayDepth > 0) sb.append("[]".repeat(arrayDepth));
 
             return sb.toString();
         }
@@ -1589,12 +1588,7 @@
                     dimensions++;
                     cl = cl.getComponentType();
                 } while (cl.isArray());
-                StringBuilder sb = new StringBuilder();
-                sb.append(cl.getName());
-                for (int i = 0; i < dimensions; i++) {
-                    sb.append("[]");
-                }
-                return sb.toString();
+                return cl.getName() + "[]".repeat(dimensions);
             } catch (Throwable e) { /*FALLTHRU*/ }
         }
         return getName();
@@ -3418,15 +3412,12 @@
      * Helper method to get the method name from arguments.
      */
     private String methodToString(String name, Class<?>[] argTypes) {
-        StringBuilder sb = new StringBuilder();
-        sb.append(getName() + "." + name + "(");
-        if (argTypes != null) {
-            sb.append(Arrays.stream(argTypes)
-                      .map(c -> (c == null) ? "null" : c.getName())
-                      .collect(Collectors.joining(",")));
-        }
-        sb.append(")");
-        return sb.toString();
+        return getName() + '.' + name +
+                ((argTypes == null || argTypes.length == 0) ?
+                "()" :
+                Arrays.stream(argTypes)
+                        .map(c -> c == null ? "null" : c.getName())
+                        .collect(Collectors.joining(",", "(", ")")));
     }
 
     /** use serialVersionUID from JDK 1.1 for interoperability */