Skip to content

Commit 168d2f8

Browse files
committed
Merge pull request #15589 from igor-suhorukov
* pr/15589: Polish "Avoid string concatenation inside StringBuilder append()" Avoid string concatenation inside StringBuilder append()
2 parents 1b970b0 + 8d1d3fb commit 168d2f8

File tree

12 files changed

+56
-53
lines changed

12 files changed

+56
-53
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/jmx/annotation/DiscoveredJmxOperation.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -180,9 +180,9 @@ public String getDescription() {
180180
public String toString() {
181181
StringBuilder result = new StringBuilder(this.name);
182182
if (this.description != null) {
183-
result.append(" (" + this.description + ")");
183+
result.append(" (").append(this.description).append(")");
184184
}
185-
result.append(":" + this.type);
185+
result.append(":").append(this.type);
186186
return result.toString();
187187
}
188188

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/web/WebOperationRequestPredicate.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -124,12 +124,12 @@ public String toString() {
124124
StringBuilder result = new StringBuilder(
125125
this.httpMethod + " to path '" + this.path + "'");
126126
if (!CollectionUtils.isEmpty(this.consumes)) {
127-
result.append(" consumes: "
128-
+ StringUtils.collectionToCommaDelimitedString(this.consumes));
127+
result.append(" consumes: ")
128+
.append(StringUtils.collectionToCommaDelimitedString(this.consumes));
129129
}
130130
if (!CollectionUtils.isEmpty(this.produces)) {
131-
result.append(" produces: "
132-
+ StringUtils.collectionToCommaDelimitedString(this.produces));
131+
result.append(" produces: ")
132+
.append(StringUtils.collectionToCommaDelimitedString(this.produces));
133133
}
134134
return result.toString();
135135
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/condition/ConditionMessage.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -386,14 +386,14 @@ public ConditionMessage items(Style style, Collection<?> items) {
386386
items = style.applyTo(items);
387387
if ((this.condition == null || items.size() <= 1)
388388
&& StringUtils.hasLength(this.singular)) {
389-
message.append(" " + this.singular);
389+
message.append(" ").append(this.singular);
390390
}
391391
else if (StringUtils.hasLength(this.plural)) {
392-
message.append(" " + this.plural);
392+
message.append(" ").append(this.plural);
393393
}
394394
if (items != null && !items.isEmpty()) {
395-
message.append(
396-
" " + StringUtils.collectionToDelimitedString(items, ", "));
395+
message.append(" ")
396+
.append(StringUtils.collectionToDelimitedString(items, ", "));
397397
}
398398
return this.condition.because(message.toString());
399399
}

spring-boot-project/spring-boot-cli/src/main/java/org/springframework/boot/cli/command/init/ServiceCapabilitiesReportGenerator.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -66,9 +66,9 @@ public String generate(String url) throws IOException {
6666
private String generateHelp(String url, InitializrServiceMetadata metadata) {
6767
String header = "Capabilities of " + url;
6868
StringBuilder report = new StringBuilder();
69-
report.append(repeat("=", header.length()) + NEW_LINE);
70-
report.append(header + NEW_LINE);
71-
report.append(repeat("=", header.length()) + NEW_LINE);
69+
report.append(repeat("=", header.length())).append(NEW_LINE);
70+
report.append(header).append(NEW_LINE);
71+
report.append(repeat("=", header.length())).append(NEW_LINE);
7272
report.append(NEW_LINE);
7373
reportAvailableDependencies(metadata, report);
7474
report.append(NEW_LINE);
@@ -80,13 +80,13 @@ private String generateHelp(String url, InitializrServiceMetadata metadata) {
8080

8181
private void reportAvailableDependencies(InitializrServiceMetadata metadata,
8282
StringBuilder report) {
83-
report.append("Available dependencies:" + NEW_LINE);
84-
report.append("-----------------------" + NEW_LINE);
83+
report.append("Available dependencies:").append(NEW_LINE);
84+
report.append("-----------------------").append(NEW_LINE);
8585
List<Dependency> dependencies = getSortedDependencies(metadata);
8686
for (Dependency dependency : dependencies) {
87-
report.append(dependency.getId() + " - " + dependency.getName());
87+
report.append(dependency.getId()).append(" - ").append(dependency.getName());
8888
if (dependency.getDescription() != null) {
89-
report.append(": " + dependency.getDescription());
89+
report.append(": ").append(dependency.getDescription());
9090
}
9191
report.append(NEW_LINE);
9292
}
@@ -100,14 +100,14 @@ private List<Dependency> getSortedDependencies(InitializrServiceMetadata metadat
100100

101101
private void reportAvailableProjectTypes(InitializrServiceMetadata metadata,
102102
StringBuilder report) {
103-
report.append("Available project types:" + NEW_LINE);
104-
report.append("------------------------" + NEW_LINE);
103+
report.append("Available project types:").append(NEW_LINE);
104+
report.append("------------------------").append(NEW_LINE);
105105
SortedSet<Entry<String, ProjectType>> entries = new TreeSet<>(
106106
Comparator.comparing(Entry::getKey));
107107
entries.addAll(metadata.getProjectTypes().entrySet());
108108
for (Entry<String, ProjectType> entry : entries) {
109109
ProjectType type = entry.getValue();
110-
report.append(entry.getKey() + " - " + type.getName());
110+
report.append(entry.getKey()).append(" - ").append(type.getName());
111111
if (!type.getTags().isEmpty()) {
112112
reportTags(report, type);
113113
}
@@ -124,7 +124,7 @@ private void reportTags(StringBuilder report, ProjectType type) {
124124
report.append(" [");
125125
while (iterator.hasNext()) {
126126
Map.Entry<String, String> entry = iterator.next();
127-
report.append(entry.getKey() + ":" + entry.getValue());
127+
report.append(entry.getKey()).append(":").append(entry.getValue());
128128
if (iterator.hasNext()) {
129129
report.append(", ");
130130
}
@@ -134,13 +134,14 @@ private void reportTags(StringBuilder report, ProjectType type) {
134134

135135
private void reportDefaults(StringBuilder report,
136136
InitializrServiceMetadata metadata) {
137-
report.append("Defaults:" + NEW_LINE);
138-
report.append("---------" + NEW_LINE);
137+
report.append("Defaults:").append(NEW_LINE);
138+
report.append("---------").append(NEW_LINE);
139139
List<String> defaultsKeys = new ArrayList<>(metadata.getDefaults().keySet());
140140
Collections.sort(defaultsKeys);
141141
for (String defaultsKey : defaultsKeys) {
142142
String defaultsValue = metadata.getDefaults().get(defaultsKey);
143-
report.append(defaultsKey + ": " + defaultsValue + NEW_LINE);
143+
report.append(defaultsKey).append(": ").append(defaultsValue)
144+
.append(NEW_LINE);
144145
}
145146
}
146147

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/test/java/org/springframework/boot/configurationprocessor/metadata/Metadata.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -108,7 +108,7 @@ public MetadataItemCondition(ItemType itemType, String name, String type,
108108

109109
private String createDescription() {
110110
StringBuilder description = new StringBuilder();
111-
description.append("an item named '" + this.name + "'");
111+
description.append("an item named '").append(this.name).append("'");
112112
if (this.type != null) {
113113
description.append(" with dataType:").append(this.type);
114114
}
@@ -258,7 +258,7 @@ public MetadataHintCondition(String name,
258258

259259
private String createDescription() {
260260
StringBuilder description = new StringBuilder();
261-
description.append("a hints name '" + this.name + "'");
261+
description.append("a hints name '").append(this.name).append("'");
262262
if (!this.valueConditions.isEmpty()) {
263263
description.append(" with values:").append(this.valueConditions);
264264
}
@@ -348,7 +348,7 @@ private static class ItemHintValueCondition extends Condition<ItemHint> {
348348

349349
private String createDescription() {
350350
StringBuilder description = new StringBuilder();
351-
description.append("value hint at index '" + this.index + "'");
351+
description.append("value hint at index '").append(this.index).append("'");
352352
if (this.value != null) {
353353
description.append(" with value:").append(this.value);
354354
}

spring-boot-project/spring-boot-tools/spring-boot-gradle-plugin/src/main/java/org/springframework/boot/gradle/plugin/UnresolvedDependenciesAnalyzer.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ void buildFinished(Project project) {
5252
StringBuilder message = new StringBuilder();
5353
message.append("\nDuring the build, one or more dependencies that were "
5454
+ "declared without a version failed to resolve:\n");
55-
this.dependenciesWithNoVersion
56-
.forEach((dependency) -> message.append(" " + dependency + "\n"));
55+
this.dependenciesWithNoVersion.forEach((dependency) -> message.append(" ")
56+
.append(dependency).append("\n"));
5757
message.append("\nDid you forget to apply the "
58-
+ "io.spring.dependency-management plugin to the " + project.getName()
59-
+ " project?\n");
58+
+ "io.spring.dependency-management plugin to the ");
59+
message.append(project.getName()).append(" project?\n");
6060
logger.warn(message.toString());
6161
}
6262
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/StartupInfoLogger.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -88,7 +88,7 @@ private StringBuilder getStartedMessage(StopWatch stopWatch) {
8888
message.append(stopWatch.getTotalTimeSeconds());
8989
try {
9090
double uptime = ManagementFactory.getRuntimeMXBean().getUptime() / 1000.0;
91-
message.append(" seconds (JVM running for " + uptime + ")");
91+
message.append(" seconds (JVM running for ").append(uptime).append(")");
9292
}
9393
catch (Throwable ex) {
9494
// No JVM time available

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/ConfigurationPropertiesBindException.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -58,8 +58,8 @@ public ConfigurationProperties getAnnotation() {
5858

5959
private static String getMessage(Object bean, ConfigurationProperties annotation) {
6060
StringBuilder message = new StringBuilder();
61-
message.append("Could not bind properties to '"
62-
+ ClassUtils.getShortName(bean.getClass()) + "' : ");
61+
message.append("Could not bind properties to '");
62+
message.append(ClassUtils.getShortName(bean.getClass())).append("' : ");
6363
message.append("prefix=").append(annotation.prefix());
6464
message.append(", ignoreInvalidFields=").append(annotation.ignoreInvalidFields());
6565
message.append(", ignoreUnknownFields=").append(annotation.ignoreUnknownFields());

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/bind/validation/BindValidationException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -48,7 +48,7 @@ public ValidationErrors getValidationErrors() {
4848
private static String getMessage(ValidationErrors errors) {
4949
StringBuilder message = new StringBuilder("Binding validation errors");
5050
if (errors != null) {
51-
message.append(" on " + errors.getName());
51+
message.append(" on ").append(errors.getName());
5252
errors.getAllErrors().forEach(
5353
(error) -> message.append(String.format("%n - %s", error)));
5454
}

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/analyzer/InvalidConfigurationPropertyValueFailureAnalyzer.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -84,8 +84,9 @@ private void appendDetails(StringBuilder message,
8484
InvalidConfigurationPropertyValueException cause,
8585
List<Descriptor> descriptors) {
8686
Descriptor mainDescriptor = descriptors.get(0);
87-
message.append("Invalid value '" + mainDescriptor.getValue()
88-
+ "' for configuration property '" + cause.getName() + "'");
87+
message.append("Invalid value '").append(mainDescriptor.getValue())
88+
.append("' for configuration property '");
89+
message.append(cause.getName()).append("'");
8990
mainDescriptor.appendOrigin(message);
9091
message.append(".");
9192
}
@@ -111,8 +112,8 @@ private void appendAdditionalProperties(StringBuilder message,
111112
+ "property %s:%n%n",
112113
(others.size() > 1) ? "sources" : "source"));
113114
for (Descriptor other : others) {
114-
message.append("\t- In '" + other.getPropertySource() + "'");
115-
message.append(" with the value '" + other.getValue() + "'");
115+
message.append("\t- In '").append(other.getPropertySource()).append("'");
116+
message.append(" with the value '").append(other.getValue()).append("'");
116117
other.appendOrigin(message);
117118
message.append(String.format(".%n"));
118119
}
@@ -153,7 +154,7 @@ public Object getValue() {
153154

154155
public void appendOrigin(StringBuilder message) {
155156
if (this.origin != null) {
156-
message.append(" (originating from '" + this.origin + "')");
157+
message.append(" (originating from '").append(this.origin).append("')");
157158
}
158159
}
159160

0 commit comments

Comments
 (0)