org.springframework.expression.spel.SpelEvaluationException Java Examples

The following examples show how to use org.springframework.expression.spel.SpelEvaluationException. 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: StandardTypeLocator.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Find a (possibly unqualified) type reference - first using the type name as-is,
 * then trying any registered prefixes if the type name cannot be found.
 * @param typeName the type to locate
 * @return the class object for the type
 * @throws EvaluationException if the type cannot be found
 */
@Override
public Class<?> findType(String typeName) throws EvaluationException {
	String nameToLookup = typeName;
	try {
		return ClassUtils.forName(nameToLookup, this.classLoader);
	}
	catch (ClassNotFoundException ey) {
		// try any registered prefixes before giving up
	}
	for (String prefix : this.knownPackagePrefixes) {
		try {
			nameToLookup = prefix + '.' + typeName;
			return ClassUtils.forName(nameToLookup, this.classLoader);
		}
		catch (ClassNotFoundException ex) {
			// might be a different prefix
		}
	}
	throw new SpelEvaluationException(SpelMessage.TYPE_NOT_FOUND, typeName);
}
 
Example #2
Source File: SpelExpression.java    From java-technology-stack with 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 #3
Source File: SpelExpression.java    From spring4-understanding with 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 #4
Source File: OperatorBetween.java    From spring-analysis-note with MIT License 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 #5
Source File: Indexer.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public void setValue(Object newValue) {
	growCollectionIfNecessary();
	if (this.collection instanceof List) {
		List list = (List) this.collection;
		if (this.collectionEntryDescriptor.getElementTypeDescriptor() != null) {
			newValue = this.typeConverter.convertValue(newValue, TypeDescriptor.forObject(newValue),
					this.collectionEntryDescriptor.getElementTypeDescriptor());
		}
		list.set(this.index, newValue);
	}
	else {
		throw new SpelEvaluationException(getStartPosition(), SpelMessage.INDEXING_NOT_SUPPORTED_FOR_TYPE,
				this.collectionEntryDescriptor.toString());
	}
}
 
Example #6
Source File: StandardTypeLocator.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Find a (possibly unqualified) type reference - first using the type name as-is,
 * then trying any registered prefixes if the type name cannot be found.
 * @param typeName the type to locate
 * @return the class object for the type
 * @throws EvaluationException if the type cannot be found
 */
@Override
public Class<?> findType(String typeName) throws EvaluationException {
	String nameToLookup = typeName;
	try {
		return ClassUtils.forName(nameToLookup, this.classLoader);
	}
	catch (ClassNotFoundException ey) {
		// try any registered prefixes before giving up
	}
	for (String prefix : this.knownPackagePrefixes) {
		try {
			nameToLookup = prefix + '.' + typeName;
			return ClassUtils.forName(nameToLookup, this.classLoader);
		}
		catch (ClassNotFoundException ex) {
			// might be a different prefix
		}
	}
	throw new SpelEvaluationException(SpelMessage.TYPE_NOT_FOUND, typeName);
}
 
Example #7
Source File: OperatorBetween.java    From spring4-understanding with 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 #8
Source File: FunctionReference.java    From spring4-understanding with 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 #9
Source File: FunctionReference.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public TypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	TypedValue value = state.lookupVariable(this.name);
	if (value == TypedValue.NULL) {
		throw new SpelEvaluationException(getStartPosition(), SpelMessage.FUNCTION_NOT_DEFINED, this.name);
	}
	if (!(value.getValue() instanceof Method)) {
		// Possibly a static Java method registered as a function
		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 #10
Source File: BeanReference.java    From spring-analysis-note with 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 #11
Source File: ConstructorReference.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Go through the list of registered constructor resolvers and see if any can find a
 * constructor that takes the specified set of arguments.
 * @param typeName the type trying to be constructed
 * @param argumentTypes the types of the arguments supplied that the constructor must take
 * @param state the current state of the expression
 * @return a reusable ConstructorExecutor that can be invoked to run the constructor or null
 * @throws SpelEvaluationException if there is a problem locating the constructor
 */
private ConstructorExecutor findExecutorForConstructor(String typeName,
		List<TypeDescriptor> argumentTypes, ExpressionState state)
		throws SpelEvaluationException {

	EvaluationContext evalContext = state.getEvaluationContext();
	List<ConstructorResolver> ctorResolvers = evalContext.getConstructorResolvers();
	if (ctorResolvers != null) {
		for (ConstructorResolver ctorResolver : ctorResolvers) {
			try {
				ConstructorExecutor ce = ctorResolver.resolve(state.getEvaluationContext(), typeName, argumentTypes);
				if (ce != null) {
					return ce;
				}
			}
			catch (AccessException ex) {
				throw new SpelEvaluationException(getStartPosition(), ex,
						SpelMessage.CONSTRUCTOR_INVOCATION_PROBLEM, typeName,
						FormatHelper.formatMethodForMessage("", argumentTypes));
			}
		}
	}
	throw new SpelEvaluationException(getStartPosition(), SpelMessage.CONSTRUCTOR_NOT_FOUND, typeName,
			FormatHelper.formatMethodForMessage("", argumentTypes));
}
 
Example #12
Source File: OperatorBetween.java    From java-technology-stack with MIT License 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 #13
Source File: MethodReference.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private MethodExecutor findAccessorForMethod(String name, List<TypeDescriptor> argumentTypes,
		Object targetObject, EvaluationContext evaluationContext) throws SpelEvaluationException {

	List<MethodResolver> methodResolvers = evaluationContext.getMethodResolvers();
	if (methodResolvers != null) {
		for (MethodResolver methodResolver : methodResolvers) {
			try {
				MethodExecutor methodExecutor = methodResolver.resolve(
						evaluationContext, targetObject, name, argumentTypes);
				if (methodExecutor != null) {
					return methodExecutor;
				}
			}
			catch (AccessException ex) {
				throw new SpelEvaluationException(getStartPosition(), ex,
						SpelMessage.PROBLEM_LOCATING_METHOD, name, targetObject.getClass());
			}
		}
	}

	throw new SpelEvaluationException(getStartPosition(), SpelMessage.METHOD_NOT_FOUND,
			FormatHelper.formatMethodForMessage(name, argumentTypes),
			FormatHelper.formatClassNameForMessage(
					targetObject instanceof Class ? ((Class<?>) targetObject) : targetObject.getClass()));
}
 
Example #14
Source File: FunctionReference.java    From lams with GNU General Public License v2.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 #15
Source File: SpelExpression.java    From spring-analysis-note with 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.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(), this.configuration);
	Object result = this.ast.getValue(expressionState);
	checkCompile(expressionState);
	return result;
}
 
Example #16
Source File: SpelExpression.java    From spring-analysis-note with 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 #17
Source File: BeanReference.java    From java-technology-stack with 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 #18
Source File: ConstructorReference.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Go through the list of registered constructor resolvers and see if any can find a
 * constructor that takes the specified set of arguments.
 * @param typeName the type trying to be constructed
 * @param argumentTypes the types of the arguments supplied that the constructor must take
 * @param state the current state of the expression
 * @return a reusable ConstructorExecutor that can be invoked to run the constructor or null
 * @throws SpelEvaluationException if there is a problem locating the constructor
 */
private ConstructorExecutor findExecutorForConstructor(String typeName,
		List<TypeDescriptor> argumentTypes, ExpressionState state) throws SpelEvaluationException {

	EvaluationContext evalContext = state.getEvaluationContext();
	List<ConstructorResolver> ctorResolvers = evalContext.getConstructorResolvers();
	for (ConstructorResolver ctorResolver : ctorResolvers) {
		try {
			ConstructorExecutor ce = ctorResolver.resolve(state.getEvaluationContext(), typeName, argumentTypes);
			if (ce != null) {
				return ce;
			}
		}
		catch (AccessException ex) {
			throw new SpelEvaluationException(getStartPosition(), ex,
					SpelMessage.CONSTRUCTOR_INVOCATION_PROBLEM, typeName,
					FormatHelper.formatMethodForMessage("", argumentTypes));
		}
	}
	throw new SpelEvaluationException(getStartPosition(), SpelMessage.CONSTRUCTOR_NOT_FOUND, typeName,
			FormatHelper.formatMethodForMessage("", argumentTypes));
}
 
Example #19
Source File: Indexer.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public void setValue(@Nullable Object newValue) {
	growCollectionIfNecessary();
	if (this.collection instanceof List) {
		List list = (List) this.collection;
		if (this.collectionEntryDescriptor.getElementTypeDescriptor() != null) {
			newValue = this.typeConverter.convertValue(newValue, TypeDescriptor.forObject(newValue),
					this.collectionEntryDescriptor.getElementTypeDescriptor());
		}
		list.set(this.index, newValue);
	}
	else {
		throw new SpelEvaluationException(getStartPosition(), SpelMessage.INDEXING_NOT_SUPPORTED_FOR_TYPE,
				this.collectionEntryDescriptor.toString());
	}
}
 
Example #20
Source File: SpelExpression.java    From java-technology-stack with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
@Nullable
public <T> T getValue(@Nullable Class<T> expectedResultType) throws EvaluationException {
	if (this.compiledAst != null) {
		try {
			EvaluationContext context = getEvaluationContext();
			Object result = this.compiledAst.getValue(context.getRootObject().getValue(), context);
			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(), this.configuration);
	TypedValue typedResultValue = this.ast.getTypedValue(expressionState);
	checkCompile(expressionState);
	return ExpressionUtils.convertTypedValue(
			expressionState.getEvaluationContext(), typedResultValue, expectedResultType);
}
 
Example #21
Source File: MethodReference.java    From java-technology-stack with MIT License 5 votes vote down vote up
private MethodExecutor findAccessorForMethod(List<TypeDescriptor> argumentTypes, Object targetObject,
		EvaluationContext evaluationContext) throws SpelEvaluationException {

	AccessException accessException = null;
	List<MethodResolver> methodResolvers = evaluationContext.getMethodResolvers();
	for (MethodResolver methodResolver : methodResolvers) {
		try {
			MethodExecutor methodExecutor = methodResolver.resolve(
					evaluationContext, targetObject, this.name, argumentTypes);
			if (methodExecutor != null) {
				return methodExecutor;
			}
		}
		catch (AccessException ex) {
			accessException = ex;
			break;
		}
	}

	String method = FormatHelper.formatMethodForMessage(this.name, argumentTypes);
	String className = FormatHelper.formatClassNameForMessage(
			targetObject instanceof Class ? ((Class<?>) targetObject) : targetObject.getClass());
	if (accessException != null) {
		throw new SpelEvaluationException(
				getStartPosition(), accessException, SpelMessage.PROBLEM_LOCATING_METHOD, method, className);
	}
	else {
		throw new SpelEvaluationException(getStartPosition(), SpelMessage.METHOD_NOT_FOUND, method, className);
	}
}
 
Example #22
Source File: OpAnd.java    From lams with GNU General Public License v2.0 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 #23
Source File: SpelExpression.java    From java-technology-stack with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
@Nullable
public <T> T getValue(EvaluationContext context, @Nullable Class<T> expectedResultType) throws EvaluationException {
	Assert.notNull(context, "EvaluationContext is required");

	if (this.compiledAst != null) {
		try {
			Object result = this.compiledAst.getValue(context.getRootObject().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 #24
Source File: SpelExpression.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
@Nullable
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 #25
Source File: SpelExpression.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
public <T> T getValue(EvaluationContext context, Object rootObject, Class<T> expectedResultType)
		throws EvaluationException {

	Assert.notNull(context, "EvaluationContext is required");

	if (this.compiledAst != null) {
		try {
			Object result = this.compiledAst.getValue(rootObject, 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, toTypedValue(rootObject), this.configuration);
	TypedValue typedResultValue = this.ast.getTypedValue(expressionState);
	checkCompile(expressionState);
	return ExpressionUtils.convertTypedValue(context, typedResultValue, expectedResultType);
}
 
Example #26
Source File: Indexer.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public TypedValue getValue() {
	if (this.index >= this.target.length()) {
		throw new SpelEvaluationException(getStartPosition(), SpelMessage.STRING_INDEX_OUT_OF_BOUNDS,
				this.target.length(), this.index);
	}
	return new TypedValue(String.valueOf(this.target.charAt(this.index)));
}
 
Example #27
Source File: StandardTypeConverter.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public Object convertValue(Object value, TypeDescriptor sourceType, TypeDescriptor targetType) {
	try {
		return this.conversionService.convert(value, sourceType, targetType);
	}
	catch (ConversionException ex) {
		throw new SpelEvaluationException(
				ex, SpelMessage.TYPE_CONVERSION_ERROR, sourceType.toString(), targetType.toString());
	}
}
 
Example #28
Source File: OpPlusTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test(expected = SpelEvaluationException.class)
public void test_unaryPlusWithStringLiteral() {
	ExpressionState expressionState = new ExpressionState(new StandardEvaluationContext());

	StringLiteral str = new StringLiteral("word", -1, -1, "word");

	OpPlus o = new OpPlus(-1, -1, str);
	o.getValueInternal(expressionState);
}
 
Example #29
Source File: StandardTypeConverter.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
@Nullable
public Object convertValue(@Nullable Object value, @Nullable TypeDescriptor sourceType, TypeDescriptor targetType) {
	try {
		return this.conversionService.convert(value, sourceType, targetType);
	}
	catch (ConversionException ex) {
		throw new SpelEvaluationException(ex, SpelMessage.TYPE_CONVERSION_ERROR,
				(sourceType != null ? sourceType.toString() : (value != null ? value.getClass().getName() : "null")),
				targetType.toString());
	}
}
 
Example #30
Source File: OpAnd.java    From java-technology-stack with 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;
	}
}