Java Code Examples for org.springframework.expression.spel.ast.SpelNodeImpl

The following examples show how to use org.springframework.expression.spel.ast.SpelNodeImpl. 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
@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 2
@Nullable
private SpelNodeImpl eatPrimaryExpression() {
	SpelNodeImpl start = eatStartNode();  // always a start node
	List<SpelNodeImpl> nodes = null;
	SpelNodeImpl node = eatNode();
	while (node != null) {
		if (nodes == null) {
			nodes = new ArrayList<>(4);
			nodes.add(start);
		}
		nodes.add(node);
		node = eatNode();
	}
	if (start == null || nodes == null) {
		return start;
	}
	return new CompoundExpression(start.getStartPosition(), nodes.get(nodes.size() - 1).getEndPosition(),
			nodes.toArray(new SpelNodeImpl[0]));
}
 
Example 3
private boolean maybeEatFunctionOrVar() {
	if (!peekToken(TokenKind.HASH)) {
		return false;
	}
	Token t = takeToken();
	Token functionOrVariableName = eatToken(TokenKind.IDENTIFIER);
	SpelNodeImpl[] args = maybeEatMethodArgs();
	if (args == null) {
		push(new VariableReference(functionOrVariableName.stringValue(),
				t.startPos, functionOrVariableName.endPos));
		return true;
	}

	push(new FunctionReference(functionOrVariableName.stringValue(),
			t.startPos, functionOrVariableName.endPos, args));
	return true;
}
 
Example 4
/**
 * 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 5
@Nullable
private SpelNodeImpl eatStartNode() {
	if (maybeEatLiteral()) {
		return pop();
	}
	else if (maybeEatParenExpression()) {
		return pop();
	}
	else if (maybeEatTypeReference() || maybeEatNullReference() || maybeEatConstructorReference() ||
			maybeEatMethodOrProperty(false) || maybeEatFunctionOrVar()) {
		return pop();
	}
	else if (maybeEatBeanReference()) {
		return pop();
	}
	else if (maybeEatProjection(false) || maybeEatSelection(false) || maybeEatIndexer()) {
		return pop();
	}
	else if (maybeEatInlineListOrMap()) {
		return pop();
	}
	else {
		return null;
	}
}
 
Example 6
/**
 * 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 7
private boolean maybeEatMethodOrProperty(boolean nullSafeNavigation) {
	if (peekToken(TokenKind.IDENTIFIER)) {
		Token methodOrPropertyName = takeToken();
		SpelNodeImpl[] args = maybeEatMethodArgs();
		if (args == null) {
			// property
			push(new PropertyOrFieldReference(nullSafeNavigation, methodOrPropertyName.stringValue(),
					methodOrPropertyName.startPos, methodOrPropertyName.endPos));
			return true;
		}
		// method reference
		push(new MethodReference(nullSafeNavigation, methodOrPropertyName.stringValue(),
				methodOrPropertyName.startPos, methodOrPropertyName.endPos, args));
		// TODO what is the end position for a method reference? the name or the last arg?
		return true;
	}
	return false;
}
 
Example 8
Source Project: spring-analysis-note   Source File: SpelCompiler.java    License: MIT License 6 votes vote down vote up
/**
 * Attempt compilation of the supplied expression. A check is made to see
 * if it is compilable before compilation proceeds. The check involves
 * visiting all the nodes in the expression Ast and ensuring enough state
 * is known about them that bytecode can be generated for them.
 * @param expression the expression to compile
 * @return an instance of the class implementing the compiled expression,
 * or {@code null} if compilation is not possible
 */
@Nullable
public CompiledExpression compile(SpelNodeImpl expression) {
	if (expression.isCompilable()) {
		if (logger.isDebugEnabled()) {
			logger.debug("SpEL: compiling " + expression.toStringAST());
		}
		Class<? extends CompiledExpression> clazz = createExpressionClass(expression);
		if (clazz != null) {
			try {
				return ReflectionUtils.accessibleConstructor(clazz).newInstance();
			}
			catch (Throwable ex) {
				throw new IllegalStateException("Failed to instantiate CompiledExpression", ex);
			}
		}
	}

	if (logger.isDebugEnabled()) {
		logger.debug("SpEL: unable to compile " + expression.toStringAST());
	}
	return null;
}
 
Example 9
@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 10
@Nullable
private SpelNodeImpl eatPrimaryExpression() {
	SpelNodeImpl start = eatStartNode();  // always a start node
	List<SpelNodeImpl> nodes = null;
	SpelNodeImpl node = eatNode();
	while (node != null) {
		if (nodes == null) {
			nodes = new ArrayList<>(4);
			nodes.add(start);
		}
		nodes.add(node);
		node = eatNode();
	}
	if (start == null || nodes == null) {
		return start;
	}
	return new CompoundExpression(toPos(start.getStartPosition(),
			nodes.get(nodes.size() - 1).getEndPosition()),
			nodes.toArray(new SpelNodeImpl[0]));
}
 
Example 11
private boolean maybeEatFunctionOrVar() {
	if (!peekToken(TokenKind.HASH)) {
		return false;
	}
	Token t = takeToken();
	Token functionOrVariableName = eatToken(TokenKind.IDENTIFIER);
	SpelNodeImpl[] args = maybeEatMethodArgs();
	if (args == null) {
		push(new VariableReference(functionOrVariableName.stringValue(),
				toPos(t.startPos, functionOrVariableName.endPos)));
		return true;
	}

	push(new FunctionReference(functionOrVariableName.stringValue(),
			toPos(t.startPos, functionOrVariableName.endPos), args));
	return true;
}
 
Example 12
/**
 * 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 13
@Nullable
private SpelNodeImpl eatStartNode() {
	if (maybeEatLiteral()) {
		return pop();
	}
	else if (maybeEatParenExpression()) {
		return pop();
	}
	else if (maybeEatTypeReference() || maybeEatNullReference() || maybeEatConstructorReference() ||
			maybeEatMethodOrProperty(false) || maybeEatFunctionOrVar()) {
		return pop();
	}
	else if (maybeEatBeanReference()) {
		return pop();
	}
	else if (maybeEatProjection(false) || maybeEatSelection(false) || maybeEatIndexer()) {
		return pop();
	}
	else if (maybeEatInlineListOrMap()) {
		return pop();
	}
	else {
		return null;
	}
}
 
Example 14
private boolean maybeEatSelection(boolean nullSafeNavigation) {
	Token t = peekToken();
	if (!peekSelectToken()) {
		return false;
	}
	Assert.state(t != null, "No token");
	nextToken();
	SpelNodeImpl expr = eatExpression();
	if (expr == null) {
		throw internalException(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 15
/**
 * 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(), toPos(node)));
		}
		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());
	}
	int pos = toPos(qualifiedIdPieces.getFirst().getStartPosition(), qualifiedIdPieces.getLast().getEndPosition());
	return new QualifiedIdentifier(pos, qualifiedIdPieces.toArray(new SpelNodeImpl[0]));
}
 
Example 16
private boolean maybeEatMethodOrProperty(boolean nullSafeNavigation) {
	if (peekToken(TokenKind.IDENTIFIER)) {
		Token methodOrPropertyName = takeToken();
		SpelNodeImpl[] args = maybeEatMethodArgs();
		if (args == null) {
			// property
			push(new PropertyOrFieldReference(nullSafeNavigation, methodOrPropertyName.stringValue(),
					toPos(methodOrPropertyName)));
			return true;
		}
		// method reference
		push(new MethodReference(nullSafeNavigation, methodOrPropertyName.stringValue(),
				toPos(methodOrPropertyName), args));
		// TODO what is the end position for a method reference? the name or the last arg?
		return true;
	}
	return false;
}
 
Example 17
@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 18
Source Project: java-technology-stack   Source File: SpelCompiler.java    License: MIT License 6 votes vote down vote up
/**
 * Attempt compilation of the supplied expression. A check is made to see
 * if it is compilable before compilation proceeds. The check involves
 * visiting all the nodes in the expression Ast and ensuring enough state
 * is known about them that bytecode can be generated for them.
 * @param expression the expression to compile
 * @return an instance of the class implementing the compiled expression,
 * or {@code null} if compilation is not possible
 */
@Nullable
public CompiledExpression compile(SpelNodeImpl expression) {
	if (expression.isCompilable()) {
		if (logger.isDebugEnabled()) {
			logger.debug("SpEL: compiling " + expression.toStringAST());
		}
		Class<? extends CompiledExpression> clazz = createExpressionClass(expression);
		if (clazz != null) {
			try {
				return ReflectionUtils.accessibleConstructor(clazz).newInstance();
			}
			catch (Throwable ex) {
				throw new IllegalStateException("Failed to instantiate CompiledExpression", ex);
			}
		}
	}

	if (logger.isDebugEnabled()) {
		logger.debug("SpEL: unable to compile " + expression.toStringAST());
	}
	return null;
}
 
Example 19
@Override
protected SpelExpression doParseExpression(String expressionString, 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();
		if (moreTokens()) {
			throw new SpelParseException(peekToken().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 20
private boolean maybeEatNode() {
	SpelNodeImpl expr = null;
	if (peekToken(TokenKind.DOT, TokenKind.SAFE_NAVI)) {
		expr = eatDottedNode();
	}
	else {
		expr = maybeEatNonDottedNode();
	}

	if (expr == null) {
		return false;
	}
	else {
		push(expr);
		return true;
	}
}
 
Example 21
private SpelNodeImpl eatDottedNode() {
	Token t = nextToken();  // it was a '.' or a '?.'
	boolean nullSafeNavigation = (t.kind == TokenKind.SAFE_NAVI);
	if (maybeEatMethodOrProperty(nullSafeNavigation) || maybeEatFunctionOrVar() ||
			maybeEatProjection(nullSafeNavigation) || maybeEatSelection(nullSafeNavigation)) {
		return pop();
	}
	if (peekToken() == null) {
		// unexpectedly ran out of data
		raiseInternalException(t.startPos, SpelMessage.OOD);
	}
	else {
		raiseInternalException(t.startPos, SpelMessage.UNEXPECTED_DATA_AFTER_DOT, toString(peekToken()));
	}
	return null;
}
 
Example 22
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 23
/**
 * 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 24
/**
 * 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 25
/**
 * 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 26
private boolean maybeEatMethodOrProperty(boolean nullSafeNavigation) {
	if (peekToken(TokenKind.IDENTIFIER)) {
		Token methodOrPropertyName = nextToken();
		SpelNodeImpl[] args = maybeEatMethodArgs();
		if (args == null) {
			// property
			push(new PropertyOrFieldReference(nullSafeNavigation, methodOrPropertyName.data,
					toPos(methodOrPropertyName)));
			return true;
		}
		// method reference
		push(new MethodReference(nullSafeNavigation, methodOrPropertyName.data,
				toPos(methodOrPropertyName), args));
		// TODO what is the end position for a method reference? the name or the last arg?
		return true;
	}
	return false;
}
 
Example 27
Source Project: lams   Source File: SpelCompiler.java    License: GNU General Public License v2.0 6 votes vote down vote up
/**
 * Attempt compilation of the supplied expression. A check is made to see
 * if it is compilable before compilation proceeds. The check involves
 * visiting all the nodes in the expression Ast and ensuring enough state
 * is known about them that bytecode can be generated for them.
 * @param expression the expression to compile
 * @return an instance of the class implementing the compiled expression,
 * or {@code null} if compilation is not possible
 */
public CompiledExpression compile(SpelNodeImpl expression) {
	if (expression.isCompilable()) {
		if (logger.isDebugEnabled()) {
			logger.debug("SpEL: compiling " + expression.toStringAST());
		}
		Class<? extends CompiledExpression> clazz = createExpressionClass(expression);
		if (clazz != null) {
			try {
				return clazz.newInstance();
			}
			catch (Throwable ex) {
				throw new IllegalStateException("Failed to instantiate CompiledExpression", ex);
			}
		}
	}

	if (logger.isDebugEnabled()) {
		logger.debug("SpEL: unable to compile " + expression.toStringAST());
	}
	return null;
}
 
Example 28
private SpelNodeImpl eatDottedNode() {
	Token t = takeToken();  // it was a '.' or a '?.'
	boolean nullSafeNavigation = (t.kind == TokenKind.SAFE_NAVI);
	if (maybeEatMethodOrProperty(nullSafeNavigation) || maybeEatFunctionOrVar() ||
			maybeEatProjection(nullSafeNavigation) || maybeEatSelection(nullSafeNavigation)) {
		return pop();
	}
	if (peekToken() == null) {
		// unexpectedly ran out of data
		throw internalException(t.startPos, SpelMessage.OOD);
	}
	else {
		throw internalException(t.startPos, SpelMessage.UNEXPECTED_DATA_AFTER_DOT, toString(peekToken()));
	}
}
 
Example 29
@Nullable
private SpelNodeImpl[] maybeEatMethodArgs() {
	if (!peekToken(TokenKind.LPAREN)) {
		return null;
	}
	List<SpelNodeImpl> args = new ArrayList<>();
	consumeArguments(args);
	eatToken(TokenKind.RPAREN);
	return args.toArray(new SpelNodeImpl[0]);
}
 
Example 30
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);
}