KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > ast > StringLiteralExpr


1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the compiler and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  *
22  * Contributor(s):
23  */

24
25 package org.aspectj.compiler.base.ast;
26
27 import org.aspectj.compiler.base.JavaCompiler;
28 import org.aspectj.compiler.base.TypeManager;
29
30 import org.aspectj.compiler.base.bcg.FieldBuilder;
31 import org.aspectj.compiler.base.bcg.CodeBuilder;
32 import org.aspectj.compiler.base.bcg.Label;
33
34
35 /**
36   * @grammar StringLiteral
37   * @property String stringValue
38   */

39 public class StringLiteralExpr extends LiteralExpr {
40
41     /** Stolen from CharacterLiteral inner class in {@link
42         org.aspectj.compiler.base.parser.JavaTokenizer}.
43     */

44     private static void quoteCharacter(char ch, StringBuffer JavaDoc buf) {
45         //if (ch >= 32 && ch <= 127) {
46
// buf.append(ch); return;
47
//}
48

49         switch (ch) {
50         case '\b': buf.append("\\b"); return;
51         case '\t': buf.append("\\t"); return;
52         case '\n': buf.append("\\n"); return;
53         case '\f': buf.append("\\f"); return;
54         case '\r': buf.append("\\r"); return;
55         case '\"': buf.append("\\\""); return;
56         case '\'': buf.append("\\\'"); return;
57         case '\\': buf.append("\\\\"); return;
58         }
59
60         buf.append(ch);
61
62         //buf.append("\\u");
63
//String hex = Integer.toHexString(ch);
64
//for(int i=0; i<(4-hex.length()); i++) buf.append('0');
65
//buf.append(hex);
66
}
67
68     private static String JavaDoc quoteString(String JavaDoc value) {
69         StringBuffer JavaDoc buf = new StringBuffer JavaDoc("\"");
70         for(int i=0; i<value.length(); i++) {
71             quoteCharacter(value.charAt(i), buf);
72         }
73         buf.append("\"");
74         return buf.toString();
75     }
76
77     public StringLiteralExpr(SourceLocation source, String JavaDoc _stringValue) {
78         this(source, source.getCompiler().getTypeManager().getStringType(),
79              quoteString(_stringValue),
80              _stringValue);
81     }
82
83     // ------------------------------
84
// bcg
85

86     protected void cgValue(CodeBuilder cb) {
87         cb.emitStringConstant(stringValue);
88     }
89     public void addConstant(FieldBuilder fb) {
90         fb.setConstantValue(stringValue);
91     }
92
93     //BEGIN: Generated from @child and @property
94
protected String JavaDoc stringValue;
95     public String JavaDoc getStringValue() { return stringValue; }
96     public void setStringValue(String JavaDoc _stringValue) { stringValue = _stringValue; }
97
98     public StringLiteralExpr(SourceLocation location, Type _type, String JavaDoc _value, String JavaDoc _stringValue) {
99         super(location, _type, _value);
100         setStringValue(_stringValue);
101     }
102     protected StringLiteralExpr(SourceLocation source) {
103         super(source);
104     }
105
106     public ASTObject copyWalk(CopyWalker walker) {
107         StringLiteralExpr ret = new StringLiteralExpr(getSourceLocation());
108         ret.preCopy(walker, this);
109         ret.type = type;
110         ret.value = value;
111         ret.stringValue = stringValue;
112         return ret;
113     }
114
115
116     public String JavaDoc getDefaultDisplayName() {
117         return "StringLiteralExpr(type: "+type+", "+"value: "+value+", "+"stringValue: "+stringValue+")";
118     }
119
120     //END: Generated from @child and @property
121
}
122
Popular Tags