001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.codemodel.internal;
027:
028: /**
029: * Factory methods that generate various {@link JExpression}s.
030: */
031: public abstract class JExpr {
032:
033: /**
034: * This class is not instanciable.
035: */
036: private JExpr() {
037: }
038:
039: public static JExpression assign(JAssignmentTarget lhs,
040: JExpression rhs) {
041: return new JAssignment(lhs, rhs);
042: }
043:
044: public static JExpression assignPlus(JAssignmentTarget lhs,
045: JExpression rhs) {
046: return new JAssignment(lhs, rhs, "+");
047: }
048:
049: public static JInvocation _new(JClass c) {
050: return new JInvocation(c);
051: }
052:
053: public static JInvocation _new(JType t) {
054: return new JInvocation(t);
055: }
056:
057: public static JInvocation invoke(String method) {
058: return new JInvocation((JExpression) null, method);
059: }
060:
061: public static JInvocation invoke(JMethod method) {
062: return new JInvocation((JExpression) null, method.name());
063: }
064:
065: public static JInvocation invoke(JExpression lhs, JMethod method) {
066: return new JInvocation(lhs, method.name());
067: }
068:
069: public static JInvocation invoke(JExpression lhs, String method) {
070: return new JInvocation(lhs, method);
071: }
072:
073: public static JFieldRef ref(String field) {
074: return new JFieldRef((JExpression) null, field);
075: }
076:
077: public static JFieldRef ref(JExpression lhs, JVar field) {
078: return new JFieldRef(lhs, field);
079: }
080:
081: public static JFieldRef ref(JExpression lhs, String field) {
082: return new JFieldRef(lhs, field);
083: }
084:
085: public static JFieldRef refthis (String field) {
086: return new JFieldRef(null, field, true);
087: }
088:
089: public static JExpression dotclass(final JClass cl) {
090: return new JExpressionImpl() {
091: public void generate(JFormatter f) {
092: JClass c;
093: if (cl instanceof JNarrowedClass)
094: c = ((JNarrowedClass) cl).basis;
095: else
096: c = cl;
097: f.g(c).p(".class");
098: }
099: };
100: }
101:
102: public static JArrayCompRef component(JExpression lhs,
103: JExpression index) {
104: return new JArrayCompRef(lhs, index);
105: }
106:
107: public static JCast cast(JType type, JExpression expr) {
108: return new JCast(type, expr);
109: }
110:
111: public static JArray newArray(JType type) {
112: return newArray(type, null);
113: }
114:
115: /**
116: * Generates {@code new T[size]}.
117: *
118: * @param type
119: * The type of the array component. 'T' or {@code new T[size]}.
120: */
121: public static JArray newArray(JType type, JExpression size) {
122: // you cannot create an array whose component type is a generic
123: return new JArray(type.erasure(), size);
124: }
125:
126: /**
127: * Generates {@code new T[size]}.
128: *
129: * @param type
130: * The type of the array component. 'T' or {@code new T[size]}.
131: */
132: public static JArray newArray(JType type, int size) {
133: return newArray(type, lit(size));
134: }
135:
136: private static final JExpression __this = new JAtom("this");
137:
138: /**
139: * Returns a reference to "this", an implicit reference
140: * to the current object.
141: */
142: public static JExpression _this () {
143: return __this ;
144: }
145:
146: private static final JExpression __super = new JAtom("super");
147:
148: /**
149: * Returns a reference to "super", an implicit reference
150: * to the super class.
151: */
152: public static JExpression _super () {
153: return __super ;
154: }
155:
156: /* -- Literals -- */
157:
158: private static final JExpression __null = new JAtom("null");
159:
160: public static JExpression _null() {
161: return __null;
162: }
163:
164: /**
165: * Boolean constant that represents <code>true</code>
166: */
167: public static final JExpression TRUE = new JAtom("true");
168:
169: /**
170: * Boolean constant that represents <code>false</code>
171: */
172: public static final JExpression FALSE = new JAtom("false");
173:
174: public static JExpression lit(boolean b) {
175: return b ? TRUE : FALSE;
176: }
177:
178: public static JExpression lit(int n) {
179: return new JAtom(Integer.toString(n));
180: }
181:
182: public static JExpression lit(long n) {
183: return new JAtom(Long.toString(n) + "L");
184: }
185:
186: public static JExpression lit(float f) {
187: return new JAtom(Float.toString(f) + "F");
188: }
189:
190: public static JExpression lit(double d) {
191: return new JAtom(Double.toString(d) + "D");
192: }
193:
194: static final String charEscape = "\b\t\n\f\r\"\'\\";
195: static final String charMacro = "btnfr\"'\\";
196:
197: /**
198: * Escapes the given string, then surrounds it by the specified
199: * quotation mark.
200: */
201: public static String quotify(char quote, String s) {
202: int n = s.length();
203: StringBuilder sb = new StringBuilder(n + 2);
204: sb.append(quote);
205: for (int i = 0; i < n; i++) {
206: char c = s.charAt(i);
207: int j = charEscape.indexOf(c);
208: if (j >= 0) {
209: sb.append('\\');
210: sb.append(charMacro.charAt(j));
211: } else {
212: // technically Unicode escape shouldn't be done here,
213: // for it's a lexical level handling.
214: //
215: // However, various tools are so broken around this area,
216: // so just to be on the safe side, it's better to do
217: // the escaping here (regardless of the actual file encoding)
218: //
219: // see bug
220: if (c < 0x20 || 0x7E < c) {
221: // not printable. use Unicode escape
222: sb.append("\\u");
223: String hex = Integer
224: .toHexString(((int) c) & 0xFFFF);
225: for (int k = hex.length(); k < 4; k++)
226: sb.append('0');
227: sb.append(hex);
228: } else {
229: sb.append(c);
230: }
231: }
232: }
233: sb.append(quote);
234: return sb.toString();
235: }
236:
237: public static JExpression lit(char c) {
238: return new JAtom(quotify('\'', "" + c));
239: }
240:
241: public static JExpression lit(String s) {
242: return new JStringLiteral(s);
243: }
244:
245: /**
246: * Creates an expression directly from a source code fragment.
247: *
248: * <p>
249: * This method can be used as a short-cut to create a JExpression.
250: * For example, instead of <code>_a.gt(_b)</code>, you can write
251: * it as: <code>JExpr.direct("a>b")</code>.
252: *
253: * <p>
254: * Be warned that there is a danger in using this method,
255: * as it obfuscates the object model.
256: */
257: public static JExpression direct(final String source) {
258: return new JExpressionImpl() {
259: public void generate(JFormatter f) {
260: f.p('(').p(source).p(')');
261: }
262: };
263: }
264: }
|