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,18 +174,18 @@ 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);
}
mv.visitInsn(RETURN);
mv.visitMaxs(0,0); // not supplied due to COMPUTE_MAXS
mv.visitMaxs(0,0); // not supplied due to COMPUTE_MAXS
mv.visitEnd();
}
}
@@ -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