Skip to content

Commit 3e419cd

Browse files
committed
Polishing
Issue: SPR-14863
1 parent 82fa4ef commit 3e419cd

File tree

2 files changed

+154
-143
lines changed

2 files changed

+154
-143
lines changed

spring-expression/src/main/java/org/springframework/expression/spel/CodeFlow.java

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,28 @@
2828
import org.springframework.util.Assert;
2929

3030
/**
31-
* Manages the class being generated by the compilation process. It records
32-
* intermediate compilation state as the bytecode is generated. It also includes
33-
* various bytecode generation helper functions.
31+
* Manages the class being generated by the compilation process.
32+
*
33+
* <p>Records intermediate compilation state as the bytecode is generated.
34+
* Also includes various bytecode generation helper functions.
3435
*
3536
* @author Andy Clement
37+
* @author Juergen Hoeller
3638
* @since 4.1
3739
*/
3840
public class CodeFlow implements Opcodes {
3941

42+
/**
43+
* Name of the class being generated. Typically used when generating code
44+
* that accesses freshly generated fields on the generated type.
45+
*/
46+
private final String className;
47+
48+
/**
49+
* The current class being generated.
50+
*/
51+
private final ClassWriter classWriter;
52+
4053
/**
4154
* Record the type of what is on top of the bytecode stack (i.e. the type of the
4255
* output from the previous expression component). New scopes are used to evaluate
@@ -45,31 +58,20 @@ public class CodeFlow implements Opcodes {
4558
*/
4659
private final Stack<ArrayList<String>> compilationScopes;
4760

48-
/**
49-
* The current class being generated
50-
*/
51-
private ClassWriter cw;
52-
5361
/**
5462
* As SpEL ast nodes are called to generate code for the main evaluation method
5563
* they can register to add a field to this class. Any registered FieldAdders
5664
* will be called after the main evaluation function has finished being generated.
5765
*/
58-
private List<FieldAdder> fieldAdders = null;
66+
private List<FieldAdder> fieldAdders;
5967

6068
/**
6169
* As SpEL ast nodes are called to generate code for the main evaluation method
6270
* they can register to add code to a static initializer in the class. Any
6371
* registered ClinitAdders will be called after the main evaluation function
6472
* has finished being generated.
6573
*/
66-
private List<ClinitAdder> clinitAdders = null;
67-
68-
/**
69-
* Name of the class being generated. Typically used when generating code
70-
* that accesses freshly generated fields on the generated type.
71-
*/
72-
private String clazzName;
74+
private List<ClinitAdder> clinitAdders;
7375

7476
/**
7577
* When code generation requires holding a value in a class level field, this
@@ -83,13 +85,20 @@ public class CodeFlow implements Opcodes {
8385
*/
8486
private int nextFreeVariableId = 1;
8587

86-
public CodeFlow(String clazzName, ClassWriter cw) {
87-
this.compilationScopes = new Stack<>();
88-
this.compilationScopes.add(new ArrayList<>());
89-
this.cw = cw;
90-
this.clazzName = clazzName;
88+
89+
/**
90+
* Construct a new {@code CodeFlow} for the given class.
91+
* @param className the name of the class
92+
* @param classWriter the corresponding ASM {@code ClassWriter}
93+
*/
94+
public CodeFlow(String className, ClassWriter classWriter) {
95+
this.className = className;
96+
this.classWriter = classWriter;
97+
this.compilationScopes = new Stack<ArrayList<String>>();
98+
this.compilationScopes.add(new ArrayList<String>());
9199
}
92100

101+
93102
/**
94103
* Push the byte code to load the target (i.e. what was passed as the first argument
95104
* to CompiledExpression.getValue(target, context))
@@ -103,6 +112,7 @@ public void loadTarget(MethodVisitor mv) {
103112
* Push the bytecode to load the EvaluationContext (the second parameter passed to
104113
* the compiled expression method).
105114
* @param mv the visitor into which the load instruction should be inserted
115+
* @since 4.3.4
106116
*/
107117
public void loadEvaluationContext(MethodVisitor mv) {
108118
mv.visitVarInsn(ALOAD, 2);
@@ -164,18 +174,18 @@ public void unboxBooleanIfNecessary(MethodVisitor mv) {
164174
public void finish() {
165175
if (this.fieldAdders != null) {
166176
for (FieldAdder fieldAdder : this.fieldAdders) {
167-
fieldAdder.generateField(cw,this);
177+
fieldAdder.generateField(this.classWriter, this);
168178
}
169179
}
170180
if (this.clinitAdders != null) {
171-
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "<clinit>", "()V", null, null);
181+
MethodVisitor mv = this.classWriter.visitMethod(ACC_PUBLIC | ACC_STATIC, "<clinit>", "()V", null, null);
172182
mv.visitCode();
173-
this.nextFreeVariableId = 0; // To 0 because there is no 'this' in a clinit
183+
this.nextFreeVariableId = 0; // to 0 because there is no 'this' in a clinit
174184
for (ClinitAdder clinitAdder : this.clinitAdders) {
175185
clinitAdder.generateCode(mv, this);
176186
}
177187
mv.visitInsn(RETURN);
178-
mv.visitMaxs(0,0); // not supplied due to COMPUTE_MAXS
188+
mv.visitMaxs(0,0); // not supplied due to COMPUTE_MAXS
179189
mv.visitEnd();
180190
}
181191
}
@@ -213,13 +223,13 @@ public int nextFreeVariableId() {
213223
}
214224

215225
public String getClassName() {
216-
return this.clazzName;
226+
return this.className;
217227
}
218228

219229

220230
/**
221231
* Insert any necessary cast and value call to convert from a boxed type to a
222-
* primitive value
232+
* primitive value.
223233
* @param mv the method visitor into which instructions should be inserted
224234
* @param ch the primitive type desired as output
225235
* @param stackDescriptor the descriptor of the type on top of the stack

0 commit comments

Comments
 (0)