Polishing

Issue: SPR-14863
This commit is contained in:
Juergen Hoeller 2016-11-02 12:44:12 +01:00
parent 82fa4ef274
commit 3e419cde7d
2 changed files with 151 additions and 140 deletions

View File

@ -28,15 +28,28 @@ import org.springframework.asm.Opcodes;
import org.springframework.util.Assert;
/**
* Manages the class being generated by the compilation process. It records
* intermediate compilation state as the bytecode is generated. It also includes
* various bytecode generation helper functions.
* Manages the class being generated by the compilation process.
*
* <p>Records intermediate compilation state as the bytecode is generated.
* Also includes various bytecode generation helper functions.
*
* @author Andy Clement
* @author Juergen Hoeller
* @since 4.1
*/
public class CodeFlow implements Opcodes {
/**
* Name of the class being generated. Typically used when generating code
* that accesses freshly generated fields on the generated type.
*/
private final String className;
/**
* The current class being generated.
*/
private final ClassWriter classWriter;
/**
* Record the type of what is on top of the bytecode stack (i.e. the type of the
* output from the previous expression component). New scopes are used to evaluate
@ -45,17 +58,12 @@ public class CodeFlow implements Opcodes {
*/
private final Stack<ArrayList<String>> compilationScopes;
/**
* The current class being generated
*/
private ClassWriter cw;
/**
* As SpEL ast nodes are called to generate code for the main evaluation method
* they can register to add a field to this class. Any registered FieldAdders
* will be called after the main evaluation function has finished being generated.
*/
private List<FieldAdder> fieldAdders = null;
private List<FieldAdder> fieldAdders;
/**
* As SpEL ast nodes are called to generate code for the main evaluation method
@ -63,13 +71,7 @@ public class CodeFlow implements Opcodes {
* registered ClinitAdders will be called after the main evaluation function
* has finished being generated.
*/
private List<ClinitAdder> clinitAdders = null;
/**
* Name of the class being generated. Typically used when generating code
* that accesses freshly generated fields on the generated type.
*/
private String clazzName;
private List<ClinitAdder> clinitAdders;
/**
* When code generation requires holding a value in a class level field, this
@ -83,13 +85,20 @@ public class CodeFlow implements Opcodes {
*/
private int nextFreeVariableId = 1;
public CodeFlow(String clazzName, ClassWriter cw) {
this.compilationScopes = new Stack<>();
this.compilationScopes.add(new ArrayList<>());
this.cw = cw;
this.clazzName = clazzName;
/**
* Construct a new {@code CodeFlow} for the given class.
* @param className the name of the class
* @param classWriter the corresponding ASM {@code ClassWriter}
*/
public CodeFlow(String className, ClassWriter classWriter) {
this.className = className;
this.classWriter = classWriter;
this.compilationScopes = new Stack<ArrayList<String>>();
this.compilationScopes.add(new ArrayList<String>());
}
/**
* Push the byte code to load the target (i.e. what was passed as the first argument
* to CompiledExpression.getValue(target, context))
@ -103,6 +112,7 @@ public class CodeFlow implements Opcodes {
* Push the bytecode to load the EvaluationContext (the second parameter passed to
* the compiled expression method).
* @param mv the visitor into which the load instruction should be inserted
* @since 4.3.4
*/
public void loadEvaluationContext(MethodVisitor mv) {
mv.visitVarInsn(ALOAD, 2);
@ -164,13 +174,13 @@ public class CodeFlow implements Opcodes {
public void finish() {
if (this.fieldAdders != null) {
for (FieldAdder fieldAdder : this.fieldAdders) {
fieldAdder.generateField(cw,this);
fieldAdder.generateField(this.classWriter, this);
}
}
if (this.clinitAdders != null) {
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "<clinit>", "()V", null, null);
MethodVisitor mv = this.classWriter.visitMethod(ACC_PUBLIC | ACC_STATIC, "<clinit>", "()V", null, null);
mv.visitCode();
this.nextFreeVariableId = 0; // To 0 because there is no 'this' in a clinit
this.nextFreeVariableId = 0; // to 0 because there is no 'this' in a clinit
for (ClinitAdder clinitAdder : this.clinitAdders) {
clinitAdder.generateCode(mv, this);
}
@ -213,13 +223,13 @@ public class CodeFlow implements Opcodes {
}
public String getClassName() {
return this.clazzName;
return this.className;
}
/**
* Insert any necessary cast and value call to convert from a boxed type to a
* primitive value
* primitive value.
* @param mv the method visitor into which instructions should be inserted
* @param ch the primitive type desired as output
* @param stackDescriptor the descriptor of the type on top of the stack

View File

@ -1624,14 +1624,14 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
@Test
public void opNe_SPR14863() throws Exception {
// First part is from the test case specified in the bug report
SpelParserConfiguration configuration =
new SpelParserConfiguration(SpelCompilerMode.MIXED, ClassLoader.getSystemClassLoader());
SpelExpressionParser parser = new SpelExpressionParser(configuration);
Expression expression = parser.parseExpression("data['my-key'] != 'my-value'");
Map<String, String> data = new HashMap<>();
data.put("my-key", new String("my-value"));
StandardEvaluationContext context = new StandardEvaluationContext(new MyContext(data));
SpelParserConfiguration configuration = new SpelParserConfiguration(SpelCompilerMode.MIXED,
ClassLoader.getSystemClassLoader());
SpelExpressionParser parser = new SpelExpressionParser(configuration);
Expression expression = parser.parseExpression("data['my-key'] != 'my-value'");
assertFalse(expression.getValue(context, Boolean.class));
assertCanCompile(expression);
((SpelExpression) expression).compileExpression();
@ -1698,48 +1698,6 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
assertFalse(expression.getValue(context, Boolean.class));
}
public static class Apple implements Comparable<Apple> {
public Object gotComparedTo = null;
public int i;
public Apple(int i) {
this.i = i;
}
public void setValue(int i) {
this.i = i;
}
@Override
public int compareTo(Apple that) {
this.gotComparedTo = that;
if (this.i<that.i) {
return -1;
}
else if (this.i>that.i) {
return +1;
}
else {
return 0;
}
}
}
// For opNe_SPR14863
public static class MyContext {
private final Map<String, String> data;
public MyContext(Map<String, String> data) {
this.data = data;
}
public Map<String, String> getData() {
return data;
}
}
@Test
public void opPlus() throws Exception {
expression = parse("2+2");
@ -2733,7 +2691,6 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
checkCalc(p,"payload.valueI*payload.valueBB20",2400);
}
@Test
public void opModulus_mixedNumberTypes() throws Exception {
PayloadX p = new PayloadX();
@ -5660,4 +5617,48 @@ public class SpelCompilationCoverageTests extends AbstractExpressionTests {
}
}
public static class Apple implements Comparable<Apple> {
public Object gotComparedTo = null;
public int i;
public Apple(int i) {
this.i = i;
}
public void setValue(int i) {
this.i = i;
}
@Override
public int compareTo(Apple that) {
this.gotComparedTo = that;
if (this.i < that.i) {
return -1;
}
else if (this.i > that.i) {
return +1;
}
else {
return 0;
}
}
}
// For opNe_SPR14863
public static class MyContext {
private final Map<String, String> data;
public MyContext(Map<String, String> data) {
this.data = data;
}
public Map<String, String> getData() {
return data;
}
}
}