Avoid regex pattern matching for simple String replacement steps

Issue: SPR-17279
This commit is contained in:
Juergen Hoeller
2018-09-17 14:22:19 +02:00
parent cc87fbcb7f
commit 34663300a6
13 changed files with 61 additions and 30 deletions

View File

@@ -19,6 +19,7 @@ package org.springframework.expression.spel.ast;
import org.springframework.asm.MethodVisitor;
import org.springframework.expression.TypedValue;
import org.springframework.expression.spel.CodeFlow;
import org.springframework.util.StringUtils;
/**
* Expression language AST node that represents a string literal.
@@ -33,9 +34,13 @@ public class StringLiteral extends Literal {
public StringLiteral(String payload, int pos, String value) {
super(payload,pos);
value = value.substring(1, value.length() - 1);
this.value = new TypedValue(value.replaceAll("''", "'").replaceAll("\"\"", "\""));
super(payload, pos);
String valueWithinQuotes = value.substring(1, value.length() - 1);
valueWithinQuotes = StringUtils.replace(valueWithinQuotes, "''", "'");
valueWithinQuotes = StringUtils.replace(valueWithinQuotes, "\"\"", "\"");
this.value = new TypedValue(valueWithinQuotes);
this.exitTypeDescriptor = "Ljava/lang/String";
}

View File

@@ -36,6 +36,7 @@ import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils;
import org.springframework.util.ConcurrentReferenceHashMap;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.StringUtils;
/**
* A SpelCompiler will take a regular parsed expression and create (and load) a class
@@ -132,9 +133,9 @@ public final class SpelCompiler implements Opcodes {
@Nullable
private Class<? extends CompiledExpression> createExpressionClass(SpelNodeImpl expressionToCompile) {
// Create class outline 'spel/ExNNN extends org.springframework.expression.spel.CompiledExpression'
String clazzName = "spel/Ex" + getNextSuffix();
String className = "spel/Ex" + getNextSuffix();
ClassWriter cw = new ExpressionClassWriter();
cw.visit(V1_5, ACC_PUBLIC, clazzName, null, "org/springframework/expression/spel/CompiledExpression", null);
cw.visit(V1_5, ACC_PUBLIC, className, null, "org/springframework/expression/spel/CompiledExpression", null);
// Create default constructor
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
@@ -152,7 +153,7 @@ public final class SpelCompiler implements Opcodes {
new String[ ]{"org/springframework/expression/EvaluationException"});
mv.visitCode();
CodeFlow cf = new CodeFlow(clazzName, cw);
CodeFlow cf = new CodeFlow(className, cw);
// Ask the expression AST to generate the body of the method
try {
@@ -181,7 +182,7 @@ public final class SpelCompiler implements Opcodes {
byte[] data = cw.toByteArray();
// TODO need to make this conditionally occur based on a debug flag
// dump(expressionToCompile.toStringAST(), clazzName, data);
return loadClass(clazzName.replaceAll("/", "."), data);
return loadClass(StringUtils.replace(className, "/", "."), data);
}
/**