View Javadoc
1   /*
2    * Copyright 2002-2014 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.springframework.expression.spel.ast;
18  
19  import org.springframework.asm.MethodVisitor;
20  import org.springframework.expression.TypedValue;
21  import org.springframework.expression.spel.CodeFlow;
22  
23  /**
24   * Expression language AST node that represents a string literal.
25   *
26   * @author Andy Clement
27   * @author Juergen Hoeller
28   * @since 3.0
29   */
30  public class StringLiteral extends Literal {
31  
32  	private final TypedValue value;
33  
34  
35  	public StringLiteral(String payload, int pos, String value) {
36  		super(payload,pos);
37  		value = value.substring(1, value.length() - 1);
38  		this.value = new TypedValue(value.replaceAll("''", "'").replaceAll("\"\"", "\""));
39  		this.exitTypeDescriptor = "Ljava/lang/String";
40  	}
41  
42  
43  	@Override
44  	public TypedValue getLiteralValue() {
45  		return this.value;
46  	}
47  
48  	@Override
49  	public String toString() {
50  		return "'" + getLiteralValue().getValue() + "'";
51  	}
52  	
53  	@Override
54  	public boolean isCompilable() {
55  		return true;
56  	}
57  	
58  	@Override
59  	public void generateCode(MethodVisitor mv, CodeFlow cf) {
60  		mv.visitLdcInsn(this.value.getValue());
61  		cf.pushDescriptor(this.exitTypeDescriptor);
62  	}
63  
64  }