View Javadoc
1   /*
2    * Copyright 2002-2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.expression.spel;
18  
19  import org.springframework.expression.EvaluationException;
20  import org.springframework.expression.TypedValue;
21  
22  /**
23   * Represents a node in the Ast for a parsed expression.
24   *
25   * @author Andy Clement
26   * @since 3.0
27   */
28  public interface SpelNode {
29  
30  	/**
31  	 * Evaluate the expression node in the context of the supplied expression state
32  	 * and return the value.
33  	 * @param expressionState the current expression state (includes the context)
34  	 * @return the value of this node evaluated against the specified state
35  	 */
36  	Object getValue(ExpressionState expressionState) throws EvaluationException;
37  
38  	/**
39  	 * Evaluate the expression node in the context of the supplied expression state
40  	 * and return the typed value.
41  	 * @param expressionState the current expression state (includes the context)
42  	 * @return the type value of this node evaluated against the specified state
43  	 */
44  	TypedValue getTypedValue(ExpressionState expressionState) throws EvaluationException;
45  
46  	/**
47  	 * Determine if this expression node will support a setValue() call.
48  	 * @param expressionState the current expression state (includes the context)
49  	 * @return true if the expression node will allow setValue()
50  	 * @throws EvaluationException if something went wrong trying to determine
51  	 * if the node supports writing
52  	 */
53  	boolean isWritable(ExpressionState expressionState) throws EvaluationException;
54  
55  	/**
56  	 * Evaluate the expression to a node and then set the new value on that node.
57  	 * For example, if the expression evaluates to a property reference, then the
58  	 * property will be set to the new value.
59  	 * @param expressionState the current expression state (includes the context)
60  	 * @param newValue the new value
61  	 * @throws EvaluationException if any problem occurs evaluating the expression or
62  	 * setting the new value
63  	 */
64  	void setValue(ExpressionState expressionState, Object newValue) throws EvaluationException;
65  
66  	/**
67  	 * @return the string form of this AST node
68  	 */
69  	String toStringAST();
70  
71  	/**
72  	 * @return the number of children under this node
73  	 */
74  	int getChildCount();
75  
76  	/**
77  	 * Helper method that returns a SpelNode rather than an Antlr Tree node.
78  	 * @return the child node cast to a SpelNode
79  	 */
80  	SpelNode getChild(int index);
81  
82  	/**
83  	 * Determine the class of the object passed in, unless it is already a class object.
84  	 * @param obj the object that the caller wants the class of
85  	 * @return the class of the object if it is not already a class object,
86  	 * or {@code null} if the object is {@code null}
87  	 */
88  	Class<?> getObjectClass(Object obj);
89  
90  	/**
91  	 * @return the start position of this Ast node in the expression string
92  	 */
93  	int getStartPosition();
94  
95  	/**
96  	 * @return the end position of this Ast node in the expression string
97  	 */
98  	int getEndPosition();
99  
100 }