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

The following examples show how to use org.springframework.expression.spel.SpelMessage. 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: spring-analysis-note   Source File: FunctionReference.java    License: 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 2
Source Project: lams   Source File: ConstructorReference.java    License: GNU General Public License v2.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 3
Source Project: spring4-understanding   Source File: OperatorInstanceof.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Compare the left operand to see it is an instance of the type specified as the
 * right operand. The right operand must be a class.
 * @param state the expression state
 * @return true if the left operand is an instanceof of the right operand, otherwise
 *         false
 * @throws EvaluationException if there is a problem evaluating the expression
 */
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	TypedValue left = getLeftOperand().getValueInternal(state);
	TypedValue right = getRightOperand().getValueInternal(state);
	Object leftValue = left.getValue();
	Object rightValue = right.getValue();
	BooleanTypedValue result = null;
	if (rightValue == null || !(rightValue instanceof Class<?>)) {
		throw new SpelEvaluationException(getRightOperand().getStartPosition(),
				SpelMessage.INSTANCEOF_OPERATOR_NEEDS_CLASS_OPERAND,
				(rightValue == null ? "null" : rightValue.getClass().getName()));
	}
	Class<?> rightClass = (Class<?>) rightValue;
	if (leftValue == null) {
		result = BooleanTypedValue.FALSE;  // null is not an instanceof anything
	}
	else {
		result = BooleanTypedValue.forValue(rightClass.isAssignableFrom(leftValue.getClass()));
	}
	this.type = rightClass;
	this.exitTypeDescriptor = "Z";
	return result;
}
 
Example 4
Source Project: spring-analysis-note   Source File: Indexer.java    License: 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 5
/**
 * Used for consuming arguments for either a method or a constructor call
 */
private void consumeArguments(List<SpelNodeImpl> accumulatedArguments) {
	int pos = peekToken().startPos;
	Token next;
	do {
		nextToken();  // consume ( (first time through) or comma (subsequent times)
		Token t = peekToken();
		if (t == null) {
			raiseInternalException(pos, SpelMessage.RUN_OUT_OF_ARGUMENTS);
		}
		if (t.kind != TokenKind.RPAREN) {
			accumulatedArguments.add(eatExpression());
		}
		next = peekToken();
	}
	while (next != null && next.kind == TokenKind.COMMA);

	if (next == null) {
		raiseInternalException(pos, SpelMessage.RUN_OUT_OF_ARGUMENTS);
	}
}
 
Example 6
@Override
protected SpelExpression doParseExpression(String expressionString, @Nullable ParserContext context)
		throws ParseException {

	try {
		this.expressionString = expressionString;
		Tokenizer tokenizer = new Tokenizer(expressionString);
		this.tokenStream = tokenizer.process();
		this.tokenStreamLength = this.tokenStream.size();
		this.tokenStreamPointer = 0;
		this.constructedNodes.clear();
		SpelNodeImpl ast = eatExpression();
		Assert.state(ast != null, "No node");
		Token t = peekToken();
		if (t != null) {
			throw new SpelParseException(t.startPos, SpelMessage.MORE_INPUT, toString(nextToken()));
		}
		Assert.isTrue(this.constructedNodes.isEmpty(), "At least one node expected");
		return new SpelExpression(expressionString, ast, this.configuration);
	}
	catch (InternalParseException ex) {
		throw ex.getCause();
	}
}
 
Example 7
private boolean maybeEatSelection(boolean nullSafeNavigation) {
	Token t = peekToken();
	if (!peekSelectToken()) {
		return false;
	}
	nextToken();
	SpelNodeImpl expr = eatExpression();
	if (expr == null) {
		raiseInternalException(toPos(t), SpelMessage.MISSING_SELECTION_EXPRESSION);
	}
	eatToken(TokenKind.RSQUARE);
	if (t.kind == TokenKind.SELECT_FIRST) {
		this.constructedNodes.push(new Selection(nullSafeNavigation, Selection.FIRST, toPos(t), expr));
	}
	else if (t.kind == TokenKind.SELECT_LAST) {
		this.constructedNodes.push(new Selection(nullSafeNavigation, Selection.LAST, toPos(t), expr));
	}
	else {
		this.constructedNodes.push(new Selection(nullSafeNavigation, Selection.ALL, toPos(t), expr));
	}
	return true;
}
 
Example 8
Source Project: lams   Source File: Tokenizer.java    License: GNU General Public License v2.0 6 votes vote down vote up
private void pushHexIntToken(char[] data, boolean isLong, int start, int end) {
	if (data.length == 0) {
		if (isLong) {
			raiseParseException(start, SpelMessage.NOT_A_LONG, this.expressionString.substring(start, end + 1));
		}
		else {
			raiseParseException(start, SpelMessage.NOT_AN_INTEGER, this.expressionString.substring(start, end));
		}
	}
	if (isLong) {
		this.tokens.add(new Token(TokenKind.LITERAL_HEXLONG, data, start, end));
	}
	else {
		this.tokens.add(new Token(TokenKind.LITERAL_HEXINT, data, start, end));
	}
}
 
Example 9
/**
 * Eat an identifier, possibly qualified (meaning that it is dotted).
 * TODO AndyC Could create complete identifiers (a.b.c) here rather than a sequence of them? (a, b, c)
 */
private SpelNodeImpl eatPossiblyQualifiedId() {
	Deque<SpelNodeImpl> qualifiedIdPieces = new ArrayDeque<>();
	Token node = peekToken();
	while (isValidQualifiedId(node)) {
		nextToken();
		if (node.kind != TokenKind.DOT) {
			qualifiedIdPieces.add(new Identifier(node.stringValue(), node.startPos, node.endPos));
		}
		node = peekToken();
	}
	if (qualifiedIdPieces.isEmpty()) {
		if (node == null) {
			throw internalException( this.expressionString.length(), SpelMessage.OOD);
		}
		throw internalException(node.startPos, SpelMessage.NOT_EXPECTED_TOKEN,
				"qualified ID", node.getKind().toString().toLowerCase());
	}
	return new QualifiedIdentifier(qualifiedIdPieces.getFirst().getStartPosition(),
			qualifiedIdPieces.getLast().getEndPosition(), qualifiedIdPieces.toArray(new SpelNodeImpl[0]));
}
 
Example 10
Source Project: spring-analysis-note   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.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 11
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 12
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 13
Source Project: spring4-understanding   Source File: Indexer.java    License: 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 14
/**
 * Eat an identifier, possibly qualified (meaning that it is dotted).
 * TODO AndyC Could create complete identifiers (a.b.c) here rather than a sequence of them? (a, b, c)
 */
private SpelNodeImpl eatPossiblyQualifiedId() {
	LinkedList<SpelNodeImpl> qualifiedIdPieces = new LinkedList<SpelNodeImpl>();
	Token node = peekToken();
	while (isValidQualifiedId(node)) {
		nextToken();
		if (node.kind != TokenKind.DOT) {
			qualifiedIdPieces.add(new Identifier(node.stringValue(),toPos(node)));
		}
		node = peekToken();
	}
	if (qualifiedIdPieces.isEmpty()) {
		if (node == null) {
			raiseInternalException( this.expressionString.length(), SpelMessage.OOD);
		}
		raiseInternalException(node.startPos, SpelMessage.NOT_EXPECTED_TOKEN,
				"qualified ID", node.getKind().toString().toLowerCase());
	}
	int pos = toPos(qualifiedIdPieces.getFirst().getStartPosition(), qualifiedIdPieces.getLast().getEndPosition());
	return new QualifiedIdentifier(pos, qualifiedIdPieces.toArray(new SpelNodeImpl[qualifiedIdPieces.size()]));
}
 
Example 15
Source Project: spring-analysis-note   Source File: SpelParserTests.java    License: MIT License 6 votes vote down vote up
@Test
public void numerics() {
	checkNumber("2", 2, Integer.class);
	checkNumber("22", 22, Integer.class);
	checkNumber("+22", 22, Integer.class);
	checkNumber("-22", -22, Integer.class);
	checkNumber("2L", 2L, Long.class);
	checkNumber("22l", 22L, Long.class);

	checkNumber("0x1", 1, Integer.class);
	checkNumber("0x1L", 1L, Long.class);
	checkNumber("0xa", 10, Integer.class);
	checkNumber("0xAL", 10L, Long.class);

	checkNumberError("0x", SpelMessage.NOT_AN_INTEGER);
	checkNumberError("0xL", SpelMessage.NOT_A_LONG);
	checkNumberError(".324", SpelMessage.UNEXPECTED_DATA_AFTER_DOT);
	checkNumberError("3.4L", SpelMessage.REAL_CANNOT_BE_LONG);

	checkNumber("3.5f", 3.5f, Float.class);
	checkNumber("1.2e3", 1.2e3d, Double.class);
	checkNumber("1.2e+3", 1.2e3d, Double.class);
	checkNumber("1.2e-3", 1.2e-3d, Double.class);
	checkNumber("1.2e3", 1.2e3d, Double.class);
	checkNumber("1e+3", 1e3d, Double.class);
}
 
Example 16
Source Project: spring4-understanding   Source File: Tokenizer.java    License: Apache License 2.0 6 votes vote down vote up
private void lexDoubleQuotedStringLiteral() {
	int start = this.pos;
	boolean terminated = false;
	while (!terminated) {
		this.pos++;
		char ch = this.toProcess[this.pos];
		if (ch == '"') {
			// may not be the end if the char after is also a "
			if (this.toProcess[this.pos + 1] == '"') {
				this.pos++; // skip over that too, and continue
			}
			else {
				terminated = true;
			}
		}
		if (ch == 0) {
			throw new InternalParseException(new SpelParseException(this.expressionString,
					start, SpelMessage.NON_TERMINATING_DOUBLE_QUOTED_STRING));
		}
	}
	this.pos++;
	this.tokens.add(new Token(TokenKind.LITERAL_STRING, subarray(start, this.pos), start, this.pos));
}
 
Example 17
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 18
@Override
protected SpelExpression doParseExpression(String expressionString, ParserContext context) throws ParseException {
	try {
		this.expressionString = expressionString;
		Tokenizer tokenizer = new Tokenizer(expressionString);
		tokenizer.process();
		this.tokenStream = tokenizer.getTokens();
		this.tokenStreamLength = this.tokenStream.size();
		this.tokenStreamPointer = 0;
		this.constructedNodes.clear();
		SpelNodeImpl ast = eatExpression();
		if (moreTokens()) {
			throw new SpelParseException(peekToken().startPos, SpelMessage.MORE_INPUT, toString(nextToken()));
		}
		Assert.isTrue(this.constructedNodes.isEmpty());
		return new SpelExpression(expressionString, ast, this.configuration);
	}
	catch (InternalParseException ex) {
		throw ex.getCause();
	}
}
 
Example 19
/**
 * Used for consuming arguments for either a method or a constructor call.
 */
private void consumeArguments(List<SpelNodeImpl> accumulatedArguments) {
	Token t = peekToken();
	Assert.state(t != null, "Expected token");
	int pos = t.startPos;
	Token next;
	do {
		nextToken();  // consume (first time through) or comma (subsequent times)
		t = peekToken();
		if (t == null) {
			throw internalException(pos, SpelMessage.RUN_OUT_OF_ARGUMENTS);
		}
		if (t.kind != TokenKind.RPAREN) {
			accumulatedArguments.add(eatExpression());
		}
		next = peekToken();
	}
	while (next != null && next.kind == TokenKind.COMMA);

	if (next == null) {
		throw internalException(pos, SpelMessage.RUN_OUT_OF_ARGUMENTS);
	}
}
 
Example 20
Source Project: lams   Source File: MethodReference.java    License: GNU General Public License v2.0 5 votes vote down vote up
private void throwIfNotNullSafe(List<TypeDescriptor> argumentTypes) {
	if (!this.nullSafe) {
		throw new SpelEvaluationException(getStartPosition(),
				SpelMessage.METHOD_CALL_ON_NULL_OBJECT_NOT_ALLOWED,
				FormatHelper.formatMethodForMessage(this.name, argumentTypes));
	}
}
 
Example 21
Source Project: lams   Source File: FunctionReference.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Execute a function represented as a java.lang.reflect.Method.
 * @param state the expression evaluation state
 * @param method the method to invoke
 * @return the return value of the invoked Java method
 * @throws EvaluationException if there is any problem invoking the method
 */
private TypedValue executeFunctionJLRMethod(ExpressionState state, Method method) throws EvaluationException {
	this.method = null;
	Object[] functionArgs = getArguments(state);

	if (!method.isVarArgs() && method.getParameterTypes().length != functionArgs.length) {
		throw new SpelEvaluationException(SpelMessage.INCORRECT_NUMBER_OF_ARGUMENTS_TO_FUNCTION,
				functionArgs.length, method.getParameterTypes().length);
	}
	// Only static methods can be called in this way
	if (!Modifier.isStatic(method.getModifiers())) {
		throw new SpelEvaluationException(getStartPosition(),
				SpelMessage.FUNCTION_MUST_BE_STATIC, ClassUtils.getQualifiedMethodName(method), this.name);
	}

	this.argumentConversionOccurred = false;
	// Convert arguments if necessary and remap them for varargs if required
	if (functionArgs != null) {
		TypeConverter converter = state.getEvaluationContext().getTypeConverter();
		this.argumentConversionOccurred = ReflectionHelper.convertAllArguments(converter, functionArgs, method);
	}
	if (method.isVarArgs()) {
		functionArgs = ReflectionHelper.setupArgumentsForVarargsInvocation(
				method.getParameterTypes(), functionArgs);
	}

	try {
		ReflectionUtils.makeAccessible(method);
		Object result = method.invoke(method.getClass(), functionArgs);
		if (!argumentConversionOccurred) {
			this.method = method;
			this.exitTypeDescriptor = CodeFlow.toDescriptor(method.getReturnType());
		}
		return new TypedValue(result, new TypeDescriptor(new MethodParameter(method, -1)).narrow(result));
	}
	catch (Exception ex) {
		throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.EXCEPTION_DURING_FUNCTION_CALL,
				this.name, ex.getMessage());
	}
}
 
Example 22
Source Project: spring-analysis-note   Source File: Literal.java    License: MIT License 5 votes vote down vote up
public static Literal getLongLiteral(String numberToken, int startPos, int endPos, int radix) {
	try {
		long value = Long.parseLong(numberToken, radix);
		return new LongLiteral(numberToken, startPos, endPos, value);
	}
	catch (NumberFormatException ex) {
		throw new InternalParseException(new SpelParseException(startPos, ex, SpelMessage.NOT_A_LONG, numberToken));
	}
}
 
Example 23
private void eatConstructorArgs(List<SpelNodeImpl> accumulatedArguments) {
	if (!peekToken(TokenKind.LPAREN)) {
		throw new InternalParseException(new SpelParseException(this.expressionString,
				positionOf(peekToken()), SpelMessage.MISSING_CONSTRUCTOR_ARGS));
	}
	consumeArguments(accumulatedArguments);
	eatToken(TokenKind.RPAREN);
}
 
Example 24
Source Project: spring-analysis-note   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 25
Source Project: spring-analysis-note   Source File: MethodReference.java    License: MIT License 5 votes vote down vote up
private void throwIfNotNullSafe(List<TypeDescriptor> argumentTypes) {
	if (!this.nullSafe) {
		throw new SpelEvaluationException(getStartPosition(),
				SpelMessage.METHOD_CALL_ON_NULL_OBJECT_NOT_ALLOWED,
				FormatHelper.formatMethodForMessage(this.name, argumentTypes));
	}
}
 
Example 26
Source Project: spring4-understanding   Source File: SpelExpression.java    License: Apache License 2.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 27
Source Project: spring-analysis-note   Source File: Indexer.java    License: MIT License 5 votes vote down vote up
private void growCollectionIfNecessary() {
	if (this.index >= this.collection.size()) {
		if (!this.growCollection) {
			throw new SpelEvaluationException(getStartPosition(), SpelMessage.COLLECTION_INDEX_OUT_OF_BOUNDS,
					this.collection.size(), this.index);
		}
		if (this.index >= this.maximumSize) {
			throw new SpelEvaluationException(getStartPosition(), SpelMessage.UNABLE_TO_GROW_COLLECTION);
		}
		if (this.collectionEntryDescriptor.getElementTypeDescriptor() == null) {
			throw new SpelEvaluationException(
					getStartPosition(), SpelMessage.UNABLE_TO_GROW_COLLECTION_UNKNOWN_ELEMENT_TYPE);
		}
		TypeDescriptor elementType = this.collectionEntryDescriptor.getElementTypeDescriptor();
		try {
			Constructor<?> ctor = ReflectionUtils.accessibleConstructor(elementType.getType());
			int newElements = this.index - this.collection.size();
			while (newElements >= 0) {
				this.collection.add(ctor.newInstance());
				newElements--;
			}
		}
		catch (Throwable ex) {
			throw new SpelEvaluationException(getStartPosition(), ex, SpelMessage.UNABLE_TO_GROW_COLLECTION);
		}
	}
}
 
Example 28
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 29
private void eatConstructorArgs(List<SpelNodeImpl> accumulatedArguments) {
	if (!peekToken(TokenKind.LPAREN)) {
		throw new InternalParseException(new SpelParseException(this.expressionString,
				positionOf(peekToken()), SpelMessage.MISSING_CONSTRUCTOR_ARGS));
	}
	consumeArguments(accumulatedArguments);
	eatToken(TokenKind.RPAREN);
}
 
Example 30
private Token eatToken(TokenKind expectedKind) {
	Token t = nextToken();
	if (t == null) {
		int pos = this.expressionString.length();
		throw internalException(pos, SpelMessage.OOD);
	}
	if (t.kind != expectedKind) {
		throw internalException(t.startPos, SpelMessage.NOT_EXPECTED_TOKEN,
				expectedKind.toString().toLowerCase(), t.getKind().toString().toLowerCase());
	}
	return t;
}