< prev index next >

src/java.base/share/classes/java/lang/Class.java

Print this page
rev 54515 : 8222151: refactoring: enhancements to java.lang.Class::methodToString and java.lang.Class::getTypeName

@@ -275,12 +275,11 @@
                 sb.append(Arrays.stream(typeparms)
                           .map(Class::typeVarBounds)
                           .collect(Collectors.joining(",", "<", ">")));
             }
 
-            for (int i = 0; i < arrayDepth; i++)
-                sb.append("[]");
+            if (arrayDepth > 0) sb.append("[]".repeat(arrayDepth));
 
             return sb.toString();
         }
     }
 

@@ -1587,16 +1586,11 @@
                 int dimensions = 0;
                 do {
                     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();
     }
 

@@ -3416,19 +3410,18 @@
 
     /**
      * 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();
+        String prefix = getName() + '.' + name;
+        if (argTypes == null || argTypes.length == 0) {
+            return prefix + "()";
+        }
+        return prefix
+                + Arrays.stream(argTypes)
+                .map(c -> c == null ? "null" : c.getName())
+                .collect(Collectors.joining(",", "(", ")"));
     }
 
     /** use serialVersionUID from JDK 1.1 for interoperability */
     private static final long serialVersionUID = 3206093459760846163L;
 
< prev index next >