Search in sources :

Example 1 with SpelNode

use of org.springframework.expression.spel.SpelNode in project spring-framework by spring-projects.

the class InlineList method checkIfConstant.

/**
	 * If all the components of the list are constants, or lists that themselves contain constants, then a constant list
	 * can be built to represent this node. This will speed up later getValue calls and reduce the amount of garbage
	 * created.
	 */
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));
    }
}
Also used : ArrayList(java.util.ArrayList) SpelNode(org.springframework.expression.spel.SpelNode) TypedValue(org.springframework.expression.TypedValue)

Example 2 with SpelNode

use of org.springframework.expression.spel.SpelNode in project spring-framework by spring-projects.

the class InlineMap method checkIfConstant.

/**
	 * If all the components of the list are constants, or lists/maps that themselves
	 * contain constants, then a constant list can be built to represent this node.
	 * This will speed up later getValue calls and reduce the amount of garbage created.
	 */
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;
                    break;
                }
            } else if (child instanceof InlineMap) {
                InlineMap inlineMap = (InlineMap) child;
                if (!inlineMap.isConstant()) {
                    isConstant = false;
                    break;
                }
            } else if (!((c % 2) == 0 && (child instanceof PropertyOrFieldReference))) {
                isConstant = false;
                break;
            }
        }
    }
    if (isConstant) {
        Map<Object, Object> constantMap = new LinkedHashMap<>();
        int childCount = getChildCount();
        for (int c = 0; c < childCount; c++) {
            SpelNode keyChild = getChild(c++);
            SpelNode valueChild = getChild(c);
            Object key = null;
            Object value = null;
            if (keyChild instanceof Literal) {
                key = ((Literal) keyChild).getLiteralValue().getValue();
            } else if (keyChild instanceof PropertyOrFieldReference) {
                key = ((PropertyOrFieldReference) keyChild).getName();
            } else {
                return;
            }
            if (valueChild instanceof Literal) {
                value = ((Literal) valueChild).getLiteralValue().getValue();
            } else if (valueChild instanceof InlineList) {
                value = ((InlineList) valueChild).getConstantValue();
            } else if (valueChild instanceof InlineMap) {
                value = ((InlineMap) valueChild).getConstantValue();
            }
            constantMap.put(key, value);
        }
        this.constant = new TypedValue(Collections.unmodifiableMap(constantMap));
    }
}
Also used : SpelNode(org.springframework.expression.spel.SpelNode) LinkedHashMap(java.util.LinkedHashMap) TypedValue(org.springframework.expression.TypedValue)

Example 3 with SpelNode

use of org.springframework.expression.spel.SpelNode in project spring-framework by spring-projects.

the class SpelParserTests method positionalInformation.

@Test
public void positionalInformation() {
    SpelExpression expr = new SpelExpressionParser().parseRaw("true and true or false");
    SpelNode rootAst = expr.getAST();
    OpOr operatorOr = (OpOr) rootAst;
    OpAnd operatorAnd = (OpAnd) operatorOr.getLeftOperand();
    SpelNode rightOrOperand = operatorOr.getRightOperand();
    // check position for final 'false'
    assertEquals(17, rightOrOperand.getStartPosition());
    assertEquals(22, rightOrOperand.getEndPosition());
    // check position for first 'true'
    assertEquals(0, operatorAnd.getLeftOperand().getStartPosition());
    assertEquals(4, operatorAnd.getLeftOperand().getEndPosition());
    // check position for second 'true'
    assertEquals(9, operatorAnd.getRightOperand().getStartPosition());
    assertEquals(13, operatorAnd.getRightOperand().getEndPosition());
    // check position for OperatorAnd
    assertEquals(5, operatorAnd.getStartPosition());
    assertEquals(8, operatorAnd.getEndPosition());
    // check position for OperatorOr
    assertEquals(14, operatorOr.getStartPosition());
    assertEquals(16, operatorOr.getEndPosition());
}
Also used : OpOr(org.springframework.expression.spel.ast.OpOr) OpAnd(org.springframework.expression.spel.ast.OpAnd) SpelNode(org.springframework.expression.spel.SpelNode) Test(org.junit.Test)

Example 4 with SpelNode

use of org.springframework.expression.spel.SpelNode in project spring-framework by spring-projects.

the class InlineMap method getValueInternal.

@Override
public TypedValue getValueInternal(ExpressionState expressionState) throws EvaluationException {
    if (this.constant != null) {
        return this.constant;
    } else {
        Map<Object, Object> returnValue = new LinkedHashMap<>();
        int childcount = getChildCount();
        for (int c = 0; c < childcount; c++) {
            // TODO allow for key being PropertyOrFieldReference like Indexer on maps
            SpelNode keyChild = getChild(c++);
            Object key = null;
            if (keyChild instanceof PropertyOrFieldReference) {
                PropertyOrFieldReference reference = (PropertyOrFieldReference) keyChild;
                key = reference.getName();
            } else {
                key = keyChild.getValue(expressionState);
            }
            Object value = getChild(c).getValue(expressionState);
            returnValue.put(key, value);
        }
        return new TypedValue(returnValue);
    }
}
Also used : SpelNode(org.springframework.expression.spel.SpelNode) LinkedHashMap(java.util.LinkedHashMap) TypedValue(org.springframework.expression.TypedValue)

Example 5 with SpelNode

use of org.springframework.expression.spel.SpelNode in project spring-framework by spring-projects.

the class ConstructorReference method populateReferenceTypeArray.

private void populateReferenceTypeArray(ExpressionState state, Object newArray, TypeConverter typeConverter, InlineList initializer, Class<?> componentType) {
    TypeDescriptor toTypeDescriptor = TypeDescriptor.valueOf(componentType);
    Object[] newObjectArray = (Object[]) newArray;
    for (int i = 0; i < newObjectArray.length; i++) {
        SpelNode elementNode = initializer.getChild(i);
        Object arrayEntry = elementNode.getValue(state);
        newObjectArray[i] = typeConverter.convertValue(arrayEntry, TypeDescriptor.forObject(arrayEntry), toTypeDescriptor);
    }
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) SpelNode(org.springframework.expression.spel.SpelNode)

Aggregations

SpelNode (org.springframework.expression.spel.SpelNode)5 TypedValue (org.springframework.expression.TypedValue)3 LinkedHashMap (java.util.LinkedHashMap)2 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)1 OpAnd (org.springframework.expression.spel.ast.OpAnd)1 OpOr (org.springframework.expression.spel.ast.OpOr)1