groovy-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sun...@apache.org
Subject [1/4] groovy git commit: Trivial refactoring: Replace String with StringBuilder to concatenate strings
Date Tue, 05 Dec 2017 01:59:57 GMT
Repository: groovy
Updated Branches:
  refs/heads/GROOVY_2_5_X 56d450560 -> 699d79bd5


Trivial refactoring: Replace String with StringBuilder to concatenate strings

(cherry picked from commit c84ca72)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/c3b070e1
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/c3b070e1
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/c3b070e1

Branch: refs/heads/GROOVY_2_5_X
Commit: c3b070e163f96d2ab1fe88bb39577b0806094284
Parents: 56d4505
Author: sunlan <sunlan@apache.org>
Authored: Tue Dec 5 08:06:50 2017 +0800
Committer: sunlan <sunlan@apache.org>
Committed: Tue Dec 5 09:59:34 2017 +0800

----------------------------------------------------------------------
 src/main/org/codehaus/groovy/ast/ClassNode.java | 32 ++++++++++----------
 .../org/codehaus/groovy/ast/GenericsType.java   | 12 ++++----
 .../groovy/classgen/AsmClassGenerator.java      |  6 ++--
 .../classgen/asm/OptimizingStatementWriter.java | 20 ++++++------
 .../classgen/asm/indy/InvokeDynamicWriter.java  | 12 ++++----
 .../groovy/control/GenericsVisitor.java         | 24 +++++++--------
 .../groovy/tools/LoaderConfiguration.java       | 12 ++++----
 .../codehaus/groovy/vmplugin/v7/Selector.java   | 29 +++++++++---------
 .../ui/text/StructuredSyntaxDocumentFilter.java | 20 ++++++------
 .../jsr223/GroovyScriptEngineFactory.java       | 14 ++++-----
 10 files changed, 90 insertions(+), 91 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/c3b070e1/src/main/org/codehaus/groovy/ast/ClassNode.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/ast/ClassNode.java b/src/main/org/codehaus/groovy/ast/ClassNode.java
index d3b70f1..4e0684f 100644
--- a/src/main/org/codehaus/groovy/ast/ClassNode.java
+++ b/src/main/org/codehaus/groovy/ast/ClassNode.java
@@ -1172,21 +1172,21 @@ public class ClassNode extends AnnotatedNode implements Opcodes {
         if (isArray()) {
             return componentType.toString(showRedirect)+"[]";
         }
-        String ret = getName();
-        if (placeholder) ret = getUnresolvedName();
+        StringBuilder ret = new StringBuilder(getName());
+        if (placeholder) ret = new StringBuilder(getUnresolvedName());
         if (!placeholder && genericsTypes != null) {
-            ret += " <";
+            ret.append(" <");
             for (int i = 0; i < genericsTypes.length; i++) {
-                if (i != 0) ret += ", ";
+                if (i != 0) ret.append(", ");
                 GenericsType genericsType = genericsTypes[i];
-                ret += genericTypeAsString(genericsType);
+                ret.append(genericTypeAsString(genericsType));
             }
-            ret += ">";
+            ret.append(">");
         }
         if (redirect != null && showRedirect) {
-            ret += " -> " + redirect().toString();
+            ret.append(" -> ").append(redirect().toString());
         }
-        return ret;
+        return ret.toString();
     }
 
     /**
@@ -1196,27 +1196,27 @@ public class ClassNode extends AnnotatedNode implements Opcodes {
      * @return the string representing the generic type
      */
     private String genericTypeAsString(GenericsType genericsType) {
-        String ret = genericsType.getName();
+        StringBuilder ret = new StringBuilder(genericsType.getName());
         if (genericsType.getUpperBounds() != null) {
-            ret += " extends ";
+            ret.append(" extends ");
             for (int i = 0; i < genericsType.getUpperBounds().length; i++) {
                 ClassNode classNode = genericsType.getUpperBounds()[i];
                 if (classNode.equals(this)) {
-                    ret += classNode.getName();
+                    ret.append(classNode.getName());
                 } else {
-                    ret += classNode.toString(false);
+                    ret.append(classNode.toString(false));
                 }
-                if (i + 1 < genericsType.getUpperBounds().length) ret += " & ";
+                if (i + 1 < genericsType.getUpperBounds().length) ret.append(" & ");
             }
         } else if (genericsType.getLowerBound() !=null) {
             ClassNode classNode = genericsType.getLowerBound();
             if (classNode.equals(this)) {
-                ret += " super " + classNode.getName();
+                ret.append(" super ").append(classNode.getName());
             } else {
-                ret += " super " + classNode;
+                ret.append(" super ").append(classNode);
             }
         }
-        return ret;
+        return ret.toString();
     }
 
     /**

http://git-wip-us.apache.org/repos/asf/groovy/blob/c3b070e1/src/main/org/codehaus/groovy/ast/GenericsType.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/ast/GenericsType.java b/src/main/org/codehaus/groovy/ast/GenericsType.java
index 55d0bb0..57d4919 100644
--- a/src/main/org/codehaus/groovy/ast/GenericsType.java
+++ b/src/main/org/codehaus/groovy/ast/GenericsType.java
@@ -73,21 +73,21 @@ public class GenericsType extends ASTNode {
 
     private String toString(Set<String> visited) {
         if (placeholder) visited.add(name);
-        String ret = wildcard?"?":((type == null || placeholder) ? name : genericsBounds(type,
visited));
+        StringBuilder ret = new StringBuilder(wildcard ? "?" : ((type == null || placeholder)
? name : genericsBounds(type, visited)));
         if (upperBounds != null) {
             if (placeholder && upperBounds.length==1 && !upperBounds[0].isGenericsPlaceHolder()
&& upperBounds[0].getName().equals("java.lang.Object")) {
                 // T extends Object should just be printed as T
             } else {
-                ret += " extends ";
+                ret.append(" extends ");
                 for (int i = 0; i < upperBounds.length; i++) {
-                    ret += genericsBounds(upperBounds[i], visited);
-                    if (i + 1 < upperBounds.length) ret += " & ";
+                    ret.append(genericsBounds(upperBounds[i], visited));
+                    if (i + 1 < upperBounds.length) ret.append(" & ");
                 }
             }
         } else if (lowerBound != null) {
-            ret += " super " + genericsBounds(lowerBound, visited);
+            ret.append(" super ").append(genericsBounds(lowerBound, visited));
         }
-        return ret;
+        return ret.toString();
     }
 
     private String nameOf(ClassNode theType) {

http://git-wip-us.apache.org/repos/asf/groovy/blob/c3b070e1/src/main/org/codehaus/groovy/classgen/AsmClassGenerator.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/classgen/AsmClassGenerator.java b/src/main/org/codehaus/groovy/classgen/AsmClassGenerator.java
index e6cc30c..9fb9a81 100644
--- a/src/main/org/codehaus/groovy/classgen/AsmClassGenerator.java
+++ b/src/main/org/codehaus/groovy/classgen/AsmClassGenerator.java
@@ -881,11 +881,11 @@ public class AsmClassGenerator extends ClassGenerator {
 
     private static String getStaticFieldName(ClassNode type) {
         ClassNode componentType = type;
-        String prefix = "";
+        StringBuilder prefix = new StringBuilder();
         for (; componentType.isArray(); componentType = componentType.getComponentType())
{
-            prefix += "$";
+            prefix.append("$");
         }
-        if (prefix.length() != 0) prefix = "array" + prefix;
+        if (prefix.length() != 0) prefix.insert(0, "array");
         String name = prefix + "$class$" + makeFieldClassName(componentType);
         return name;
     }

http://git-wip-us.apache.org/repos/asf/groovy/blob/c3b070e1/src/main/org/codehaus/groovy/classgen/asm/OptimizingStatementWriter.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/classgen/asm/OptimizingStatementWriter.java b/src/main/org/codehaus/groovy/classgen/asm/OptimizingStatementWriter.java
index e73dc5f..e553477 100644
--- a/src/main/org/codehaus/groovy/classgen/asm/OptimizingStatementWriter.java
+++ b/src/main/org/codehaus/groovy/classgen/asm/OptimizingStatementWriter.java
@@ -111,11 +111,11 @@ public class OptimizingStatementWriter extends StatementWriter {
             }
         }
         public String toString() {
-            String ret = "optimize="+optimize+" target="+target+" type="+type+" involvedTypes=";
+            StringBuilder ret = new StringBuilder("optimize=" + optimize + " target=" + target
+ " type=" + type + " involvedTypes=");
             for (int i=0; i<typeMapKeyNames.length; i++) {
-                if (involvedTypes[i]) ret += " "+typeMapKeyNames[i];
+                if (involvedTypes[i]) ret.append(" ").append(typeMapKeyNames[i]);
             }
-            return ret;
+            return ret.toString();
         }
     }
 
@@ -498,19 +498,19 @@ public class OptimizingStatementWriter extends StatementWriter {
             }
         }
         public String toString() {
-            String ret = "";
+            StringBuilder ret;
             if (current.shouldOptimize) {
-                ret = "should optimize, can = "+current.canOptimize;
+                ret = new StringBuilder("should optimize, can = " + current.canOptimize);
             } else if (current.canOptimize) {
-                ret = "can optimize";
+                ret = new StringBuilder("can optimize");
             } else {
-                ret = "don't optimize";
+                ret = new StringBuilder("don't optimize");
             }
-            ret += " involvedTypes =";
+            ret.append(" involvedTypes =");
             for (int i=0; i<typeMapKeyNames.length; i++) {
-                if (current.involvedTypes[i]) ret += " "+typeMapKeyNames[i];
+                if (current.involvedTypes[i]) ret.append(" ").append(typeMapKeyNames[i]);
             }
-            return ret;
+            return ret.toString();
         }
         /**
          * @return true iff we should Optimize - this is almost seen as must

http://git-wip-us.apache.org/repos/asf/groovy/blob/c3b070e1/src/main/org/codehaus/groovy/classgen/asm/indy/InvokeDynamicWriter.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/classgen/asm/indy/InvokeDynamicWriter.java b/src/main/org/codehaus/groovy/classgen/asm/indy/InvokeDynamicWriter.java
index 9cd08c5..038b1c8 100644
--- a/src/main/org/codehaus/groovy/classgen/asm/indy/InvokeDynamicWriter.java
+++ b/src/main/org/codehaus/groovy/classgen/asm/indy/InvokeDynamicWriter.java
@@ -127,7 +127,7 @@ public class InvokeDynamicWriter extends InvocationWriter {
     private void makeIndyCall(MethodCallerMultiAdapter adapter, Expression receiver, boolean
implicitThis, boolean safe, String methodName, Expression arguments) {
         OperandStack operandStack = controller.getOperandStack();
         
-        String sig = prepareIndyCall(receiver, implicitThis);
+        StringBuilder sig = new StringBuilder(prepareIndyCall(receiver, implicitThis));
         
         // load arguments
         int numberOfArguments = 1;
@@ -135,26 +135,26 @@ public class InvokeDynamicWriter extends InvocationWriter {
         boolean containsSpreadExpression = AsmClassGenerator.containsSpreadExpression(arguments);
         if (containsSpreadExpression) {
             controller.getAcg().despreadList(ae.getExpressions(), true);
-            sig += getTypeDescription(Object[].class);
+            sig.append(getTypeDescription(Object[].class));
         } else {
             for (Expression arg : ae.getExpressions()) {
                 arg.visit(controller.getAcg());
                 if (arg instanceof CastExpression) {
                     operandStack.box();
                     controller.getAcg().loadWrapper(arg);
-                    sig += getTypeDescription(Wrapper.class);
+                    sig.append(getTypeDescription(Wrapper.class));
                 } else {
-                    sig += getTypeDescription(operandStack.getTopOperand());
+                    sig.append(getTypeDescription(operandStack.getTopOperand()));
                 }
                 numberOfArguments++;
             }
         }
 
-        sig += ")Ljava/lang/Object;";
+        sig.append(")Ljava/lang/Object;");
         String callSiteName = METHOD.getCallSiteName();
         if (adapter==null) callSiteName = INIT.getCallSiteName();
         int flags = getMethodCallFlags(adapter, safe, containsSpreadExpression);
-        finishIndyCall(BSM, callSiteName, sig, numberOfArguments, methodName, flags);
+        finishIndyCall(BSM, callSiteName, sig.toString(), numberOfArguments, methodName,
flags);
     }
     
     private static int getMethodCallFlags(MethodCallerMultiAdapter adapter, boolean safe,
boolean spread) {

http://git-wip-us.apache.org/repos/asf/groovy/blob/c3b070e1/src/main/org/codehaus/groovy/control/GenericsVisitor.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/control/GenericsVisitor.java b/src/main/org/codehaus/groovy/control/GenericsVisitor.java
index 6447ab9..36ef5ea 100644
--- a/src/main/org/codehaus/groovy/control/GenericsVisitor.java
+++ b/src/main/org/codehaus/groovy/control/GenericsVisitor.java
@@ -155,34 +155,34 @@ public class GenericsVisitor extends ClassCodeVisitorSupport {
     }
 
     private static String getPrintName(GenericsType gt) {
-        String ret = gt.getName();
+        StringBuilder ret = new StringBuilder(gt.getName());
         ClassNode[] upperBounds = gt.getUpperBounds();
         ClassNode lowerBound = gt.getLowerBound();
         if (upperBounds != null) {
             if (upperBounds.length != 1 || !"java.lang.Object".equals(getPrintName(upperBounds[0])))
{
-                ret += " extends ";
+                ret.append(" extends ");
                 for (int i = 0; i < upperBounds.length; i++) {
-                    ret += getPrintName(upperBounds[i]);
-                    if (i + 1 < upperBounds.length) ret += " & ";
+                    ret.append(getPrintName(upperBounds[i]));
+                    if (i + 1 < upperBounds.length) ret.append(" & ");
                 }
             }
         } else if (lowerBound != null) {
-            ret += " super " + getPrintName(lowerBound);
+            ret.append(" super ").append(getPrintName(lowerBound));
         }
-        return ret;
+        return ret.toString();
     }
 
     private static String getPrintName(ClassNode cn) {
-        String ret = cn.getName();
+        StringBuilder ret = new StringBuilder(cn.getName());
         GenericsType[] gts = cn.getGenericsTypes();
         if (gts != null) {
-            ret += "<";
+            ret.append("<");
             for (int i = 0; i < gts.length; i++) {
-                if (i != 0) ret += ",";
-                ret += getPrintName(gts[i]);
+                if (i != 0) ret.append(",");
+                ret.append(getPrintName(gts[i]));
             }
-            ret += ">";
+            ret.append(">");
         }
-        return ret;
+        return ret.toString();
     }
 }

http://git-wip-us.apache.org/repos/asf/groovy/blob/c3b070e1/src/main/org/codehaus/groovy/tools/LoaderConfiguration.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/tools/LoaderConfiguration.java b/src/main/org/codehaus/groovy/tools/LoaderConfiguration.java
index ff9b357..6b9da28 100644
--- a/src/main/org/codehaus/groovy/tools/LoaderConfiguration.java
+++ b/src/main/org/codehaus/groovy/tools/LoaderConfiguration.java
@@ -151,7 +151,7 @@ public class LoaderConfiguration {
     private static String assignProperties(String str) {
         int propertyIndexStart = 0, propertyIndexEnd = 0;
         boolean requireProperty;
-        String result = "";
+        StringBuilder result = new StringBuilder();
 
         while (propertyIndexStart < str.length()) {
             {
@@ -167,7 +167,7 @@ public class LoaderConfiguration {
                 requireProperty = propertyIndexStart == i2;
             }
             if (propertyIndexStart == -1) break;
-            result += str.substring(propertyIndexEnd, propertyIndexStart);
+            result.append(str.substring(propertyIndexEnd, propertyIndexStart));
 
             propertyIndexEnd = str.indexOf("}", propertyIndexStart);
             if (propertyIndexEnd == -1) break;
@@ -184,19 +184,19 @@ public class LoaderConfiguration {
             }
             propertyValue = getSlashyPath(propertyValue);
             propertyValue = correctDoubleSlash(propertyValue, propertyIndexEnd, str);
-            result += propertyValue;
+            result.append(propertyValue);
 
             propertyIndexEnd++;
             propertyIndexStart = propertyIndexEnd;
         }
 
         if (propertyIndexStart == -1 || propertyIndexStart >= str.length()) {
-            result += str.substring(propertyIndexEnd);
+            result.append(str.substring(propertyIndexEnd));
         } else if (propertyIndexEnd == -1) {
-            result += str.substring(propertyIndexStart);
+            result.append(str.substring(propertyIndexStart));
         }
 
-        return result;
+        return result.toString();
     }
 
 

http://git-wip-us.apache.org/repos/asf/groovy/blob/c3b070e1/src/main/org/codehaus/groovy/vmplugin/v7/Selector.java
----------------------------------------------------------------------
diff --git a/src/main/org/codehaus/groovy/vmplugin/v7/Selector.java b/src/main/org/codehaus/groovy/vmplugin/v7/Selector.java
index d1ce7d7..aa615b1 100644
--- a/src/main/org/codehaus/groovy/vmplugin/v7/Selector.java
+++ b/src/main/org/codehaus/groovy/vmplugin/v7/Selector.java
@@ -513,26 +513,25 @@ public abstract class Selector {
             this.cache = !spread;
 
             if (LOG_ENABLED) {
-                String msg =
-                    "----------------------------------------------------"+
-                    "\n\t\tinvocation of method '"+methodName+"'"+
-                    "\n\t\tinvocation type: "+callType+
-                    "\n\t\tsender: "+sender+
-                    "\n\t\ttargetType: "+targetType+
-                    "\n\t\tsafe navigation: "+safeNavigation+
-                    "\n\t\tthisCall: "+thisCall+
-                    "\n\t\tspreadCall: "+spreadCall+
-                    "\n\t\twith "+arguments.length+" arguments";
+                StringBuilder msg =
+                        new StringBuilder("----------------------------------------------------"
+
+                                "\n\t\tinvocation of method '" + methodName + "'" +
+                                "\n\t\tinvocation type: " + callType +
+                                "\n\t\tsender: " + sender +
+                                "\n\t\ttargetType: " + targetType +
+                                "\n\t\tsafe navigation: " + safeNavigation +
+                                "\n\t\tthisCall: " + thisCall +
+                                "\n\t\tspreadCall: " + spreadCall +
+                                "\n\t\twith " + arguments.length + " arguments");
                 for (int i=0; i<arguments.length; i++) {
-                    msg += "\n\t\t\targument["+i+"] = ";
+                    msg.append("\n\t\t\targument[").append(i).append("] = ");
                     if (arguments[i] == null) {
-                        msg += "null";
+                        msg.append("null");
                     } else {
-                        msg += arguments[i].getClass().getName()+
-                            "@"+Integer.toHexString(System.identityHashCode(arguments[i]));
+                        msg.append(arguments[i].getClass().getName()).append("@").append(Integer.toHexString(System.identityHashCode(arguments[i])));
                     }
                 }
-                LOG.info(msg);
+                LOG.info(msg.toString());
             }
         }
 

http://git-wip-us.apache.org/repos/asf/groovy/blob/c3b070e1/subprojects/groovy-console/src/main/groovy/groovy/ui/text/StructuredSyntaxDocumentFilter.java
----------------------------------------------------------------------
diff --git a/subprojects/groovy-console/src/main/groovy/groovy/ui/text/StructuredSyntaxDocumentFilter.java
b/subprojects/groovy-console/src/main/groovy/groovy/ui/text/StructuredSyntaxDocumentFilter.java
index 3945c2f..63f86ae 100644
--- a/subprojects/groovy-console/src/main/groovy/groovy/ui/text/StructuredSyntaxDocumentFilter.java
+++ b/subprojects/groovy-console/src/main/groovy/groovy/ui/text/StructuredSyntaxDocumentFilter.java
@@ -71,13 +71,13 @@ public class StructuredSyntaxDocumentFilter extends DocumentFilter {
         String checking = regexp.replaceAll("\\\\\\(", "X").replaceAll("\\(\\?", "X");
         int checked = checking.indexOf('(');
         if (checked > -1) {
-            String msg = "Only non-capturing groups allowed:\r\n" +
-                         regexp + "\r\n";
+            StringBuilder msg = new StringBuilder("Only non-capturing groups allowed:\r\n"
+
+                    regexp + "\r\n");
             for (int i = 0; i < checked; i++) {
-                msg += " ";
+                msg.append(" ");
             }
-            msg += "^";
-            throw new IllegalArgumentException(msg);
+            msg.append("^");
+            throw new IllegalArgumentException(msg.toString());
         }
     }
     
@@ -313,10 +313,10 @@ public class StructuredSyntaxDocumentFilter extends DocumentFilter {
         }
     
         private String buildRegexp(String[] regexps) {
-            String regexp = "";
+            StringBuilder regexp = new StringBuilder();
 
             for (int i = 0; i < regexps.length; i++) {
-                regexp += "|" + regexps[i];
+                regexp.append("|").append(regexps[i]);
             }
 
             // ensure leading '|' is removed
@@ -333,14 +333,14 @@ public class StructuredSyntaxDocumentFilter extends DocumentFilter {
             groupList.add(null);
             
             Iterator iter = styleMap.keySet().iterator();
-            String regexp = "";
+            StringBuilder regexp = new StringBuilder();
             while (iter.hasNext()) {
                 String nextRegexp = (String)iter.next();
-                regexp += "|(" + nextRegexp + ")";
+                regexp.append("|(").append(nextRegexp).append(")");
                 // have to compile regexp first so that it will match
                 groupList.add(Pattern.compile(nextRegexp).pattern());
             }
-            if (!regexp.equals("")) {
+            if (!regexp.toString().equals("")) {
                 matcher = Pattern.compile(regexp.substring(1)).matcher("");
                 
                 iter = children.values().iterator();

http://git-wip-us.apache.org/repos/asf/groovy/blob/c3b070e1/subprojects/groovy-jsr223/src/main/java/org/codehaus/groovy/jsr223/GroovyScriptEngineFactory.java
----------------------------------------------------------------------
diff --git a/subprojects/groovy-jsr223/src/main/java/org/codehaus/groovy/jsr223/GroovyScriptEngineFactory.java
b/subprojects/groovy-jsr223/src/main/java/org/codehaus/groovy/jsr223/GroovyScriptEngineFactory.java
index c8deabf..62981f6 100644
--- a/subprojects/groovy-jsr223/src/main/java/org/codehaus/groovy/jsr223/GroovyScriptEngineFactory.java
+++ b/subprojects/groovy-jsr223/src/main/java/org/codehaus/groovy/jsr223/GroovyScriptEngineFactory.java
@@ -137,22 +137,22 @@ public class GroovyScriptEngineFactory implements ScriptEngineFactory
{
     public String getMethodCallSyntax(String obj, String method,
                                       String... args) {
 
-        String ret = obj + "." + method + "(";
+        StringBuilder ret = new StringBuilder(obj + "." + method + "(");
         int len = args.length;
         if (len == 0) {
-            ret += ")";
-            return ret;
+            ret.append(")");
+            return ret.toString();
         }
 
         for (int i = 0; i < len; i++) {
-            ret += args[i];
+            ret.append(args[i]);
             if (i != len - 1) {
-                ret += ",";
+                ret.append(",");
             } else {
-                ret += ")";
+                ret.append(")");
             }
         }
-        return ret;
+        return ret.toString();
     }
 
     public String getOutputStatement(String toDisplay) {


Mime
View raw message