org.springframework.expression.spel.SpelNode Java Examples

The following examples show how to use org.springframework.expression.spel.SpelNode. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: InlineMap.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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);
	}
}
 
Example #2
Source File: InlineMap.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public TypedValue getValueInternal(ExpressionState expressionState) throws EvaluationException {
	if (this.constant != null) {
		return this.constant;
	}
	else {
		Map<Object, Object> returnValue = new LinkedHashMap<Object, Object>();
		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);
	}
}
 
Example #3
Source File: InlineMap.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public TypedValue getValueInternal(ExpressionState expressionState) throws EvaluationException {
	if (this.constant != null) {
		return this.constant;
	}
	else {
		Map<Object, Object> returnValue = new LinkedHashMap<Object, Object>();
		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);
	}
}
 
Example #4
Source File: InlineMap.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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);
	}
}
 
Example #5
Source File: SpelParserTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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());
}
 
Example #6
Source File: SpelParserTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void positionalInformation() throws EvaluationException, ParseException {
	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());
}
 
Example #7
Source File: ConstructorReference.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
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);
	}
}
 
Example #8
Source File: ConstructorReference.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
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);
	}
}
 
Example #9
Source File: ConstructorReference.java    From spring-analysis-note with MIT License 5 votes vote down vote up
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);
	}
}
 
Example #10
Source File: ConstructorReference.java    From java-technology-stack with MIT License 5 votes vote down vote up
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);
	}
}
 
Example #11
Source File: SpelParserTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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());
}
 
Example #12
Source File: SpelNodeImpl.java    From java-technology-stack with MIT License 4 votes vote down vote up
@Override
public SpelNode getChild(int index) {
	return this.children[index];
}
 
Example #13
Source File: SpelExpression.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * Return the Abstract Syntax Tree for the expression.
 */
public SpelNode getAST() {
	return this.ast;
}
 
Example #14
Source File: InlineMap.java    From java-technology-stack with MIT License 4 votes vote down vote up
/**
 * If all the components of the map 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));
	}
}
 
Example #15
Source File: InlineMap.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * 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<Object,Object>();			
		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));
	}
}
 
Example #16
Source File: SpelNodeImpl.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
@Override
public SpelNode getChild(int index) {
	return this.children[index];
}
 
Example #17
Source File: SpelExpression.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Return the Abstract Syntax Tree for the expression.
 */
public SpelNode getAST() {
	return this.ast;
}
 
Example #18
Source File: SpelExpression.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * Return the Abstract Syntax Tree for the expression.
 */
public SpelNode getAST() {
	return this.ast;
}
 
Example #19
Source File: InlineMap.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * 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<Object,Object>();			
		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));
	}
}
 
Example #20
Source File: SpelNodeImpl.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Override
public SpelNode getChild(int index) {
	return this.children[index];
}
 
Example #21
Source File: SpelNodeImpl.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Override
public SpelNode getChild(int index) {
	return this.children[index];
}
 
Example #22
Source File: SpelExpression.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
/**
 * Return the Abstract Syntax Tree for the expression.
 */
public SpelNode getAST() {
	return this.ast;
}
 
Example #23
Source File: InlineMap.java    From spring-analysis-note with MIT License 4 votes vote down vote up
/**
 * If all the components of the map 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));
	}
}