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
This commit is contained in:
Sam Brannen
2021-06-24 18:53:51 +02:00
parent ddbb7c1b5b
commit a2ef6badc4
74 changed files with 232 additions and 220 deletions

View File

@@ -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.
@@ -137,7 +137,7 @@ public class ExpressionException extends RuntimeException {
StringBuilder output = new StringBuilder();
output.append("Expression [");
output.append(this.expressionString);
output.append("]");
output.append(']');
if (this.position >= 0) {
output.append(" @");
output.append(this.position);

View File

@@ -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.
@@ -432,11 +432,11 @@ public class CodeFlow implements Opcodes {
public static String createSignatureDescriptor(Method method) {
Class<?>[] params = method.getParameterTypes();
StringBuilder sb = new StringBuilder();
sb.append("(");
sb.append('(');
for (Class<?> param : params) {
sb.append(toJvmDescriptor(param));
}
sb.append(")");
sb.append(')');
sb.append(toJvmDescriptor(method.getReturnType()));
return sb.toString();
}
@@ -453,7 +453,7 @@ public class CodeFlow implements Opcodes {
public static String createSignatureDescriptor(Constructor<?> ctor) {
Class<?>[] params = ctor.getParameterTypes();
StringBuilder sb = new StringBuilder();
sb.append("(");
sb.append('(');
for (Class<?> param : params) {
sb.append(toJvmDescriptor(param));
}
@@ -473,7 +473,7 @@ public class CodeFlow implements Opcodes {
StringBuilder sb = new StringBuilder();
if (clazz.isArray()) {
while (clazz.isArray()) {
sb.append("[");
sb.append('[');
clazz = clazz.getComponentType();
}
}
@@ -507,9 +507,9 @@ public class CodeFlow implements Opcodes {
}
}
else {
sb.append("L");
sb.append('L');
sb.append(clazz.getName().replace('.', '/'));
sb.append(";");
sb.append(';');
}
return sb.toString();
}

View File

@@ -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.
@@ -284,7 +284,7 @@ public enum SpelMessage {
formattedMessage.append("EL").append(this.code);
switch (this.kind) {
case ERROR:
formattedMessage.append("E");
formattedMessage.append('E');
break;
}
formattedMessage.append(": ");

View File

@@ -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.
@@ -64,13 +64,13 @@ public class BeanReference extends SpelNodeImpl {
public String toStringAST() {
StringBuilder sb = new StringBuilder();
if (!this.beanName.startsWith(FACTORY_BEAN_PREFIX)) {
sb.append("@");
sb.append('@');
}
if (!this.beanName.contains(".")) {
sb.append(this.beanName);
}
else {
sb.append("'").append(this.beanName).append("'");
sb.append('\'').append(this.beanName).append('\'');
}
return sb.toString();
}

View File

@@ -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.
@@ -208,14 +208,14 @@ public class ConstructorReference extends SpelNodeImpl {
StringBuilder sb = new StringBuilder("new ");
int index = 0;
sb.append(getChild(index++).toStringAST());
sb.append("(");
sb.append('(');
for (int i = index; i < getChildCount(); i++) {
if (i > index) {
sb.append(",");
sb.append(',');
}
sb.append(getChild(i).toStringAST());
}
sb.append(")");
sb.append(')');
return sb.toString();
}

View File

@@ -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.
@@ -140,13 +140,13 @@ public class InlineMap extends SpelNodeImpl {
int count = getChildCount();
for (int c = 0; c < count; c++) {
if (c > 0) {
sb.append(",");
sb.append(',');
}
sb.append(getChild(c++).toStringAST());
sb.append(":");
sb.append(':');
sb.append(getChild(c).toStringAST());
}
sb.append("}");
sb.append('}');
return sb.toString();
}

View File

@@ -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.
@@ -81,10 +81,10 @@ public abstract class Operator extends SpelNodeImpl {
StringBuilder sb = new StringBuilder("(");
sb.append(getChild(0).toStringAST());
for (int i = 1; i < getChildCount(); i++) {
sb.append(" ").append(getOperatorName()).append(" ");
sb.append(' ').append(getOperatorName()).append(' ');
sb.append(getChild(i).toStringAST());
}
sb.append(")");
sb.append(')');
return sb.toString();
}

View File

@@ -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.
@@ -49,7 +49,7 @@ public class QualifiedIdentifier extends SpelNodeImpl {
for (int i = 0; i < getChildCount(); i++) {
Object value = this.children[i].getValueInternal(state).getValue();
if (i > 0 && (value == null || !value.toString().startsWith("$"))) {
sb.append(".");
sb.append('.');
}
sb.append(value);
}
@@ -67,7 +67,7 @@ public class QualifiedIdentifier extends SpelNodeImpl {
else {
for (int i = 0; i < getChildCount(); i++) {
if (i > 0) {
sb.append(".");
sb.append('.');
}
sb.append(getChild(i).toStringAST());
}

View File

@@ -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.
@@ -90,7 +90,7 @@ public class TypeReference extends SpelNodeImpl {
for (int d = 0; d < this.dimensions; d++) {
sb.append("[]");
}
sb.append(")");
sb.append(')');
return sb.toString();
}

View File

@@ -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.
@@ -88,12 +88,12 @@ class Token {
@Override
public String toString() {
StringBuilder s = new StringBuilder();
s.append("[").append(this.kind.toString());
s.append('[').append(this.kind.toString());
if (this.kind.hasPayload()) {
s.append(":").append(this.data);
s.append(':').append(this.data);
}
s.append("]");
s.append("(").append(this.startPos).append(",").append(this.endPos).append(")");
s.append(']');
s.append('(').append(this.startPos).append(',').append(this.endPos).append(')');
return s.toString();
}

View File

@@ -245,22 +245,22 @@ public abstract class AbstractExpressionTests {
sb.append("int[").append(l.length).append("]{");
for (int j = 0; j < l.length; j++) {
if (j > 0) {
sb.append(",");
sb.append(',');
}
sb.append(stringValueOf(l[j]));
}
sb.append("}");
sb.append('}');
}
else if (primitiveType == Long.TYPE) {
long[] l = (long[]) value;
sb.append("long[").append(l.length).append("]{");
for (int j = 0; j < l.length; j++) {
if (j > 0) {
sb.append(",");
sb.append(',');
}
sb.append(stringValueOf(l[j]));
}
sb.append("}");
sb.append('}');
}
else {
throw new RuntimeException("Please implement support for type " + primitiveType.getName() +
@@ -272,32 +272,32 @@ public abstract class AbstractExpressionTests {
if (!isNested) {
sb.append(value.getClass().getComponentType().getName());
}
sb.append("[").append(l.size()).append("]{");
sb.append('[').append(l.size()).append("]{");
int i = 0;
for (Object object : l) {
if (i > 0) {
sb.append(",");
sb.append(',');
}
i++;
sb.append(stringValueOf(object, true));
}
sb.append("}");
sb.append('}');
}
else {
List<Object> l = Arrays.asList((Object[]) value);
if (!isNested) {
sb.append(value.getClass().getComponentType().getName());
}
sb.append("[").append(l.size()).append("]{");
sb.append('[').append(l.size()).append("]{");
int i = 0;
for (Object object : l) {
if (i > 0) {
sb.append(",");
sb.append(',');
}
i++;
sb.append(stringValueOf(object));
}
sb.append("}");
sb.append('}');
}
return sb.toString();
}

View File

@@ -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.
@@ -5111,21 +5111,21 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
List<?> ls = (List<?>) object;
for (Object l: ls) {
s.append(l);
s.append(" ");
s.append(' ');
}
}
else if (object instanceof Object[]) {
Object[] os = (Object[]) object;
for (Object o: os) {
s.append(o);
s.append(" ");
s.append(' ');
}
}
else if (object instanceof int[]) {
int[] is = (int[]) object;
for (int i: is) {
s.append(i);
s.append(" ");
s.append(' ');
}
}
else {
@@ -5931,9 +5931,9 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
public Obj3(String s, Float f, int... ints) {
StringBuilder b = new StringBuilder();
b.append(s);
b.append(":");
b.append(':');
b.append(Float.toString(f));
b.append(":");
b.append(':');
for (int param: ints) {
b.append(Integer.toString(param));
}