From be522b7e8a591ef54f71d574dd5c76799a2b5136 Mon Sep 17 00:00:00 2001 From: Andrey Turbanov Date: Sat, 18 Dec 2021 15:53:24 +0300 Subject: [PATCH] Use StringBuilder/StringJoiner for concatenation String's in loop --- .../internal/LstBuildConfigFileUpdater.java | 12 ++--- .../ui/internal/UserPreferencesStore.java | 6 +-- .../aspectj/tools/ajdoc/HtmlDecorator.java | 53 ++++++++++--------- .../core/builder/AsmElementFormatter.java | 10 ++-- .../org/aspectj/tools/ant/taskdefs/Ajdoc.java | 11 ++-- 5 files changed, 47 insertions(+), 45 deletions(-) diff --git a/ajde/src/main/java/org/aspectj/ajde/internal/LstBuildConfigFileUpdater.java b/ajde/src/main/java/org/aspectj/ajde/internal/LstBuildConfigFileUpdater.java index 5b83060b8..fd2940c32 100644 --- a/ajde/src/main/java/org/aspectj/ajde/internal/LstBuildConfigFileUpdater.java +++ b/ajde/src/main/java/org/aspectj/ajde/internal/LstBuildConfigFileUpdater.java @@ -117,11 +117,11 @@ class LstBuildConfigFileUpdater { public void writeConfigFile(String filePath, List files, List importedNodes) { // Set contentsSet = new TreeSet(fileContents); - String fileContentsString = ""; + StringBuilder fileContentsString = new StringBuilder(); // List filesToWrite = null; Set includedFiles = new HashSet<>(); for (BuildConfigNode node : importedNodes) { - fileContentsString += '@' + node.getResourcePath() + "\n"; + fileContentsString.append('@').append(node.getResourcePath()).append("\n"); String parentPath = new File(filePath).getParent(); String importedFilePath = parentPath + File.separator + node.getResourcePath(); includedFiles.addAll(getIncludedFiles(importedFilePath, parentPath)); @@ -129,15 +129,15 @@ class LstBuildConfigFileUpdater { for (BuildConfigNode node : files) { if (node.getName().endsWith(".lst") && !node.getResourcePath().startsWith("..")) { - fileContentsString += '@'; - fileContentsString += node.getResourcePath() + "\n"; + fileContentsString.append('@'); + fileContentsString.append(node.getResourcePath()).append("\n"); } else { if (!includedFiles.contains(node.getResourcePath())) { - fileContentsString += node.getResourcePath() + "\n"; + fileContentsString.append(node.getResourcePath()).append("\n"); } } } - writeFile(fileContentsString, filePath); + writeFile(fileContentsString.toString(), filePath); } private List getIncludedFiles(String path, String rootPath) { diff --git a/ajde/src/main/java/org/aspectj/ajde/ui/internal/UserPreferencesStore.java b/ajde/src/main/java/org/aspectj/ajde/ui/internal/UserPreferencesStore.java index b8dc93332..d774dadee 100644 --- a/ajde/src/main/java/org/aspectj/ajde/ui/internal/UserPreferencesStore.java +++ b/ajde/src/main/java/org/aspectj/ajde/ui/internal/UserPreferencesStore.java @@ -74,11 +74,11 @@ public class UserPreferencesStore implements UserPreferencesAdapter { @Override public void setProjectMultivalPreference(String name, List values) { - String valuesString = ""; + StringBuilder valuesString = new StringBuilder(); for (Object value : values) { - valuesString += (String) value + ';'; + valuesString.append((String)value).append(';'); } - properties.setProperty(name, valuesString); + properties.setProperty(name, valuesString.toString()); saveProperties(); } diff --git a/ajdoc/src/main/java/org/aspectj/tools/ajdoc/HtmlDecorator.java b/ajdoc/src/main/java/org/aspectj/tools/ajdoc/HtmlDecorator.java index 3f34d1b3d..8e2432092 100644 --- a/ajdoc/src/main/java/org/aspectj/tools/ajdoc/HtmlDecorator.java +++ b/ajdoc/src/main/java/org/aspectj/tools/ajdoc/HtmlDecorator.java @@ -572,8 +572,8 @@ class HtmlDecorator { static void decorateDocWithRel(IProgramElement node, StringBuilder fileContentsBuffer, int index, List targets, HtmlRelationshipKind relKind) { if (targets != null && !targets.isEmpty()) { - String adviceDoc = "" - + "
" + relKind.toString() + ""; + StringBuilder adviceDoc = new StringBuilder("" + + "
" + relKind.toString() + ""); String relativePackagePath = getRelativePathFromHere(node.getPackageName().replace('.', '/') + Config.DIR_SEP_CHAR); @@ -644,15 +644,15 @@ class HtmlDecorator { hrefLink += sbuff.toString() + ".html" + "#" + sb.toString(); if (!addedNames.contains(hrefName)) { - adviceDoc = adviceDoc + "" + hrefName.replace('/', '.') + ""; + adviceDoc.append("").append(hrefName.replace('/', '.')).append(""); if (it.hasNext()) - adviceDoc += ", "; + adviceDoc.append(", "); addedNames.add(hrefName); } } - adviceDoc += "
\n"; - fileContentsBuffer.insert(index, adviceDoc); + adviceDoc.append("
\n"); + fileContentsBuffer.insert(index, adviceDoc.toString()); } } @@ -710,24 +710,29 @@ class HtmlDecorator { } if (targets == null) return ""; - String entry = ""; + StringBuilder entry = new StringBuilder("
"); IProgramElement.Kind kind = decl.getKind(); if (kind.equals(IProgramElement.Kind.ADVICE)) { - entry += "
" + HtmlRelationshipKind.ADVISES.toString() - + ""; + entry.append("") + .append(HtmlRelationshipKind.ADVISES) + .append(""); } else if (kind.equals(IProgramElement.Kind.DECLARE_WARNING) || kind.equals(IProgramElement.Kind.DECLARE_ERROR)) { - entry += "" + HtmlRelationshipKind.MATCHED_BY.toString() - + ""; + entry.append("") + .append(HtmlRelationshipKind.MATCHED_BY) + .append(""); } else if (kind.isDeclareAnnotation()) { - entry += "" + HtmlRelationshipKind.ANNOTATES.toString() - + ""; + entry.append("") + .append(HtmlRelationshipKind.ANNOTATES) + .append(""); } else if (kind.equals(IProgramElement.Kind.DECLARE_SOFT)) { - entry += "" + HtmlRelationshipKind.SOFTENS.toString() - + ""; + entry.append("") + .append(HtmlRelationshipKind.SOFTENS) + .append(""); } else { - entry += "" + HtmlRelationshipKind.DECLARED_ON.toString() - + ""; + entry.append("") + .append(HtmlRelationshipKind.DECLARED_ON) + .append(""); } String relativePackagePath = getRelativePathFromHere(decl.getPackageName().replace('.', '/') + Config.DIR_SEP_CHAR); @@ -759,15 +764,15 @@ class HtmlDecorator { } if (!addedNames.contains(hrefName)) { - entry += "" + hrefName.replace('/', '.') + ""; // !!! don't replace + entry.append("").append(hrefName.replace('/', '.')).append(""); // !!! don't replace if (it.hasNext()) - entry += ", "; + entry.append(", "); addedNames.add(hrefName); } } } - entry += "
\n\n"; - return entry; + entry.append("\n\n"); + return entry.toString(); } /** @@ -873,7 +878,7 @@ class HtmlDecorator { if (comment == null) return ""; - String formattedComment = ""; + StringBuilder formattedComment = new StringBuilder(); // strip the comment markers int startIndex = comment.indexOf("/**"); @@ -909,12 +914,12 @@ class HtmlDecorator { // if ( linkIndex != -1 ) { // line = line.substring(0, linkIndex) + line.substring(linkIndex); // } - formattedComment += line; + formattedComment.append(line); } } catch (IOException ioe) { throw new Error("Couldn't format comment for declaration: " + decl.getName()); } - return formattedComment; + return formattedComment.toString(); } static public IProgramElement[] getProgramElements(AsmManager model, String filename) { diff --git a/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/core/builder/AsmElementFormatter.java b/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/core/builder/AsmElementFormatter.java index 5ce84dca7..699ffe3e0 100644 --- a/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/core/builder/AsmElementFormatter.java +++ b/org.aspectj.ajdt.core/src/main/java/org/aspectj/ajdt/internal/core/builder/AsmElementFormatter.java @@ -16,6 +16,7 @@ package org.aspectj.ajdt.internal.core.builder; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.StringJoiner; import org.aspectj.ajdt.internal.compiler.ast.AdviceDeclaration; import org.aspectj.ajdt.internal.compiler.ast.DeclareDeclaration; @@ -256,14 +257,11 @@ public class AsmElementFormatter { } private String genPrecedenceListLabel(TypePatternList list) { - String tpList = ""; + StringJoiner tpList = new StringJoiner(", "); for (int i = 0; i < list.size(); i++) { - tpList += genTypePatternLabel(list.get(i)); - if (i < list.size() - 1) { - tpList += ", "; - } + tpList.add(genTypePatternLabel(list.get(i))); } - return tpList; + return tpList.toString(); } // private String genArguments(MethodDeclaration md) { diff --git a/taskdefs/src/main/java/org/aspectj/tools/ant/taskdefs/Ajdoc.java b/taskdefs/src/main/java/org/aspectj/tools/ant/taskdefs/Ajdoc.java index c26c13309..e58db40f2 100644 --- a/taskdefs/src/main/java/org/aspectj/tools/ant/taskdefs/Ajdoc.java +++ b/taskdefs/src/main/java/org/aspectj/tools/ant/taskdefs/Ajdoc.java @@ -20,9 +20,9 @@ import java.util.Collection; import java.util.Collections; import java.util.Enumeration; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.StringJoiner; import java.util.StringTokenizer; import java.util.Vector; @@ -636,14 +636,13 @@ public class Ajdoc extends MatchingTask { } for (String title: groupMap.keySet()) { List packages = groupMap.get(title); - String pkgstr = ""; - for (Iterator j = packages.iterator(); j.hasNext();) { - pkgstr += j.next(); - if (j.hasNext()) pkgstr += ","; + StringJoiner pkgstr = new StringJoiner(","); + for (String aPackage : packages) { + pkgstr.add(aPackage); } cmd.createArgument().setValue("-group"); cmd.createArgument().setValue(title); - cmd.createArgument().setValue(pkgstr); + cmd.createArgument().setValue(pkgstr.toString()); } if (argfiles != null) { for (File file : argfiles) { -- 2.39.5