1 /******************************************************************************* 2 * Copyright (c) 2000, 2004 IBM Corporation and others. 3 * All rights reserved. This program and the accompanying materials 4 * are made available under the terms of the Common Public License v1.0 5 * which accompanies this distribution, and is available at 6 * http://www.eclipse.org/legal/cpl-v10.html 7 * 8 * Contributors: 9 * IBM Corporation - initial API and implementation 10 *******************************************************************************/ 11 package org.eclipse.jdt.internal.compiler.ast; 12 13 import org.eclipse.jdt.internal.compiler.ASTVisitor; 14 import org.eclipse.jdt.internal.compiler.codegen.CodeStream; 15 import org.eclipse.jdt.internal.compiler.impl.Constant; 16 import org.eclipse.jdt.internal.compiler.lookup.BlockScope; 17 import org.eclipse.jdt.internal.compiler.lookup.TypeBinding; 18 19 public class StringLiteral extends Literal { 20 21 char[] source; 22 StringLiteral(char[] token, int s, int e)23 public StringLiteral(char[] token, int s, int e) { 24 25 this(s,e); 26 source = token; 27 } 28 StringLiteral(int s, int e)29 public StringLiteral(int s, int e) { 30 31 super(s,e); 32 } 33 computeConstant()34 public void computeConstant() { 35 36 constant = Constant.fromValue(String.valueOf(source)); 37 } 38 extendWith(CharLiteral lit)39 public ExtendedStringLiteral extendWith(CharLiteral lit){ 40 41 //add the lit source to mine, just as if it was mine 42 return new ExtendedStringLiteral(this,lit); 43 } 44 extendWith(StringLiteral lit)45 public ExtendedStringLiteral extendWith(StringLiteral lit){ 46 47 //add the lit source to mine, just as if it was mine 48 return new ExtendedStringLiteral(this,lit); 49 } 50 51 /** 52 * Add the lit source to mine, just as if it was mine 53 */ extendsWith(StringLiteral lit)54 public StringLiteralConcatenation extendsWith(StringLiteral lit) { 55 return new StringLiteralConcatenation(this, lit); 56 } 57 /** 58 * Code generation for string literal 59 */ generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired)60 public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired) { 61 62 int pc = codeStream.position; 63 if (valueRequired) 64 codeStream.ldc(constant.stringValue()); 65 codeStream.recordPositionsFrom(pc, this.sourceStart); 66 } 67 literalType(BlockScope scope)68 public TypeBinding literalType(BlockScope scope) { 69 70 return scope.getJavaLangString(); 71 } 72 printExpression(int indent, StringBuffer output)73 public StringBuffer printExpression(int indent, StringBuffer output) { 74 75 // handle some special char..... 76 output.append('\"'); 77 for (int i = 0; i < source.length; i++) { 78 switch (source[i]) { 79 case '\b' : 80 output.append("\\b"); //$NON-NLS-1$ 81 break; 82 case '\t' : 83 output.append("\\t"); //$NON-NLS-1$ 84 break; 85 case '\n' : 86 output.append("\\n"); //$NON-NLS-1$ 87 break; 88 case '\f' : 89 output.append("\\f"); //$NON-NLS-1$ 90 break; 91 case '\r' : 92 output.append("\\r"); //$NON-NLS-1$ 93 break; 94 case '\"' : 95 output.append("\\\""); //$NON-NLS-1$ 96 break; 97 case '\'' : 98 output.append("\\'"); //$NON-NLS-1$ 99 break; 100 case '\\' : //take care not to display the escape as a potential real char 101 output.append("\\\\"); //$NON-NLS-1$ 102 break; 103 default : 104 output.append(source[i]); 105 } 106 } 107 output.append('\"'); 108 return output; 109 } 110 source()111 public char[] source() { 112 113 return source; 114 } 115 traverse(ASTVisitor visitor, BlockScope scope)116 public void traverse(ASTVisitor visitor, BlockScope scope) { 117 visitor.visit(this, scope); 118 visitor.endVisit(this, scope); 119 } 120 } 121