001/*
002 * Copyright 2002-2018 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;
021import org.springframework.lang.Nullable;
022
023/**
024 * Represents a node in the AST for a parsed expression.
025 *
026 * @author Andy Clement
027 * @since 3.0
028 */
029public interface SpelNode {
030
031        /**
032         * Evaluate the expression node in the context of the supplied expression state
033         * and return the value.
034         * @param expressionState the current expression state (includes the context)
035         * @return the value of this node evaluated against the specified state
036         */
037        @Nullable
038        Object getValue(ExpressionState expressionState) throws EvaluationException;
039
040        /**
041         * Evaluate the expression node in the context of the supplied expression state
042         * and return the typed value.
043         * @param expressionState the current expression state (includes the context)
044         * @return the type value of this node evaluated against the specified state
045         */
046        TypedValue getTypedValue(ExpressionState expressionState) throws EvaluationException;
047
048        /**
049         * Determine if this expression node will support a setValue() call.
050         * @param expressionState the current expression state (includes the context)
051         * @return true if the expression node will allow setValue()
052         * @throws EvaluationException if something went wrong trying to determine
053         * if the node supports writing
054         */
055        boolean isWritable(ExpressionState expressionState) throws EvaluationException;
056
057        /**
058         * Evaluate the expression to a node and then set the new value on that node.
059         * For example, if the expression evaluates to a property reference, then the
060         * property will be set to the new value.
061         * @param expressionState the current expression state (includes the context)
062         * @param newValue the new value
063         * @throws EvaluationException if any problem occurs evaluating the expression or
064         * setting the new value
065         */
066        void setValue(ExpressionState expressionState, @Nullable Object newValue) throws EvaluationException;
067
068        /**
069         * Return the string form the this AST node.
070         * @return the string form
071         */
072        String toStringAST();
073
074        /**
075         * Return the number of children under this node.
076         * @return the child count
077         */
078        int getChildCount();
079
080        /**
081         * Helper method that returns a SpelNode rather than an Antlr Tree node.
082         * @return the child node cast to a SpelNode
083         */
084        SpelNode getChild(int index);
085
086        /**
087         * Determine the class of the object passed in, unless it is already a class object.
088         * @param obj the object that the caller wants the class of
089         * @return the class of the object if it is not already a class object,
090         * or {@code null} if the object is {@code null}
091         */
092        @Nullable
093        Class<?> getObjectClass(@Nullable Object obj);
094
095        /**
096         * Return the start position of this AST node in the expression string.
097         * @return the start position
098         */
099        int getStartPosition();
100
101        /**
102         * Return the end position of this AST node in the expression string.
103         * @return the end position
104         */
105        int getEndPosition();
106
107}