001/*
002 * Copyright 2002-2014 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.expression.spel;
018
019import org.springframework.expression.EvaluationException;
020import org.springframework.expression.TypedValue;
021
022/**
023 * Represents a node in the Ast for a parsed expression.
024 *
025 * @author Andy Clement
026 * @since 3.0
027 */
028public interface SpelNode {
029
030        /**
031         * Evaluate the expression node in the context of the supplied expression state
032         * and return the value.
033         * @param expressionState the current expression state (includes the context)
034         * @return the value of this node evaluated against the specified state
035         */
036        Object getValue(ExpressionState expressionState) throws EvaluationException;
037
038        /**
039         * Evaluate the expression node in the context of the supplied expression state
040         * and return the typed value.
041         * @param expressionState the current expression state (includes the context)
042         * @return the type value of this node evaluated against the specified state
043         */
044        TypedValue getTypedValue(ExpressionState expressionState) throws EvaluationException;
045
046        /**
047         * Determine if this expression node will support a setValue() call.
048         * @param expressionState the current expression state (includes the context)
049         * @return true if the expression node will allow setValue()
050         * @throws EvaluationException if something went wrong trying to determine
051         * if the node supports writing
052         */
053        boolean isWritable(ExpressionState expressionState) throws EvaluationException;
054
055        /**
056         * Evaluate the expression to a node and then set the new value on that node.
057         * For example, if the expression evaluates to a property reference, then the
058         * property will be set to the new value.
059         * @param expressionState the current expression state (includes the context)
060         * @param newValue the new value
061         * @throws EvaluationException if any problem occurs evaluating the expression or
062         * setting the new value
063         */
064        void setValue(ExpressionState expressionState, Object newValue) throws EvaluationException;
065
066        /**
067         * @return the string form of this AST node
068         */
069        String toStringAST();
070
071        /**
072         * @return the number of children under this node
073         */
074        int getChildCount();
075
076        /**
077         * Helper method that returns a SpelNode rather than an Antlr Tree node.
078         * @return the child node cast to a SpelNode
079         */
080        SpelNode getChild(int index);
081
082        /**
083         * Determine the class of the object passed in, unless it is already a class object.
084         * @param obj the object that the caller wants the class of
085         * @return the class of the object if it is not already a class object,
086         * or {@code null} if the object is {@code null}
087         */
088        Class<?> getObjectClass(Object obj);
089
090        /**
091         * @return the start position of this Ast node in the expression string
092         */
093        int getStartPosition();
094
095        /**
096         * @return the end position of this Ast node in the expression string
097         */
098        int getEndPosition();
099
100}