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.common;
018
019import org.springframework.core.convert.TypeDescriptor;
020import org.springframework.expression.EvaluationContext;
021import org.springframework.expression.EvaluationException;
022import org.springframework.expression.Expression;
023import org.springframework.expression.TypedValue;
024import org.springframework.lang.Nullable;
025
026/**
027 * A very simple hardcoded implementation of the Expression interface that represents a
028 * string literal. It is used with CompositeStringExpression when representing a template
029 * expression which is made up of pieces - some being real expressions to be handled by
030 * an EL implementation like SpEL, and some being just textual elements.
031 *
032 * @author Andy Clement
033 * @author Juergen Hoeller
034 * @since 3.0
035 */
036public class LiteralExpression implements Expression {
037
038        /** Fixed literal value of this expression. */
039        private final String literalValue;
040
041
042        public LiteralExpression(String literalValue) {
043                this.literalValue = literalValue;
044        }
045
046
047        @Override
048        public final String getExpressionString() {
049                return this.literalValue;
050        }
051
052        @Override
053        public Class<?> getValueType(EvaluationContext context) {
054                return String.class;
055        }
056
057        @Override
058        public String getValue() {
059                return this.literalValue;
060        }
061
062        @Override
063        @Nullable
064        public <T> T getValue(@Nullable Class<T> expectedResultType) throws EvaluationException {
065                Object value = getValue();
066                return ExpressionUtils.convertTypedValue(null, new TypedValue(value), expectedResultType);
067        }
068
069        @Override
070        public String getValue(@Nullable Object rootObject) {
071                return this.literalValue;
072        }
073
074        @Override
075        @Nullable
076        public <T> T getValue(@Nullable Object rootObject, @Nullable Class<T> desiredResultType) throws EvaluationException {
077                Object value = getValue(rootObject);
078                return ExpressionUtils.convertTypedValue(null, new TypedValue(value), desiredResultType);
079        }
080
081        @Override
082        public String getValue(EvaluationContext context) {
083                return this.literalValue;
084        }
085
086        @Override
087        @Nullable
088        public <T> T getValue(EvaluationContext context, @Nullable Class<T> expectedResultType)
089                        throws EvaluationException {
090
091                Object value = getValue(context);
092                return ExpressionUtils.convertTypedValue(context, new TypedValue(value), expectedResultType);
093        }
094
095        @Override
096        public String getValue(EvaluationContext context, @Nullable Object rootObject) throws EvaluationException {
097                return this.literalValue;
098        }
099
100        @Override
101        @Nullable
102        public <T> T getValue(EvaluationContext context, @Nullable Object rootObject, @Nullable Class<T> desiredResultType)
103                        throws EvaluationException {
104
105                Object value = getValue(context, rootObject);
106                return ExpressionUtils.convertTypedValue(context, new TypedValue(value), desiredResultType);
107        }
108
109        @Override
110        public Class<?> getValueType() {
111                return String.class;
112        }
113
114        @Override
115        public Class<?> getValueType(@Nullable Object rootObject) throws EvaluationException {
116                return String.class;
117        }
118
119        @Override
120        public Class<?> getValueType(EvaluationContext context, @Nullable Object rootObject) throws EvaluationException {
121                return String.class;
122        }
123
124        @Override
125        public TypeDescriptor getValueTypeDescriptor() {
126                return TypeDescriptor.valueOf(String.class);
127        }
128
129        @Override
130        public TypeDescriptor getValueTypeDescriptor(@Nullable Object rootObject) throws EvaluationException {
131                return TypeDescriptor.valueOf(String.class);
132        }
133
134        @Override
135        public TypeDescriptor getValueTypeDescriptor(EvaluationContext context) {
136                return TypeDescriptor.valueOf(String.class);
137        }
138
139        @Override
140        public TypeDescriptor getValueTypeDescriptor(EvaluationContext context, @Nullable Object rootObject) throws EvaluationException {
141                return TypeDescriptor.valueOf(String.class);
142        }
143
144        @Override
145        public boolean isWritable(@Nullable Object rootObject) throws EvaluationException {
146                return false;
147        }
148
149        @Override
150        public boolean isWritable(EvaluationContext context) {
151                return false;
152        }
153
154        @Override
155        public boolean isWritable(EvaluationContext context, @Nullable Object rootObject) throws EvaluationException {
156                return false;
157        }
158
159        @Override
160        public void setValue(@Nullable Object rootObject, @Nullable Object value) throws EvaluationException {
161                throw new EvaluationException(this.literalValue, "Cannot call setValue() on a LiteralExpression");
162        }
163
164        @Override
165        public void setValue(EvaluationContext context, @Nullable Object value) throws EvaluationException {
166                throw new EvaluationException(this.literalValue, "Cannot call setValue() on a LiteralExpression");
167        }
168
169        @Override
170        public void setValue(EvaluationContext context, @Nullable Object rootObject, @Nullable Object value) throws EvaluationException {
171                throw new EvaluationException(this.literalValue, "Cannot call setValue() on a LiteralExpression");
172        }
173
174}