package org.springframework.expression.spel.ast;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.springframework.asm.MethodVisitor;
import org.springframework.expression.EvaluationException;
import org.springframework.expression.TypedValue;
import org.springframework.expression.spel.CodeFlow;
import org.springframework.expression.spel.ExpressionState;
import org.springframework.expression.spel.SpelNode;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
public class InlineList extends SpelNodeImpl {
@Nullable
private TypedValue constant;
public InlineList(int pos, SpelNodeImpl... args) {
super(pos, args);
checkIfConstant();
}
private void checkIfConstant() {
boolean isConstant = true;
for (int c = 0, max = getChildCount(); c < max; c++) {
SpelNode child = getChild(c);
if (!(child instanceof Literal)) {
if (child instanceof InlineList) {
InlineList inlineList = (InlineList) child;
if (!inlineList.isConstant()) {
isConstant = false;
}
}
else {
isConstant = false;
}
}
}
if (isConstant) {
List<Object> constantList = new ArrayList<>();
int childcount = getChildCount();
for (int c = 0; c < childcount; c++) {
SpelNode child = getChild(c);
if ((child instanceof Literal)) {
constantList.add(((Literal) child).getLiteralValue().getValue());
}
else if (child instanceof InlineList) {
constantList.add(((InlineList) child).getConstantValue());
}
}
this.constant = new TypedValue(Collections.unmodifiableList(constantList));
}
}
@Override
public TypedValue getValueInternal(ExpressionState expressionState) throws EvaluationException {
if (this.constant != null) {
return this.constant;
}
else {
List<Object> returnValue = new ArrayList<>();
int childCount = getChildCount();
for (int c = 0; c < childCount; c++) {
returnValue.add(getChild(c).getValue(expressionState));
}
return new TypedValue(returnValue);
}
}
@Override
public String toStringAST() {
StringBuilder sb = new StringBuilder("{");
int count = getChildCount();
for (int c = 0; c < count; c++) {
if (c > 0) {
sb.append(",");
}
sb.append(getChild(c).toStringAST());
}
sb.append("}");
return sb.toString();
}
public boolean isConstant() {
return (this.constant != null);
}
@SuppressWarnings("unchecked")
@Nullable
public List<Object> getConstantValue() {
Assert.state(this.constant != null, "No constant");
return (List<Object>) this.constant.getValue();
}
@Override
public boolean isCompilable() {
return isConstant();
}
@Override
public void generateCode(MethodVisitor mv, CodeFlow codeflow) {
final String constantFieldName = "inlineList$" + codeflow.nextFieldId();
final String className = codeflow.getClassName();
codeflow.registerNewField((cw, cflow) ->
cw.visitField(ACC_PRIVATE | ACC_STATIC | ACC_FINAL, constantFieldName, "Ljava/util/List;", null, null));
codeflow.registerNewClinit((mVisitor, cflow) ->
generateClinitCode(className, constantFieldName, mVisitor, cflow, false));
mv.visitFieldInsn(GETSTATIC, className, constantFieldName, "Ljava/util/List;");
codeflow.pushDescriptor("Ljava/util/List");
}
void generateClinitCode(String clazzname, String constantFieldName, MethodVisitor mv, CodeFlow codeflow, boolean nested) {
mv.visitTypeInsn(NEW, "java/util/ArrayList");
mv.visitInsn(DUP);
mv.visitMethodInsn(INVOKESPECIAL, "java/util/ArrayList", "<init>", "()V", false);
if (!nested) {
mv.visitFieldInsn(PUTSTATIC, clazzname, constantFieldName, "Ljava/util/List;");
}
int childCount = getChildCount();
for (int c = 0; c < childCount; c++) {
if (!nested) {
mv.visitFieldInsn(GETSTATIC, clazzname, constantFieldName, "Ljava/util/List;");
}
else {
mv.visitInsn(DUP);
}
if (this.children[c] instanceof InlineList) {
((InlineList)this.children[c]).generateClinitCode(clazzname, constantFieldName, mv, codeflow, true);
}
else {
this.children[c].generateCode(mv, codeflow);
String lastDesc = codeflow.lastDescriptor();
if (CodeFlow.isPrimitive(lastDesc)) {
CodeFlow.insertBoxIfNecessary(mv, lastDesc.charAt(0));
}
}
mv.visitMethodInsn(INVOKEINTERFACE, "java/util/List", "add", "(Ljava/lang/Object;)Z", true);
mv.visitInsn(POP);
}
}
}