Skip to content
Permalink
Browse files
Avoid String concatenation in loops. Replace with StringBuilder.appen…
…d().
  • Loading branch information
arturobernalg committed Jun 20, 2021
1 parent bc1091e commit f90e61a2b498205ee1cd81c89e87b41746f60108
Showing 6 changed files with 40 additions and 49 deletions.
@@ -143,7 +143,7 @@ public String toGetSourceString( OgnlContext context, Object target )
{
try
{
String result = "";
StringBuilder result = new StringBuilder();
NodeType lastType = null;

// go through once to determine the ultimate type
@@ -183,7 +183,7 @@ && isWider( (NodeType) aChildren, lastType ) )
{
if ( i > 0 )
{
result += " " + getExpressionOperator( i ) + " ";
result.append(" ").append(getExpressionOperator(i)).append(" ");
}

String expr = children[i].toGetSourceString( context, target );
@@ -282,7 +282,7 @@ else if ( ( parent == null || !ASTChain.class.isInstance( parent ) )
}
}

result += expr;
result.append(expr);

// hanlde addition for numeric types when applicable or just string concatenation

@@ -298,10 +298,10 @@ else if ( ( parent == null || !ASTChain.class.isInstance( parent ) )
|| ASTProperty.class.isInstance( children[i] )
|| ASTChain.class.isInstance( children[i] ) )
{
result += ".";
result.append(".");
}

result += OgnlRuntime.getNumericValueGetter( context.getCurrentType() );
result.append(OgnlRuntime.getNumericValueGetter(context.getCurrentType()));
context.setCurrentType( OgnlRuntime.getPrimitiveWrapperClass( context.getCurrentType() ) );
}
}
@@ -335,7 +335,7 @@ else if ( ( parent == null || !ASTChain.class.isInstance( parent ) )
throw OgnlOps.castToRuntime( t );
}

return result;
return result.toString();

}
catch ( Throwable t )
@@ -142,7 +142,7 @@ protected Object getValueBody( OgnlContext context, Object source )

public String toGetSourceString( OgnlContext context, Object target )
{
String result = "new " + className;
StringBuilder result = new StringBuilder("new " + className);

Class clazz = null;
Object ctorValue = null;
@@ -179,30 +179,28 @@ public String toGetSourceString( OgnlContext context, Object target )
if ( children[0] instanceof ASTConst )
{

result = result + "[" + children[0].toGetSourceString( context, target ) + "]";
result.append("[").append(children[0].toGetSourceString(context, target)).append("]");
}
else if ( ASTProperty.class.isInstance( children[0] ) )
{

result =
result + "[" + ExpressionCompiler.getRootExpression( children[0], target, context )
+ children[0].toGetSourceString( context, target ) + "]";
result.append("[").append(ExpressionCompiler.getRootExpression(children[0], target, context)).append(children[0].toGetSourceString(context, target)).append("]");
}
else if ( ASTChain.class.isInstance( children[0] ) )
{

result = result + "[" + children[0].toGetSourceString( context, target ) + "]";
result.append("[").append(children[0].toGetSourceString(context, target)).append("]");
}
else
{

result = result + "[] " + children[0].toGetSourceString( context, target );
result.append("[] ").append(children[0].toGetSourceString(context, target));
}

}
else
{
result = result + "(";
result.append("(");

if ( ( children != null ) && ( children.length > 0 ) )
{
@@ -284,7 +282,7 @@ else if ( ASTChain.class.isInstance( children[0] ) )
{
if ( i > 0 )
{
result = result + ", ";
result.append(", ");
}

String value = expressions[i];
@@ -329,11 +327,11 @@ else if ( !ASTConst.class.isInstance( children[i] )
}
}

result += value;
result.append(value);
}

}
result = result + ")";
result.append(")");
}

context.setCurrentType( ctorValue != null ? ctorValue.getClass() : clazz );
@@ -348,7 +346,7 @@ else if ( !ASTConst.class.isInstance( children[i] )

context.remove( "_ctorClass" );

return result;
return result.toString();
}

public String toSetSourceString( OgnlContext context, Object target )
@@ -66,14 +66,8 @@ public Class getSetterClass()

public String toGetSourceString( OgnlContext context, Object target )
{
String result = "";
boolean array = false;

if ( parent != null && ASTCtor.class.isInstance( parent ) && ( (ASTCtor) parent ).isArray() )
{

array = true;
}
StringBuilder result = new StringBuilder();
boolean array = ASTCtor.class.isInstance(parent) && ((ASTCtor) parent).isArray();

context.setCurrentType( List.class );
context.setCurrentAccessor( List.class );
@@ -84,10 +78,10 @@ public String toGetSourceString( OgnlContext context, Object target )
{
return "java.util.Arrays.asList( new Object[0])";
}
result += "java.util.Arrays.asList( new Object[] ";
result.append("java.util.Arrays.asList( new Object[] ");
}

result += "{ ";
result.append("{ ");

try
{
@@ -96,7 +90,7 @@ public String toGetSourceString( OgnlContext context, Object target )
{
if ( i > 0 )
{
result = result + ", ";
result.append(", ");
}

Class prevType = context.getCurrentType();
@@ -194,7 +188,7 @@ else if ( ctorClass == null || !ctorClass.isPrimitive() )
{
value = "null";
}
result += value;
result.append(value);
}

}
@@ -206,13 +200,13 @@ else if ( ctorClass == null || !ctorClass.isPrimitive() )
context.setCurrentType( List.class );
context.setCurrentAccessor( List.class );

result += "}";
result.append("}");

if ( !array )
{
result += ")";
result.append(")");
}
return result;
return result.toString();
}

public String toSetSourceString( OgnlContext context, Object target )
@@ -274,7 +274,7 @@ public String toSetSourceString( OgnlContext context, Object target )
}

String post = "";
String result = "." + method.getName() + "(";
StringBuilder result = new StringBuilder("." + method.getName() + "(");

if ( method.getReturnType() != void.class && method.getReturnType().isPrimitive() && ( parent == null
|| !ASTTest.class.isInstance( parent ) ) )
@@ -310,7 +310,7 @@ public String toSetSourceString( OgnlContext context, Object target )
{
if ( i > 0 )
{
result += ", ";
result.append(", ");
}

Class prevType = context.getCurrentType();
@@ -373,7 +373,7 @@ public String toSetSourceString( OgnlContext context, Object target )
ASTMethodUtil.getParmString( context, parms[i], parmString, child, valueClass, ".class)" );
}

result += parmString;
result.append(parmString);
}

if ( prevCast != null )
@@ -84,7 +84,7 @@ public Class getSetterClass()

public String toGetSourceString( OgnlContext context, Object target )
{
String result = className + "#" + methodName + "(";
StringBuilder result = new StringBuilder(className + "#" + methodName + "(");

try
{
@@ -113,7 +113,7 @@ public String toGetSourceString( OgnlContext context, Object target )
{
if ( i > 0 )
{
result = result + ", ";
result.append(", ");
}

Class prevType = context.getCurrentType();
@@ -162,11 +162,11 @@ else if ( valueClass.isPrimitive() )
}
}

result += parmString;
result.append(parmString);
}
}

result += ")";
result.append(")");

try
{
@@ -192,7 +192,7 @@ else if ( valueClass.isPrimitive() )
throw OgnlOps.castToRuntime( t );
}

return result;
return result.toString();
}

public String toSetSourceString( OgnlContext context, Object target )
@@ -277,33 +277,32 @@ public void reset()
*/
public String toString( boolean compact, boolean showChildren, String depth )
{
String stringResult;
StringBuilder stringResult;

if ( compact )
{
stringResult = depth + "<" + node.getClass().getName() + " " + System.identityHashCode( this ) + ">";
stringResult = new StringBuilder(depth + "<" + node.getClass().getName() + " " + System.identityHashCode(this) + ">");
}
else
{
String ss = ( source != null ) ? source.getClass().getName() : "null", rs =
( result != null ) ? result.getClass().getName() : "null";

stringResult =
depth + "<" + node.getClass().getName() + ": [" + ( setOperation ? "set" : "get" ) + "] source = " + ss
+ ", result = " + result + " [" + rs + "]>";
stringResult = new StringBuilder(depth + "<" + node.getClass().getName() + ": [" + (setOperation ? "set" : "get") + "] source = " + ss
+ ", result = " + result + " [" + rs + "]>");
}
if ( showChildren )
{
Evaluation child = firstChild;

stringResult += "\n";
stringResult.append("\n");
while ( child != null )
{
stringResult += child.toString( compact, depth + " " );
stringResult.append(child.toString(compact, depth + " "));
child = child.next;
}
}
return stringResult;
return stringResult.toString();
}

/**

0 comments on commit f90e61a

Please sign in to comment.