Skip to content

Commit

Permalink
Use StringBuilder.append(char) where possible
Browse files Browse the repository at this point in the history
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 spring-projectsgh-27098
  • Loading branch information
sbrannen authored and lxbzmy committed Mar 26, 2022
1 parent e064372 commit 06f5393
Show file tree
Hide file tree
Showing 74 changed files with 232 additions and 220 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -625,7 +625,7 @@ private PointcutBody getPointcutBody(String[] tokens, int startIndex) {
StringBuilder sb = new StringBuilder();
if (bodyStart >= 0 && bodyStart != (currentToken.length() - 1)) {
sb.append(currentToken.substring(bodyStart + 1));
sb.append(" ");
sb.append(' ');
}
numTokensConsumed++;
int currentIndex = startIndex + numTokensConsumed;
Expand All @@ -645,7 +645,7 @@ private PointcutBody getPointcutBody(String[] tokens, int startIndex) {
toAppend = toAppend.substring(1);
}
sb.append(toAppend);
sb.append(" ");
sb.append(' ');
currentIndex++;
numTokensConsumed++;
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -547,7 +547,7 @@ public String toString() {
StringBuilder sb = new StringBuilder("AspectJExpressionPointcut: (");
for (int i = 0; i < this.pointcutParameterTypes.length; i++) {
sb.append(this.pointcutParameterTypes[i].getName());
sb.append(" ");
sb.append(' ');
sb.append(this.pointcutParameterNames[i]);
if ((i+1) < this.pointcutParameterTypes.length) {
sb.append(", ");
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -255,19 +255,19 @@ private String toString(boolean includeModifier, boolean includeReturnTypeAndArg
StringBuilder sb = new StringBuilder();
if (includeModifier) {
sb.append(Modifier.toString(getModifiers()));
sb.append(" ");
sb.append(' ');
}
if (includeReturnTypeAndArgs) {
appendType(sb, getReturnType(), useLongReturnAndArgumentTypeName);
sb.append(" ");
sb.append(' ');
}
appendType(sb, getDeclaringType(), useLongTypeName);
sb.append(".");
sb.append('.');
sb.append(getMethod().getName());
sb.append("(");
sb.append('(');
Class<?>[] parametersTypes = getParameterTypes();
appendTypes(sb, parametersTypes, includeReturnTypeAndArgs, useLongReturnAndArgumentTypeName);
sb.append(")");
sb.append(')');
return sb.toString();
}

Expand All @@ -278,7 +278,7 @@ private void appendTypes(StringBuilder sb, Class<?>[] types, boolean includeArgs
for (int size = types.length, i = 0; i < size; i++) {
appendType(sb, types[i], useLongReturnAndArgumentTypeName);
if (i < size - 1) {
sb.append(",");
sb.append(',');
}
}
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -137,7 +137,7 @@ public String toString() {
StringBuilder sb = new StringBuilder(getClass().getName());
sb.append(": advice ");
if (this.adviceBeanName != null) {
sb.append("bean '").append(this.adviceBeanName).append("'");
sb.append("bean '").append(this.adviceBeanName).append('\'');
}
else {
sb.append(this.advice);
Expand Down
Expand Up @@ -191,9 +191,9 @@ public int hashCode() {
@Override
public String toString() {
StringBuilder sb = new StringBuilder(getClass().getSimpleName());
sb.append(" for target bean '").append(this.targetBeanName).append("'");
sb.append(" for target bean '").append(this.targetBeanName).append('\'');
if (this.targetClass != null) {
sb.append(" of type [").append(this.targetClass.getName()).append("]");
sb.append(" of type [").append(this.targetClass.getName()).append(']');
}
return sb.toString();
}
Expand Down
Expand Up @@ -276,14 +276,14 @@ protected void assertException(Method method, String pointcut, String returning,

private static String format(String[] names) {
StringBuilder sb = new StringBuilder();
sb.append("(");
sb.append('(');
for (int i = 0; i < names.length; i++) {
sb.append(names[i]);
if ((i + 1) < names.length) {
sb.append(",");
sb.append(',');
}
}
sb.append(")");
sb.append(')');
return sb.toString();
}

Expand Down
Expand Up @@ -247,14 +247,14 @@ else if (conversionService != null && typeDescriptor != null) {
// Definitely doesn't match: throw IllegalArgumentException/IllegalStateException
StringBuilder msg = new StringBuilder();
msg.append("Cannot convert value of type '").append(ClassUtils.getDescriptiveType(newValue));
msg.append("' to required type '").append(ClassUtils.getQualifiedName(requiredType)).append("'");
msg.append("' to required type '").append(ClassUtils.getQualifiedName(requiredType)).append('\'');
if (propertyName != null) {
msg.append(" for property '").append(propertyName).append("'");
msg.append(" for property '").append(propertyName).append('\'');
}
if (editor != null) {
msg.append(": PropertyEditor [").append(editor.getClass().getName()).append(
"] returned inappropriate value of type '").append(
ClassUtils.getDescriptiveType(convertedValue)).append("'");
ClassUtils.getDescriptiveType(convertedValue)).append('\'');
throw new IllegalArgumentException(msg.toString());
}
else {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -217,13 +217,13 @@ private String buildExceptionMessage(List<String> invalidProperties, String bean
sb.append(" and");
}
else {
sb.append(",");
sb.append(',');
}
}
sb.append(" '").append(propertyName).append("'");
sb.append(" '").append(propertyName).append('\'');
}
sb.append(size == 1 ? " is" : " are");
sb.append(" required for bean '").append(beanName).append("'");
sb.append(" required for bean '").append(beanName).append('\'');
return sb.toString();
}

Expand Down
Expand Up @@ -1241,7 +1241,7 @@ public int hashCode() {
@Override
public String toString() {
StringBuilder sb = new StringBuilder("class [");
sb.append(getBeanClassName()).append("]");
sb.append(getBeanClassName()).append(']');
sb.append("; scope=").append(this.scope);
sb.append("; abstract=").append(this.abstractFlag);
sb.append("; lazyInit=").append(this.lazyInit);
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -138,7 +138,7 @@ protected ExceptionTypeFilter createExceptionTypeFilter(

@Override
public String toString() {
return getOperationDescription().append("]").toString();
return getOperationDescription().append(']').toString();
}

/**
Expand All @@ -148,7 +148,7 @@ public String toString() {
protected StringBuilder getOperationDescription() {
StringBuilder result = new StringBuilder();
result.append(getClass().getSimpleName());
result.append("[");
result.append('[');
result.append(this.methodDetails);
return result;
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -71,9 +71,9 @@ public void setBeforeInvocation(boolean beforeInvocation) {
@Override
protected StringBuilder getOperationDescription() {
StringBuilder sb = super.getOperationDescription();
sb.append(",");
sb.append(',');
sb.append(this.cacheWide);
sb.append(",");
sb.append(',');
sb.append(this.beforeInvocation);
return sb;
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -216,13 +216,13 @@ public void setCondition(String condition) {
*/
protected StringBuilder getOperationDescription() {
StringBuilder result = new StringBuilder(getClass().getSimpleName());
result.append("[").append(this.name);
result.append('[').append(this.name);
result.append("] caches=").append(this.cacheNames);
result.append(" | key='").append(this.key);
result.append("' | keyGenerator='").append(this.keyGenerator);
result.append("' | cacheManager='").append(this.cacheManager);
result.append("' | cacheResolver='").append(this.cacheResolver);
result.append("' | condition='").append(this.condition).append("'");
result.append("' | condition='").append(this.condition).append('\'');
return result;
}

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -66,7 +66,7 @@ protected StringBuilder getOperationDescription() {
StringBuilder sb = super.getOperationDescription();
sb.append(" | unless='");
sb.append(this.unless);
sb.append("'");
sb.append('\'');
return sb;
}

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -79,10 +79,10 @@ protected StringBuilder getOperationDescription() {
StringBuilder sb = super.getOperationDescription();
sb.append(" | unless='");
sb.append(this.unless);
sb.append("'");
sb.append('\'');
sb.append(" | sync='");
sb.append(this.sync);
sb.append("'");
sb.append('\'');
return sb;
}

Expand Down
Expand Up @@ -396,7 +396,7 @@ protected String getCondition() {
* @param message error message to append the HandlerMethod details to
*/
protected String getDetailedErrorMessage(Object bean, String message) {
StringBuilder sb = new StringBuilder(message).append("\n");
StringBuilder sb = new StringBuilder(message).append('\n');
sb.append("HandlerMethod details: \n");
sb.append("Bean [").append(bean.getClass().getName()).append("]\n");
sb.append("Method [").append(this.method.toGenericString()).append("]\n");
Expand Down Expand Up @@ -426,7 +426,7 @@ private String getInvocationErrorMessage(Object bean, String message, Object[] r
StringBuilder sb = new StringBuilder(getDetailedErrorMessage(bean, message));
sb.append("Resolved arguments: \n");
for (int i = 0; i < resolvedArgs.length; i++) {
sb.append("[").append(i).append("] ");
sb.append('[').append(i).append("] ");
if (resolvedArgs[i] == null) {
sb.append("[null] \n");
}
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -205,12 +205,12 @@ protected String generateJson(Set<ConfigurableApplicationContext> contexts) {
}
}
result.append("]\n");
result.append("}");
result.append('}');
if (it.hasNext()) {
result.append(",\n");
}
}
result.append("]");
result.append(']');
return result.toString();
}

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -324,7 +324,7 @@ public String propertyToConstantNamePrefix(String propertyName) {
for (int i = 0; i < propertyName.length(); i++) {
char c = propertyName.charAt(i);
if (Character.isUpperCase(c)) {
parsedPrefix.append("_");
parsedPrefix.append('_');
parsedPrefix.append(c);
}
else {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -394,9 +394,11 @@ public String toString() {
sb.append(entry.getKey());
sb.append('=');
sb.append(valueToString(entry.getValue()));
sb.append(entries.hasNext() ? ", " : "");
if (entries.hasNext()) {
sb.append(", ");
}
}
sb.append("}");
sb.append('}');
return sb.toString();
}

Expand Down
Expand Up @@ -177,17 +177,17 @@ private int getValueHashCode(Object value) {
private String annotationToString() {
String string = this.string;
if (string == null) {
StringBuilder builder = new StringBuilder("@").append(this.type.getName()).append("(");
StringBuilder builder = new StringBuilder("@").append(this.type.getName()).append('(');
for (int i = 0; i < this.attributes.size(); i++) {
Method attribute = this.attributes.get(i);
if (i > 0) {
builder.append(", ");
}
builder.append(attribute.getName());
builder.append("=");
builder.append('=');
builder.append(toString(getAttributeValue(attribute)));
}
builder.append(")");
builder.append(')');
string = builder.toString();
this.string = string;
}
Expand All @@ -206,7 +206,7 @@ private String toString(Object value) {
}
builder.append(toString(Array.get(value, i)));
}
builder.append("]");
builder.append(']');
return builder.toString();
}
return String.valueOf(value);
Expand Down
Expand Up @@ -513,7 +513,7 @@ public int hashCode() {
public String toString() {
StringBuilder builder = new StringBuilder();
for (Annotation ann : getAnnotations()) {
builder.append("@").append(ann.annotationType().getName()).append(' ');
builder.append('@').append(ann.annotationType().getName()).append(' ');
}
builder.append(getResolvableType());
return builder.toString();
Expand Down

0 comments on commit 06f5393

Please sign in to comment.