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