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.ast;
018
019import org.springframework.asm.MethodVisitor;
020import org.springframework.expression.TypedValue;
021import org.springframework.expression.spel.CodeFlow;
022
023/**
024 * Expression language AST node that represents a string literal.
025 *
026 * @author Andy Clement
027 * @author Juergen Hoeller
028 * @since 3.0
029 */
030public class StringLiteral extends Literal {
031
032        private final TypedValue value;
033
034
035        public StringLiteral(String payload, int pos, String value) {
036                super(payload, pos);
037                value = value.substring(1, value.length() - 1);
038                this.value = new TypedValue(value.replaceAll("''", "'").replaceAll("\"\"", "\""));
039                this.exitTypeDescriptor = "Ljava/lang/String";
040        }
041
042
043        @Override
044        public TypedValue getLiteralValue() {
045                return this.value;
046        }
047
048        @Override
049        public String toString() {
050                return "'" + getLiteralValue().getValue() + "'";
051        }
052        
053        @Override
054        public boolean isCompilable() {
055                return true;
056        }
057        
058        @Override
059        public void generateCode(MethodVisitor mv, CodeFlow cf) {
060                mv.visitLdcInsn(this.value.getValue());
061                cf.pushDescriptor(this.exitTypeDescriptor);
062        }
063
064}