Skip to content

Commit a2ef6ba

Browse files
committed
Use StringBuilder.append(char) where possible
To slightly improve performance, this commit switches to StringBuilder.append(char) instead of StringBuilder.append(String) whenever we append a single character to a StringBuilder. Closes gh-27098
1 parent ddbb7c1 commit a2ef6ba

File tree

74 files changed

+232
-220
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

74 files changed

+232
-220
lines changed

spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJAdviceParameterNameDiscoverer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -625,7 +625,7 @@ private PointcutBody getPointcutBody(String[] tokens, int startIndex) {
625625
StringBuilder sb = new StringBuilder();
626626
if (bodyStart >= 0 && bodyStart != (currentToken.length() - 1)) {
627627
sb.append(currentToken.substring(bodyStart + 1));
628-
sb.append(" ");
628+
sb.append(' ');
629629
}
630630
numTokensConsumed++;
631631
int currentIndex = startIndex + numTokensConsumed;
@@ -645,7 +645,7 @@ private PointcutBody getPointcutBody(String[] tokens, int startIndex) {
645645
toAppend = toAppend.substring(1);
646646
}
647647
sb.append(toAppend);
648-
sb.append(" ");
648+
sb.append(' ');
649649
currentIndex++;
650650
numTokensConsumed++;
651651
}

spring-aop/src/main/java/org/springframework/aop/aspectj/AspectJExpressionPointcut.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -547,7 +547,7 @@ public String toString() {
547547
StringBuilder sb = new StringBuilder("AspectJExpressionPointcut: (");
548548
for (int i = 0; i < this.pointcutParameterTypes.length; i++) {
549549
sb.append(this.pointcutParameterTypes[i].getName());
550-
sb.append(" ");
550+
sb.append(' ');
551551
sb.append(this.pointcutParameterNames[i]);
552552
if ((i+1) < this.pointcutParameterTypes.length) {
553553
sb.append(", ");

spring-aop/src/main/java/org/springframework/aop/aspectj/MethodInvocationProceedingJoinPoint.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -255,19 +255,19 @@ private String toString(boolean includeModifier, boolean includeReturnTypeAndArg
255255
StringBuilder sb = new StringBuilder();
256256
if (includeModifier) {
257257
sb.append(Modifier.toString(getModifiers()));
258-
sb.append(" ");
258+
sb.append(' ');
259259
}
260260
if (includeReturnTypeAndArgs) {
261261
appendType(sb, getReturnType(), useLongReturnAndArgumentTypeName);
262-
sb.append(" ");
262+
sb.append(' ');
263263
}
264264
appendType(sb, getDeclaringType(), useLongTypeName);
265-
sb.append(".");
265+
sb.append('.');
266266
sb.append(getMethod().getName());
267-
sb.append("(");
267+
sb.append('(');
268268
Class<?>[] parametersTypes = getParameterTypes();
269269
appendTypes(sb, parametersTypes, includeReturnTypeAndArgs, useLongReturnAndArgumentTypeName);
270-
sb.append(")");
270+
sb.append(')');
271271
return sb.toString();
272272
}
273273

@@ -278,7 +278,7 @@ private void appendTypes(StringBuilder sb, Class<?>[] types, boolean includeArgs
278278
for (int size = types.length, i = 0; i < size; i++) {
279279
appendType(sb, types[i], useLongReturnAndArgumentTypeName);
280280
if (i < size - 1) {
281-
sb.append(",");
281+
sb.append(',');
282282
}
283283
}
284284
}

spring-aop/src/main/java/org/springframework/aop/support/AbstractBeanFactoryPointcutAdvisor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -137,7 +137,7 @@ public String toString() {
137137
StringBuilder sb = new StringBuilder(getClass().getName());
138138
sb.append(": advice ");
139139
if (this.adviceBeanName != null) {
140-
sb.append("bean '").append(this.adviceBeanName).append("'");
140+
sb.append("bean '").append(this.adviceBeanName).append('\'');
141141
}
142142
else {
143143
sb.append(this.advice);

spring-aop/src/main/java/org/springframework/aop/target/AbstractBeanFactoryBasedTargetSource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,9 @@ public int hashCode() {
191191
@Override
192192
public String toString() {
193193
StringBuilder sb = new StringBuilder(getClass().getSimpleName());
194-
sb.append(" for target bean '").append(this.targetBeanName).append("'");
194+
sb.append(" for target bean '").append(this.targetBeanName).append('\'');
195195
if (this.targetClass != null) {
196-
sb.append(" of type [").append(this.targetClass.getName()).append("]");
196+
sb.append(" of type [").append(this.targetClass.getName()).append(']');
197197
}
198198
return sb.toString();
199199
}

spring-aop/src/test/java/org/springframework/aop/aspectj/AspectJAdviceParameterNameDiscovererTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,14 +276,14 @@ protected void assertException(Method method, String pointcut, String returning,
276276

277277
private static String format(String[] names) {
278278
StringBuilder sb = new StringBuilder();
279-
sb.append("(");
279+
sb.append('(');
280280
for (int i = 0; i < names.length; i++) {
281281
sb.append(names[i]);
282282
if ((i + 1) < names.length) {
283-
sb.append(",");
283+
sb.append(',');
284284
}
285285
}
286-
sb.append(")");
286+
sb.append(')');
287287
return sb.toString();
288288
}
289289

spring-beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,14 +247,14 @@ else if (conversionService != null && typeDescriptor != null) {
247247
// Definitely doesn't match: throw IllegalArgumentException/IllegalStateException
248248
StringBuilder msg = new StringBuilder();
249249
msg.append("Cannot convert value of type '").append(ClassUtils.getDescriptiveType(newValue));
250-
msg.append("' to required type '").append(ClassUtils.getQualifiedName(requiredType)).append("'");
250+
msg.append("' to required type '").append(ClassUtils.getQualifiedName(requiredType)).append('\'');
251251
if (propertyName != null) {
252-
msg.append(" for property '").append(propertyName).append("'");
252+
msg.append(" for property '").append(propertyName).append('\'');
253253
}
254254
if (editor != null) {
255255
msg.append(": PropertyEditor [").append(editor.getClass().getName()).append(
256256
"] returned inappropriate value of type '").append(
257-
ClassUtils.getDescriptiveType(convertedValue)).append("'");
257+
ClassUtils.getDescriptiveType(convertedValue)).append('\'');
258258
throw new IllegalArgumentException(msg.toString());
259259
}
260260
else {

spring-beans/src/main/java/org/springframework/beans/factory/annotation/RequiredAnnotationBeanPostProcessor.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -217,13 +217,13 @@ private String buildExceptionMessage(List<String> invalidProperties, String bean
217217
sb.append(" and");
218218
}
219219
else {
220-
sb.append(",");
220+
sb.append(',');
221221
}
222222
}
223-
sb.append(" '").append(propertyName).append("'");
223+
sb.append(" '").append(propertyName).append('\'');
224224
}
225225
sb.append(size == 1 ? " is" : " are");
226-
sb.append(" required for bean '").append(beanName).append("'");
226+
sb.append(" required for bean '").append(beanName).append('\'');
227227
return sb.toString();
228228
}
229229

spring-beans/src/main/java/org/springframework/beans/factory/support/AbstractBeanDefinition.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1241,7 +1241,7 @@ public int hashCode() {
12411241
@Override
12421242
public String toString() {
12431243
StringBuilder sb = new StringBuilder("class [");
1244-
sb.append(getBeanClassName()).append("]");
1244+
sb.append(getBeanClassName()).append(']');
12451245
sb.append("; scope=").append(this.scope);
12461246
sb.append("; abstract=").append(this.abstractFlag);
12471247
sb.append("; lazyInit=").append(this.lazyInit);

spring-context-support/src/main/java/org/springframework/cache/jcache/interceptor/AbstractJCacheOperation.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -138,7 +138,7 @@ protected ExceptionTypeFilter createExceptionTypeFilter(
138138

139139
@Override
140140
public String toString() {
141-
return getOperationDescription().append("]").toString();
141+
return getOperationDescription().append(']').toString();
142142
}
143143

144144
/**
@@ -148,7 +148,7 @@ public String toString() {
148148
protected StringBuilder getOperationDescription() {
149149
StringBuilder result = new StringBuilder();
150150
result.append(getClass().getSimpleName());
151-
result.append("[");
151+
result.append('[');
152152
result.append(this.methodDetails);
153153
return result;
154154
}

spring-context/src/main/java/org/springframework/cache/interceptor/CacheEvictOperation.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -71,9 +71,9 @@ public void setBeforeInvocation(boolean beforeInvocation) {
7171
@Override
7272
protected StringBuilder getOperationDescription() {
7373
StringBuilder sb = super.getOperationDescription();
74-
sb.append(",");
74+
sb.append(',');
7575
sb.append(this.cacheWide);
76-
sb.append(",");
76+
sb.append(',');
7777
sb.append(this.beforeInvocation);
7878
return sb;
7979
}

spring-context/src/main/java/org/springframework/cache/interceptor/CacheOperation.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -216,13 +216,13 @@ public void setCondition(String condition) {
216216
*/
217217
protected StringBuilder getOperationDescription() {
218218
StringBuilder result = new StringBuilder(getClass().getSimpleName());
219-
result.append("[").append(this.name);
219+
result.append('[').append(this.name);
220220
result.append("] caches=").append(this.cacheNames);
221221
result.append(" | key='").append(this.key);
222222
result.append("' | keyGenerator='").append(this.keyGenerator);
223223
result.append("' | cacheManager='").append(this.cacheManager);
224224
result.append("' | cacheResolver='").append(this.cacheResolver);
225-
result.append("' | condition='").append(this.condition).append("'");
225+
result.append("' | condition='").append(this.condition).append('\'');
226226
return result;
227227
}
228228

spring-context/src/main/java/org/springframework/cache/interceptor/CachePutOperation.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2021 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,7 +66,7 @@ protected StringBuilder getOperationDescription() {
6666
StringBuilder sb = super.getOperationDescription();
6767
sb.append(" | unless='");
6868
sb.append(this.unless);
69-
sb.append("'");
69+
sb.append('\'');
7070
return sb;
7171
}
7272

spring-context/src/main/java/org/springframework/cache/interceptor/CacheableOperation.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -79,10 +79,10 @@ protected StringBuilder getOperationDescription() {
7979
StringBuilder sb = super.getOperationDescription();
8080
sb.append(" | unless='");
8181
sb.append(this.unless);
82-
sb.append("'");
82+
sb.append('\'');
8383
sb.append(" | sync='");
8484
sb.append(this.sync);
85-
sb.append("'");
85+
sb.append('\'');
8686
return sb;
8787
}
8888

spring-context/src/main/java/org/springframework/context/event/ApplicationListenerMethodAdapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ protected String getCondition() {
396396
* @param message error message to append the HandlerMethod details to
397397
*/
398398
protected String getDetailedErrorMessage(Object bean, String message) {
399-
StringBuilder sb = new StringBuilder(message).append("\n");
399+
StringBuilder sb = new StringBuilder(message).append('\n');
400400
sb.append("HandlerMethod details: \n");
401401
sb.append("Bean [").append(bean.getClass().getName()).append("]\n");
402402
sb.append("Method [").append(this.method.toGenericString()).append("]\n");
@@ -426,7 +426,7 @@ private String getInvocationErrorMessage(Object bean, String message, Object[] r
426426
StringBuilder sb = new StringBuilder(getDetailedErrorMessage(bean, message));
427427
sb.append("Resolved arguments: \n");
428428
for (int i = 0; i < resolvedArgs.length; i++) {
429-
sb.append("[").append(i).append("] ");
429+
sb.append('[').append(i).append("] ");
430430
if (resolvedArgs[i] == null) {
431431
sb.append("[null] \n");
432432
}

spring-context/src/main/java/org/springframework/context/support/LiveBeansView.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -205,12 +205,12 @@ protected String generateJson(Set<ConfigurableApplicationContext> contexts) {
205205
}
206206
}
207207
result.append("]\n");
208-
result.append("}");
208+
result.append('}');
209209
if (it.hasNext()) {
210210
result.append(",\n");
211211
}
212212
}
213-
result.append("]");
213+
result.append(']');
214214
return result.toString();
215215
}
216216

spring-core/src/main/java/org/springframework/core/Constants.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -324,7 +324,7 @@ public String propertyToConstantNamePrefix(String propertyName) {
324324
for (int i = 0; i < propertyName.length(); i++) {
325325
char c = propertyName.charAt(i);
326326
if (Character.isUpperCase(c)) {
327-
parsedPrefix.append("_");
327+
parsedPrefix.append('_');
328328
parsedPrefix.append(c);
329329
}
330330
else {

spring-core/src/main/java/org/springframework/core/annotation/AnnotationAttributes.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -394,9 +394,11 @@ public String toString() {
394394
sb.append(entry.getKey());
395395
sb.append('=');
396396
sb.append(valueToString(entry.getValue()));
397-
sb.append(entries.hasNext() ? ", " : "");
397+
if (entries.hasNext()) {
398+
sb.append(", ");
399+
}
398400
}
399-
sb.append("}");
401+
sb.append('}');
400402
return sb.toString();
401403
}
402404

spring-core/src/main/java/org/springframework/core/annotation/SynthesizedMergedAnnotationInvocationHandler.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,17 +177,17 @@ private int getValueHashCode(Object value) {
177177
private String annotationToString() {
178178
String string = this.string;
179179
if (string == null) {
180-
StringBuilder builder = new StringBuilder("@").append(this.type.getName()).append("(");
180+
StringBuilder builder = new StringBuilder("@").append(this.type.getName()).append('(');
181181
for (int i = 0; i < this.attributes.size(); i++) {
182182
Method attribute = this.attributes.get(i);
183183
if (i > 0) {
184184
builder.append(", ");
185185
}
186186
builder.append(attribute.getName());
187-
builder.append("=");
187+
builder.append('=');
188188
builder.append(toString(getAttributeValue(attribute)));
189189
}
190-
builder.append(")");
190+
builder.append(')');
191191
string = builder.toString();
192192
this.string = string;
193193
}
@@ -206,7 +206,7 @@ private String toString(Object value) {
206206
}
207207
builder.append(toString(Array.get(value, i)));
208208
}
209-
builder.append("]");
209+
builder.append(']');
210210
return builder.toString();
211211
}
212212
return String.valueOf(value);

spring-core/src/main/java/org/springframework/core/convert/TypeDescriptor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ public int hashCode() {
513513
public String toString() {
514514
StringBuilder builder = new StringBuilder();
515515
for (Annotation ann : getAnnotations()) {
516-
builder.append("@").append(ann.annotationType().getName()).append(' ');
516+
builder.append('@').append(ann.annotationType().getName()).append(' ');
517517
}
518518
builder.append(getResolvableType());
519519
return builder.toString();

0 commit comments

Comments
 (0)