Skip to content

Commit 8f579b3

Browse files
committed
Make SpelNode compilation aware
In order to make SpelNode compilation aware, this method moves the declaration of isCompilable() and generateCode(...) from SpelNodeImpl to SpelNode. Closes gh-32707
1 parent 1468925 commit 8f579b3

File tree

2 files changed

+38
-21
lines changed

2 files changed

+38
-21
lines changed

Diff for: spring-expression/src/main/java/org/springframework/expression/spel/SpelNode.java

+38
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.expression.spel;
1818

19+
import org.springframework.asm.MethodVisitor;
1920
import org.springframework.expression.EvaluationException;
2021
import org.springframework.expression.TypedValue;
2122
import org.springframework.lang.Nullable;
@@ -25,6 +26,7 @@
2526
* Expression Language (SpEL) expression.
2627
*
2728
* @author Andy Clement
29+
* @author Sam Brannen
2830
* @since 3.0
2931
*/
3032
public interface SpelNode {
@@ -109,4 +111,40 @@ public interface SpelNode {
109111
*/
110112
int getEndPosition();
111113

114+
/**
115+
* Determine if this node can be compiled to bytecode.
116+
* <p>The reasoning in each node may be different but will typically involve
117+
* checking whether the exit type descriptor of the node is known and any
118+
* relevant child nodes are compilable.
119+
* <p>The default implementation returns {@code false}.
120+
* <p>If you override this method, you must also override
121+
* {@link #generateCode(MethodVisitor, CodeFlow)}.
122+
* @return {@code true} if this node can be compiled to bytecode
123+
* @since 6.2
124+
* @see #generateCode(MethodVisitor, CodeFlow)
125+
*/
126+
default boolean isCompilable() {
127+
return false;
128+
}
129+
130+
/**
131+
* Generate the bytecode for this node into the supplied {@link MethodVisitor}.
132+
* <p>Context information about the current expression being compiled is
133+
* available in the supplied {@link CodeFlow} object &mdash; for example,
134+
* information about the type of the object currently on the stack.
135+
* <p>This method will not be invoked unless {@link #isCompilable()} returns
136+
* {@code true}.
137+
* <p>The default implementation throws an {@link IllegalStateException}
138+
* since {@link #isCompilable()} returns {@code false} by default.
139+
* <p>If you override this method, you must also override {@link #isCompilable()}.
140+
* @param methodVisitor the ASM {@code MethodVisitor} into which code should
141+
* be generated
142+
* @param codeFlow a context object with information about what is on the stack
143+
* @since 6.2
144+
* @see #isCompilable()
145+
*/
146+
default void generateCode(MethodVisitor methodVisitor, CodeFlow codeFlow) {
147+
throw new IllegalStateException(getClass().getName() + " does not support bytecode generation");
148+
}
149+
112150
}

Diff for: spring-expression/src/main/java/org/springframework/expression/spel/ast/SpelNodeImpl.java

-21
Original file line numberDiff line numberDiff line change
@@ -191,27 +191,6 @@ public boolean isNullSafe() {
191191
return false;
192192
}
193193

194-
/**
195-
* Check whether a node can be compiled to bytecode. The reasoning in each node may
196-
* be different but will typically involve checking whether the exit type descriptor
197-
* of the node is known and any relevant child nodes are compilable.
198-
* @return {@code true} if this node can be compiled to bytecode
199-
*/
200-
public boolean isCompilable() {
201-
return false;
202-
}
203-
204-
/**
205-
* Generate the bytecode for this node into the supplied visitor. Context info about
206-
* the current expression being compiled is available in the codeflow object, e.g.
207-
* including information about the type of the object currently on the stack.
208-
* @param mv the ASM MethodVisitor into which code should be generated
209-
* @param cf a context object with info about what is on the stack
210-
*/
211-
public void generateCode(MethodVisitor mv, CodeFlow cf) {
212-
throw new IllegalStateException(getClass().getName() +" has no generateCode(..) method");
213-
}
214-
215194
@Nullable
216195
public String getExitDescriptor() {
217196
return this.exitTypeDescriptor;

0 commit comments

Comments
 (0)