Skip to content

Commit 7546361

Browse files
authored
[MNG-8475] In the loop scenario, StringBuilder is used instead of concatenation (#2014)
Signed-off-by: crazyhzm <[email protected]> --- https://issues.apache.org/jira/browse/MNG-8475
1 parent 209cd7f commit 7546361

File tree

4 files changed

+27
-30
lines changed

4 files changed

+27
-30
lines changed

compat/maven-compat/src/test/java/org/apache/maven/project/AbstractMavenProjectTestCase.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,12 @@ protected MavenProject getProjectWithDependencies(File pom) throws Exception {
135135
} catch (Exception e) {
136136
Throwable cause = e.getCause();
137137
if (cause instanceof ModelBuildingException) {
138-
String message = "In: " + pom + "\n\n";
138+
StringBuilder message = new StringBuilder("In: " + pom + "\n\n");
139139
for (ModelProblem problem : ((ModelBuildingException) cause).getProblems()) {
140-
message += problem + "\n";
140+
message.append(problem).append("\n");
141141
}
142142
System.out.println(message);
143-
fail(message);
143+
fail(message.toString());
144144
}
145145

146146
throw e;

compat/maven-resolver-provider/src/main/java/org/apache/maven/repository/internal/DefaultArtifactDescriptorReader.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,19 +240,19 @@ private Model loadPom(
240240
if (logger.isDebugEnabled()) {
241241
String problem = (problems.size() == 1) ? "problem" : "problems";
242242
String problemPredicate = problem + ((problems.size() == 1) ? " was" : " were");
243-
String message = String.format(
243+
StringBuilder message = new StringBuilder(String.format(
244244
"%s %s encountered while building the effective model for %s during %s\n",
245245
problems.size(),
246246
problemPredicate,
247247
request.getArtifact(),
248-
RequestTraceHelper.interpretTrace(true, request.getTrace()));
249-
message += StringUtils.capitalizeFirstLetter(problem);
248+
RequestTraceHelper.interpretTrace(true, request.getTrace())));
249+
message.append(StringUtils.capitalizeFirstLetter(problem));
250250
for (ModelProblem modelProblem : problems) {
251-
message += String.format(
251+
message.append(String.format(
252252
"\n* %s @ %s",
253-
modelProblem.getMessage(), ModelProblemUtils.formatLocation(modelProblem, null));
253+
modelProblem.getMessage(), ModelProblemUtils.formatLocation(modelProblem, null)));
254254
}
255-
logger.warn(message);
255+
logger.warn(message.toString());
256256
} else {
257257
logger.warn(
258258
"{} {} encountered while building the effective model for {} during {} (use -X to see details)",

impl/maven-core/src/main/java/org/apache/maven/internal/aether/ReverseTreeRepositoryListener.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ public void artifactResolved(RepositoryEvent event) {
9898
String ext = missing ? ".miss" : ".dep";
9999
Path trackingFile = null;
100100

101-
String indent = "";
101+
StringBuilder indent = new StringBuilder();
102102
ArrayList<String> trackingData = new ArrayList<>();
103103

104104
if (collectStepTrace == null && plugin != null) {
@@ -110,16 +110,16 @@ public void artifactResolved(RepositoryEvent event) {
110110
}
111111

112112
if (event.getArtifact() != null) {
113-
trackingData.add(indent + event.getArtifact());
114-
indent += " ";
113+
trackingData.add(indent.toString() + event.getArtifact());
114+
indent.append(" ");
115115
}
116116
trackingData.add(indent + plugin.getGroupId() + ":" + plugin.getArtifactId() + ":" + plugin.getVersion());
117-
indent += " ";
117+
indent.append(" ");
118118

119119
InputLocation location = plugin.getLocation("");
120120
if (location != null && location.getSource() != null) {
121121
trackingData.add(indent + location.getSource().getModelId() + " (implicit)");
122-
indent += " ";
122+
indent.append(" ");
123123
}
124124
} else if (collectStepTrace != null) {
125125
if (collectStepTrace.getPath().get(0).getArtifact() == null) {
@@ -138,15 +138,15 @@ public void artifactResolved(RepositoryEvent event) {
138138
if (isInScope(resolvedArtifact, nodeArtifact) || "pom".equals(resolvedArtifact.getExtension())) {
139139
Dependency node = collectStepTrace.getNode();
140140
trackingData.add(resolvedArtifact.toString());
141-
indent += " ";
142-
trackingData.add(indent + node + " (" + collectStepTrace.getContext() + ")");
141+
indent.append(" ");
142+
trackingData.add(indent.toString() + node + " (" + collectStepTrace.getContext() + ")");
143143
ListIterator<DependencyNode> iter = collectStepTrace
144144
.getPath()
145145
.listIterator(collectStepTrace.getPath().size());
146146
while (iter.hasPrevious()) {
147147
DependencyNode curr = iter.previous();
148-
indent += " ";
149-
trackingData.add(indent + curr + " (" + collectStepTrace.getContext() + ")");
148+
indent.append(" ");
149+
trackingData.add(indent.toString() + curr + " (" + collectStepTrace.getContext() + ")");
150150
}
151151
}
152152
}

impl/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoDescriptorCreator.java

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,9 @@ public MojoDescriptor getMojoDescriptor(String task, MavenSession session, Maven
138138
throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
139139
MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
140140
PluginVersionResolutionException {
141-
String goal = null;
141+
StringBuilder goal = new StringBuilder();
142142

143-
Plugin plugin = null;
143+
Plugin plugin;
144144

145145
String[] tok = task.split(":");
146146

@@ -160,11 +160,11 @@ public MojoDescriptor getMojoDescriptor(String task, MavenSession session, Maven
160160
plugin.setGroupId(tok[0]);
161161
plugin.setArtifactId(tok[1]);
162162
plugin.setVersion(tok[2]);
163-
goal = tok[3];
163+
goal.append(tok[3]);
164164

165165
// This won't be valid, but it constructs something easy to read in the error message
166166
for (int idx = 4; idx < tok.length; idx++) {
167-
goal += ":" + tok[idx];
167+
goal.append(":").append(tok[idx]);
168168
}
169169
} else if (numTokens == 3) {
170170
// groupId:artifactId:goal or pluginPrefix:version:goal (since Maven 3.9.0)
@@ -189,7 +189,7 @@ public MojoDescriptor getMojoDescriptor(String task, MavenSession session, Maven
189189
plugin = findPluginForPrefix(firstToken, session);
190190
plugin.setVersion(tok[1]);
191191
}
192-
goal = tok[2];
192+
goal.append(tok[2]);
193193
} else {
194194
// We have a prefix and goal
195195
//
@@ -198,10 +198,7 @@ public MojoDescriptor getMojoDescriptor(String task, MavenSession session, Maven
198198
String prefix = tok[0];
199199

200200
if (numTokens == 2) {
201-
goal = tok[1];
202-
} else {
203-
// goal was missing - pass through to MojoNotFoundException
204-
goal = "";
201+
goal.append(tok[1]);
205202
}
206203

207204
// This is the case where someone has executed a single goal from the command line
@@ -216,9 +213,9 @@ public MojoDescriptor getMojoDescriptor(String task, MavenSession session, Maven
216213
plugin = findPluginForPrefix(prefix, session);
217214
}
218215

219-
int executionIdx = goal.indexOf('@');
216+
int executionIdx = goal.indexOf("@");
220217
if (executionIdx > 0) {
221-
goal = goal.substring(0, executionIdx);
218+
goal.setLength(executionIdx);
222219
}
223220

224221
injectPluginDeclarationFromProject(plugin, project);
@@ -231,7 +228,7 @@ public MojoDescriptor getMojoDescriptor(String task, MavenSession session, Maven
231228
}
232229

233230
return pluginManager.getMojoDescriptor(
234-
plugin, goal, project.getRemotePluginRepositories(), session.getRepositorySession());
231+
plugin, goal.toString(), project.getRemotePluginRepositories(), session.getRepositorySession());
235232
}
236233

237234
// TODO take repo mans into account as one may be aggregating prefixes of many

0 commit comments

Comments
 (0)