Java Code Examples for org.springframework.expression.spel.ExpressionState

The following examples show how to use org.springframework.expression.spel.ExpressionState. These examples are extracted from open source projects. 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 Project: spring4-understanding   Source File: OperatorBetween.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Returns a boolean based on whether a value is in the range expressed. The first
 * operand is any value whilst the second is a list of two values - those two values
 * being the bounds allowed for the first operand (inclusive).
 * @param state the expression state
 * @return true if the left operand is in the range specified, false otherwise
 * @throws EvaluationException if there is a problem evaluating the expression
 */
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	Object left = getLeftOperand().getValueInternal(state).getValue();
	Object right = getRightOperand().getValueInternal(state).getValue();
	if (!(right instanceof List) || ((List<?>) right).size() != 2) {
		throw new SpelEvaluationException(getRightOperand().getStartPosition(),
				SpelMessage.BETWEEN_RIGHT_OPERAND_MUST_BE_TWO_ELEMENT_LIST);
	}

	List<?> list = (List<?>) right;
	Object low = list.get(0);
	Object high = list.get(1);
	TypeComparator comp = state.getTypeComparator();
	try {
		return BooleanTypedValue.forValue(comp.compare(left, low) >= 0 && comp.compare(left, high) <= 0);
	}
	catch (SpelEvaluationException ex) {
		ex.setPosition(getStartPosition());
		throw ex;
	}
}
 
Example 2
Source Project: spring4-understanding   Source File: SpelExpression.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Object getValue(EvaluationContext context) throws EvaluationException {
	Assert.notNull(context, "EvaluationContext is required");
	if (compiledAst!= null) {
		try {
			TypedValue contextRoot = context == null ? null : context.getRootObject();
			return this.compiledAst.getValue(contextRoot != null ? contextRoot.getValue() : null, context);
		}
		catch (Throwable ex) {
			// If running in mixed mode, revert to interpreted
			if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) {
				this.interpretedCount = 0;
				this.compiledAst = null;
			}
			else {
				// Running in SpelCompilerMode.immediate mode - propagate exception to caller
				throw new SpelEvaluationException(ex, SpelMessage.EXCEPTION_RUNNING_COMPILED_EXPRESSION);
			}
		}
	}
	ExpressionState expressionState = new ExpressionState(context, this.configuration);
	Object result = this.ast.getValue(expressionState);
	checkCompile(expressionState);
	return result;
}
 
Example 3
Source Project: lams   Source File: BeanReference.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	BeanResolver beanResolver = state.getEvaluationContext().getBeanResolver();
	if (beanResolver == null) {
		throw new SpelEvaluationException(
				getStartPosition(), SpelMessage.NO_BEAN_RESOLVER_REGISTERED, this.beanName);
	}

	try {
		return new TypedValue(beanResolver.resolve(state.getEvaluationContext(), this.beanName));
	}
	catch (AccessException ex) {
		throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.EXCEPTION_DURING_BEAN_RESOLUTION,
			this.beanName, ex.getMessage());
	}
}
 
Example 4
Source Project: lams   Source File: SpelExpression.java    License: GNU General Public License v2.0 6 votes vote down vote up
@Override
public Object getValue(EvaluationContext context, Object rootObject) throws EvaluationException {
	Assert.notNull(context, "EvaluationContext is required");

	if (this.compiledAst != null) {
		try {
			return this.compiledAst.getValue(rootObject,context);
		}
		catch (Throwable ex) {
			// If running in mixed mode, revert to interpreted
			if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) {
				this.interpretedCount = 0;
				this.compiledAst = null;
			}
			else {
				// Running in SpelCompilerMode.immediate mode - propagate exception to caller
				throw new SpelEvaluationException(ex, SpelMessage.EXCEPTION_RUNNING_COMPILED_EXPRESSION);
			}
		}
	}

	ExpressionState expressionState = new ExpressionState(context, toTypedValue(rootObject), this.configuration);
	Object result = this.ast.getValue(expressionState);
	checkCompile(expressionState);
	return result;
}
 
Example 5
Source Project: java-technology-stack   Source File: OpPlusTests.java    License: MIT License 6 votes vote down vote up
@Test
public void test_binaryPlusWithTime_ToString() {

	ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());

	Time time = new Time(new Date().getTime());

	VariableReference var = new VariableReference("timeVar", -1);
	var.setValue(expressionState, time);

	StringLiteral n2 = new StringLiteral("\" is now\"", -1, "\" is now\"");
	OpPlus o = new OpPlus(-1, var, n2);
	TypedValue value = o.getValueInternal(expressionState);

	assertEquals(String.class, value.getTypeDescriptor().getObjectType());
	assertEquals(String.class, value.getTypeDescriptor().getType());
	assertEquals(time + " is now", value.getValue());
}
 
Example 6
Source Project: spring-analysis-note   Source File: BeanReference.java    License: MIT License 6 votes vote down vote up
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	BeanResolver beanResolver = state.getEvaluationContext().getBeanResolver();
	if (beanResolver == null) {
		throw new SpelEvaluationException(
				getStartPosition(), SpelMessage.NO_BEAN_RESOLVER_REGISTERED, this.beanName);
	}

	try {
		return new TypedValue(beanResolver.resolve(state.getEvaluationContext(), this.beanName));
	}
	catch (AccessException ex) {
		throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.EXCEPTION_DURING_BEAN_RESOLUTION,
			this.beanName, ex.getMessage());
	}
}
 
Example 7
Source Project: spring4-understanding   Source File: FunctionReference.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	TypedValue value = state.lookupVariable(this.name);
	if (value == null) {
		throw new SpelEvaluationException(getStartPosition(), SpelMessage.FUNCTION_NOT_DEFINED, this.name);
	}

	// Two possibilities: a lambda function or a Java static method registered as a function
	if (!(value.getValue() instanceof Method)) {
		throw new SpelEvaluationException(
				SpelMessage.FUNCTION_REFERENCE_CANNOT_BE_INVOKED, this.name, value.getClass());
	}

	try {
		return executeFunctionJLRMethod(state, (Method) value.getValue());
	}
	catch (SpelEvaluationException ex) {
		ex.setPosition(getStartPosition());
		throw ex;
	}
}
 
Example 8
Source Project: java-technology-stack   Source File: SpelExpression.java    License: MIT License 6 votes vote down vote up
@Override
@Nullable
public Object getValue() throws EvaluationException {
	if (this.compiledAst != null) {
		try {
			EvaluationContext context = getEvaluationContext();
			return this.compiledAst.getValue(context.getRootObject().getValue(), context);
		}
		catch (Throwable ex) {
			// If running in mixed mode, revert to interpreted
			if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) {
				this.interpretedCount = 0;
				this.compiledAst = null;
			}
			else {
				// Running in SpelCompilerMode.immediate mode - propagate exception to caller
				throw new SpelEvaluationException(ex, SpelMessage.EXCEPTION_RUNNING_COMPILED_EXPRESSION);
			}
		}
	}

	ExpressionState expressionState = new ExpressionState(getEvaluationContext(), this.configuration);
	Object result = this.ast.getValue(expressionState);
	checkCompile(expressionState);
	return result;
}
 
Example 9
Source Project: spring-analysis-note   Source File: SpelExpression.java    License: MIT License 6 votes vote down vote up
@Override
@Nullable
public Object getValue(Object rootObject) throws EvaluationException {
	if (this.compiledAst != null) {
		try {
			return this.compiledAst.getValue(rootObject, getEvaluationContext());
		}
		catch (Throwable ex) {
			// If running in mixed mode, revert to interpreted
			if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) {
				this.interpretedCount.set(0);
				this.compiledAst = null;
			}
			else {
				// Running in SpelCompilerMode.immediate mode - propagate exception to caller
				throw new SpelEvaluationException(ex, SpelMessage.EXCEPTION_RUNNING_COMPILED_EXPRESSION);
			}
		}
	}

	ExpressionState expressionState =
			new ExpressionState(getEvaluationContext(), toTypedValue(rootObject), this.configuration);
	Object result = this.ast.getValue(expressionState);
	checkCompile(expressionState);
	return result;
}
 
Example 10
Source Project: spring4-understanding   Source File: SpelExpression.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public Object getValue(EvaluationContext context, Object rootObject) throws EvaluationException {
	Assert.notNull(context, "EvaluationContext is required");
	if (this.compiledAst != null) {
		try {
			return this.compiledAst.getValue(rootObject,context);
		}
		catch (Throwable ex) {
			// If running in mixed mode, revert to interpreted
			if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) {
				this.interpretedCount = 0;
				this.compiledAst = null;
			}
			else {
				// Running in SpelCompilerMode.immediate mode - propagate exception to caller
				throw new SpelEvaluationException(ex, SpelMessage.EXCEPTION_RUNNING_COMPILED_EXPRESSION);
			}
		}
	}
	ExpressionState expressionState = new ExpressionState(context, toTypedValue(rootObject), this.configuration);
	Object result = this.ast.getValue(expressionState);
	checkCompile(expressionState);
	return result;
}
 
Example 11
Source Project: lams   Source File: ConstructorReference.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void populateByteArray(ExpressionState state, Object newArray, TypeConverter typeConverter,
		InlineList initializer) {

	byte[] newByteArray = (byte[]) newArray;
	for (int i = 0; i < newByteArray.length; i++) {
		TypedValue typedValue = initializer.getChild(i).getTypedValue(state);
		newByteArray[i] = ExpressionUtils.toByte(typeConverter, typedValue);
	}
}
 
Example 12
Source Project: java-technology-stack   Source File: OpAnd.java    License: MIT License 5 votes vote down vote up
private boolean getBooleanValue(ExpressionState state, SpelNodeImpl operand) {
	try {
		Boolean value = operand.getValue(state, Boolean.class);
		assertValueNotNull(value);
		return value;
	}
	catch (SpelEvaluationException ex) {
		ex.setPosition(operand.getStartPosition());
		throw ex;
	}
}
 
Example 13
Source Project: lams   Source File: SpelExpression.java    License: GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> T getValue(EvaluationContext context, Class<T> expectedResultType) throws EvaluationException {
	Assert.notNull(context, "EvaluationContext is required");

	if (this.compiledAst != null) {
		try {
			TypedValue contextRoot = context.getRootObject();
			Object result = this.compiledAst.getValue(contextRoot.getValue(), context);
			if (expectedResultType != null) {
				return ExpressionUtils.convertTypedValue(context, new TypedValue(result), expectedResultType);
			}
			else {
				return (T) result;
			}
		}
		catch (Throwable ex) {
			// If running in mixed mode, revert to interpreted
			if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) {
				this.interpretedCount = 0;
				this.compiledAst = null;
			}
			else {
				// Running in SpelCompilerMode.immediate mode - propagate exception to caller
				throw new SpelEvaluationException(ex, SpelMessage.EXCEPTION_RUNNING_COMPILED_EXPRESSION);
			}
		}
	}

	ExpressionState expressionState = new ExpressionState(context, this.configuration);
	TypedValue typedResultValue = this.ast.getTypedValue(expressionState);
	checkCompile(expressionState);
	return ExpressionUtils.convertTypedValue(context, typedResultValue, expectedResultType);
}
 
Example 14
Source Project: spring-analysis-note   Source File: ConstructorReference.java    License: MIT License 5 votes vote down vote up
private void populateCharArray(ExpressionState state, Object newArray, TypeConverter typeConverter,
		InlineList initializer) {

	char[] newCharArray = (char[]) newArray;
	for (int i = 0; i < newCharArray.length; i++) {
		TypedValue typedValue = initializer.getChild(i).getTypedValue(state);
		newCharArray[i] = ExpressionUtils.toChar(typeConverter, typedValue);
	}
}
 
Example 15
Source Project: java-technology-stack   Source File: SpelExpression.java    License: MIT License 5 votes vote down vote up
@Override
@Nullable
public TypeDescriptor getValueTypeDescriptor(EvaluationContext context, Object rootObject)
		throws EvaluationException {

	Assert.notNull(context, "EvaluationContext is required");
	ExpressionState expressionState = new ExpressionState(context, toTypedValue(rootObject), this.configuration);
	return this.ast.getValueInternal(expressionState).getTypeDescriptor();
}
 
Example 16
Source Project: spring-analysis-note   Source File: ConstructorReference.java    License: MIT License 5 votes vote down vote up
private void populateIntArray(ExpressionState state, Object newArray, TypeConverter typeConverter,
		InlineList initializer) {

	int[] newIntArray = (int[]) newArray;
	for (int i = 0; i < newIntArray.length; i++) {
		TypedValue typedValue = initializer.getChild(i).getTypedValue(state);
		newIntArray[i] = ExpressionUtils.toInt(typeConverter, typedValue);
	}
}
 
Example 17
private void populateIntArray(ExpressionState state, Object newArray, TypeConverter typeConverter,
		InlineList initializer) {

	int[] newIntArray = (int[]) newArray;
	for (int i = 0; i < newIntArray.length; i++) {
		TypedValue typedValue = initializer.getChild(i).getTypedValue(state);
		newIntArray[i] = ExpressionUtils.toInt(typeConverter, typedValue);
	}
}
 
Example 18
Source Project: lams   Source File: ConstructorReference.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Implements getValue() - delegating to the code for building an array or a simple type.
 */
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	if (this.isArrayConstructor) {
		return createArray(state);
	}
	else {
		return createNewInstance(state);
	}
}
 
Example 19
Source Project: java-technology-stack   Source File: MethodReference.java    License: MIT License 5 votes vote down vote up
private Object[] getArguments(ExpressionState state) {
	Object[] arguments = new Object[getChildCount()];
	for (int i = 0; i < arguments.length; i++) {
		// Make the root object the active context again for evaluating the parameter expressions
		try {
			state.pushActiveContextObject(state.getScopeRootContextObject());
			arguments[i] = this.children[i].getValueInternal(state).getValue();
		}
		finally {
			state.popActiveContextObject();
		}
	}
	return arguments;
}
 
Example 20
Source Project: spring-analysis-note   Source File: OpAnd.java    License: MIT License 5 votes vote down vote up
private boolean getBooleanValue(ExpressionState state, SpelNodeImpl operand) {
	try {
		Boolean value = operand.getValue(state, Boolean.class);
		assertValueNotNull(value);
		return value;
	}
	catch (SpelEvaluationException ex) {
		ex.setPosition(operand.getStartPosition());
		throw ex;
	}
}
 
Example 21
Source Project: spring4-understanding   Source File: OpPlusTests.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void test_binaryPlusWithLeftStringOperand() {
	ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());

	StringLiteral n1 = new StringLiteral("\"number is \"", -1, "\"number is \"");
	LongLiteral n2 = new LongLiteral("123", -1, 123);
	OpPlus o = new OpPlus(-1, n1, n2);
	TypedValue value = o.getValueInternal(expressionState);

	assertEquals(String.class, value.getTypeDescriptor().getObjectType());
	assertEquals(String.class, value.getTypeDescriptor().getType());
	assertEquals("number is 123", value.getValue());
}
 
Example 22
Source Project: lams   Source File: SpelExpression.java    License: GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> T getValue(Object rootObject, Class<T> expectedResultType) throws EvaluationException {
	if (this.compiledAst != null) {
		try {
			Object result = this.compiledAst.getValue(rootObject, null);
			if (expectedResultType == null) {
				return (T)result;
			}
			else {
				return ExpressionUtils.convertTypedValue(
						getEvaluationContext(), new TypedValue(result), expectedResultType);
			}
		}
		catch (Throwable ex) {
			// If running in mixed mode, revert to interpreted
			if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) {
				this.interpretedCount = 0;
				this.compiledAst = null;
			}
			else {
				// Running in SpelCompilerMode.immediate mode - propagate exception to caller
				throw new SpelEvaluationException(ex, SpelMessage.EXCEPTION_RUNNING_COMPILED_EXPRESSION);
			}
		}
	}

	ExpressionState expressionState =
			new ExpressionState(getEvaluationContext(), toTypedValue(rootObject), this.configuration);
	TypedValue typedResultValue = this.ast.getTypedValue(expressionState);
	checkCompile(expressionState);
	return ExpressionUtils.convertTypedValue(
			expressionState.getEvaluationContext(), typedResultValue, expectedResultType);
}
 
Example 23
Source Project: spring-analysis-note   Source File: MethodReference.java    License: MIT License 5 votes vote down vote up
private Object[] getArguments(ExpressionState state) {
	Object[] arguments = new Object[getChildCount()];
	for (int i = 0; i < arguments.length; i++) {
		// Make the root object the active context again for evaluating the parameter expressions
		try {
			state.pushActiveContextObject(state.getScopeRootContextObject());
			arguments[i] = this.children[i].getValueInternal(state).getValue();
		}
		finally {
			state.popActiveContextObject();
		}
	}
	return arguments;
}
 
Example 24
Source Project: spring-analysis-note   Source File: OpNE.java    License: MIT License 5 votes vote down vote up
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	Object leftValue = getLeftOperand().getValueInternal(state).getValue();
	Object rightValue = getRightOperand().getValueInternal(state).getValue();
	this.leftActualDescriptor = CodeFlow.toDescriptorFromObject(leftValue);
	this.rightActualDescriptor = CodeFlow.toDescriptorFromObject(rightValue);
	return BooleanTypedValue.forValue(!equalityCheck(state.getEvaluationContext(), leftValue, rightValue));
}
 
Example 25
Source Project: spring-analysis-note   Source File: FunctionReference.java    License: MIT License 5 votes vote down vote up
/**
 * Compute the arguments to the function, they are the children of this expression node.
 * @return an array of argument values for the function call
 */
private Object[] getArguments(ExpressionState state) throws EvaluationException {
	// Compute arguments to the function
	Object[] arguments = new Object[getChildCount()];
	for (int i = 0; i < arguments.length; i++) {
		arguments[i] = this.children[i].getValueInternal(state).getValue();
	}
	return arguments;
}
 
Example 26
Source Project: spring4-understanding   Source File: Ternary.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Evaluate the condition and if true evaluate the first alternative, otherwise
 * evaluate the second alternative.
 * @param state the expression state
 * @throws EvaluationException if the condition does not evaluate correctly to
 * a boolean or there is a problem executing the chosen alternative
 */
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	Boolean value = this.children[0].getValue(state, Boolean.class);
	if (value == null) {
		throw new SpelEvaluationException(getChild(0).getStartPosition(),
				SpelMessage.TYPE_CONVERSION_ERROR, "null", "boolean");
	}
	TypedValue result = this.children[value ? 1 : 2].getValueInternal(state);
	computeExitTypeDescriptor();
	return result;
}
 
Example 27
Source Project: java-technology-stack   Source File: PropertyOrFieldReference.java    License: MIT License 5 votes vote down vote up
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	TypedValue tv = getValueInternal(state.getActiveContextObject(), state.getEvaluationContext(),
			state.getConfiguration().isAutoGrowNullReferences());
	PropertyAccessor accessorToUse = this.cachedReadAccessor;
	if (accessorToUse instanceof CompilablePropertyAccessor) {
		CompilablePropertyAccessor accessor = (CompilablePropertyAccessor) accessorToUse;
		setExitTypeDescriptor(CodeFlow.toDescriptor(accessor.getPropertyType()));
	}
	return tv;
}
 
Example 28
Source Project: spring-analysis-note   Source File: Elvis.java    License: MIT License 5 votes vote down vote up
/**
 * Evaluate the condition and if not null, return it.
 * If it is null, return the other value.
 * @param state the expression state
 * @throws EvaluationException if the condition does not evaluate correctly
 * to a boolean or there is a problem executing the chosen alternative
 */
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	TypedValue value = this.children[0].getValueInternal(state);
	// If this check is changed, the generateCode method will need changing too
	if (!StringUtils.isEmpty(value.getValue())) {
		return value;
	}
	else {
		TypedValue result = this.children[1].getValueInternal(state);
		computeExitTypeDescriptor();
		return result;
	}
}
 
Example 29
Source Project: java-technology-stack   Source File: OperatorNot.java    License: MIT License 5 votes vote down vote up
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	try {
		Boolean value = this.children[0].getValue(state, Boolean.class);
		if (value == null) {
			throw new SpelEvaluationException(SpelMessage.TYPE_CONVERSION_ERROR, "null", "boolean");
		}
		return BooleanTypedValue.forValue(!value);
	}
	catch (SpelEvaluationException ex) {
		ex.setPosition(getChild(0).getStartPosition());
		throw ex;
	}
}
 
Example 30
Source Project: lams   Source File: SpelExpression.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public Object getValue(EvaluationContext context) throws EvaluationException {
	Assert.notNull(context, "EvaluationContext is required");

	if (this.compiledAst != null) {
		try {
			TypedValue contextRoot = context.getRootObject();
			return this.compiledAst.getValue(contextRoot.getValue(), context);
		}
		catch (Throwable ex) {
			// If running in mixed mode, revert to interpreted
			if (this.configuration.getCompilerMode() == SpelCompilerMode.MIXED) {
				this.interpretedCount = 0;
				this.compiledAst = null;
			}
			else {
				// Running in SpelCompilerMode.immediate mode - propagate exception to caller
				throw new SpelEvaluationException(ex, SpelMessage.EXCEPTION_RUNNING_COMPILED_EXPRESSION);
			}
		}
	}

	ExpressionState expressionState = new ExpressionState(context, this.configuration);
	Object result = this.ast.getValue(expressionState);
	checkCompile(expressionState);
	return result;
}