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