org.springframework.expression.spel.SpelMessage Java Examples

The following examples show how to use org.springframework.expression.spel.SpelMessage. 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: 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 #2
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 #3
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 #4
Source File: ConstructorReference.java    From lams with 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 #5
Source File: InternalSpelExpressionParser.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@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 #6
Source File: OperatorInstanceof.java    From spring4-understanding with 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 #7
Source File: InternalSpelExpressionParser.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * 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 #8
Source File: Indexer.java    From spring-analysis-note 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 #9
Source File: InternalSpelExpressionParser.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 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 #10
Source File: Tokenizer.java    From spring4-understanding with 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 #11
Source File: SpelParserTests.java    From spring-analysis-note with 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 #12
Source File: InternalSpelExpressionParser.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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 #13
Source File: InternalSpelExpressionParser.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
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 #14
Source File: Tokenizer.java    From lams with 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 #15
Source File: InternalSpelExpressionParser.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * 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 #16
Source File: InternalSpelExpressionParser.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 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 #17
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 #18
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 #19
Source File: SpelExpression.java    From spring4-understanding with 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 #20
Source File: Literal.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Process the string form of a number, using the specified base if supplied
 * and return an appropriate literal to hold it. Any suffix to indicate a
 * long will be taken into account (either 'l' or 'L' is supported).
 * @param numberToken the token holding the number as its payload (eg. 1234 or 0xCAFE)
 * @param radix the base of number
 * @return a subtype of Literal that can represent it
 */
public static Literal getIntLiteral(String numberToken, int pos, int radix) {
	try {
		int value = Integer.parseInt(numberToken, radix);
		return new IntLiteral(numberToken, pos, value);
	}
	catch (NumberFormatException ex) {
		throw new InternalParseException(new SpelParseException(pos>>16, ex, SpelMessage.NOT_AN_INTEGER, numberToken));
	}
}
 
Example #21
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 #22
Source File: OperatorInstanceof.java    From java-technology-stack with MIT License 5 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 {@code true} if the left operand is an instanceof of the right operand,
 * otherwise {@code false}
 * @throws EvaluationException if there is a problem evaluating the expression
 */
@Override
public BooleanTypedValue getValueInternal(ExpressionState state) throws EvaluationException {
	SpelNodeImpl rightOperand = getRightOperand();
	TypedValue left = getLeftOperand().getValueInternal(state);
	TypedValue right = rightOperand.getValueInternal(state);
	Object leftValue = left.getValue();
	Object rightValue = right.getValue();
	BooleanTypedValue result;
	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;
	if (rightOperand instanceof TypeReference) {
		// Can only generate bytecode where the right operand is a direct type reference,
		// not if it is indirect (for example when right operand is a variable reference)
		this.exitTypeDescriptor = "Z";
	}
	return result;
}
 
Example #23
Source File: SpelParserTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void checkNumberError(String expression, SpelMessage expectedMessage) {
	try {
		SpelExpressionParser parser = new SpelExpressionParser();
		parser.parseRaw(expression);
		fail();
	}
	catch (ParseException ex) {
		assertTrue(ex instanceof SpelParseException);
		SpelParseException spe = (SpelParseException) ex;
		assertEquals(expectedMessage, spe.getMessageCode());
	}
}
 
Example #24
Source File: ValueRef.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void setValue(@Nullable Object newValue) {
	// The exception position '0' isn't right but the overhead of creating
	// instances of this per node (where the node is solely for error reporting)
	// would be unfortunate.
	throw new SpelEvaluationException(0, SpelMessage.NOT_ASSIGNABLE, "null");
}
 
Example #25
Source File: SpelExpression.java    From spring-analysis-note 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.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(context, this.configuration);
	TypedValue typedResultValue = this.ast.getTypedValue(expressionState);
	checkCompile(expressionState);
	return ExpressionUtils.convertTypedValue(context, typedResultValue, expectedResultType);
}
 
Example #26
Source File: Indexer.java    From spring4-understanding with Apache License 2.0 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: SpelExpression.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
@Override
@Nullable
public <T> T getValue(Object rootObject, @Nullable Class<T> expectedResultType) throws EvaluationException {
	if (this.compiledAst != null) {
		try {
			Object result = this.compiledAst.getValue(rootObject, getEvaluationContext());
			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.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);
	TypedValue typedResultValue = this.ast.getTypedValue(expressionState);
	checkCompile(expressionState);
	return ExpressionUtils.convertTypedValue(
			expressionState.getEvaluationContext(), typedResultValue, expectedResultType);
}
 
Example #28
Source File: InternalSpelExpressionParser.java    From java-technology-stack with MIT License 5 votes vote down vote up
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 #29
Source File: MethodReference.java    From lams with 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 #30
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());
	}
}