KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > jjs > impl > SourceGenerationVisitor


1 /*
2  * Copyright 2007 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.dev.jjs.impl;
17
18 import com.google.gwt.dev.jjs.ast.CanBeFinal;
19 import com.google.gwt.dev.jjs.ast.Context;
20 import com.google.gwt.dev.jjs.ast.JClassType;
21 import com.google.gwt.dev.jjs.ast.JField;
22 import com.google.gwt.dev.jjs.ast.JInterfaceType;
23 import com.google.gwt.dev.jjs.ast.JMethod;
24 import com.google.gwt.dev.jjs.ast.JNullType;
25 import com.google.gwt.dev.jjs.ast.JProgram;
26 import com.google.gwt.dev.jjs.ast.JReferenceType;
27 import com.google.gwt.dev.jjs.ast.JType;
28 import com.google.gwt.dev.jjs.ast.js.JsniMethod;
29 import com.google.gwt.dev.util.TextOutput;
30
31 /**
32  * Generates Java source from our AST. ToStringGenerationVisitor is for
33  * relatively short toString() results, for easy viewing in a debugger. This
34  * subclass delves into the bodies of classes, interfaces, and methods to
35  * produce the whole source tree.
36  *
37  * The goal is not to generate the input source tree. Rather, the goal is to
38  * produce a set of classes that can be pasted into an enclosing class and
39  * compiled with a standard Java compiler. In practice, there are cases that
40  * require hand-editting to actually get a full compilation, due to Java's
41  * built-in reliance on particular built-in types.
42  *
43  * Known to be broken: Our generated String, Class, and Throwable are not
44  * compatable with the real ones, which breaks string literals, class literals,
45  * try/catch/throw, and overrides of Object methods.
46  */

47 public class SourceGenerationVisitor extends ToStringGenerationVisitor {
48
49   public SourceGenerationVisitor(TextOutput textOutput) {
50     super(textOutput);
51   }
52
53   // @Override
54
public boolean visit(JClassType x, Context ctx) {
55     // All classes are deemed "static" so the monolithic compile results can be
56
// copy/pasted into a single enclosing class.
57
print(CHARS_STATIC);
58
59     super.visit(x, ctx);
60
61     openBlock();
62
63     for (int i = 0; i < x.fields.size(); ++i) {
64       JField it = (JField) x.fields.get(i);
65       accept(it);
66       newline();
67       newline();
68     }
69     for (int i = 0; i < x.methods.size(); ++i) {
70       JMethod it = (JMethod) x.methods.get(i);
71       if (!isEmptyInitializer(it)) {
72         accept(it);
73         newline();
74         newline();
75       }
76     }
77
78     closeBlock();
79     return false;
80   }
81
82   public boolean visit(JField x, Context ctx) {
83     super.visit(x, ctx);
84
85     if (x.constInitializer != null) {
86       print(" = ");
87       accept(x.constInitializer);
88     }
89     semi();
90     return false;
91   }
92
93   // @Override
94
public boolean visit(JInterfaceType x, Context ctx) {
95     super.visit(x, ctx);
96
97     openBlock();
98
99     for (int i = 0; i < x.fields.size(); ++i) {
100       JField field = (JField) x.fields.get(i);
101       accept(field);
102       newline();
103       newline();
104     }
105     for (int i = 0; i < x.methods.size(); ++i) {
106       JMethod method = (JMethod) x.methods.get(i);
107       accept(method);
108       newline();
109       newline();
110     }
111
112     closeBlock();
113     return false;
114   }
115
116   // @Override
117
public boolean visit(JMethod x, Context ctx) {
118     // special: transcribe clinit and init as if they were initializer blocks
119
if (isInitializer(x)) {
120       if (x.isStatic()) {
121         print(CHARS_STATIC);
122       }
123       accept(x.body);
124     } else {
125       super.visit(x, ctx);
126
127       if (x.isAbstract()) {
128         semi();
129       } else {
130         space();
131         accept(x.body);
132       }
133     }
134
135     return false;
136   }
137
138   // @Override
139
public boolean visit(JProgram x, Context ctx) {
140     for (int i = 0; i < x.entryMethods.size(); ++i) {
141       JMethod method = (JMethod) x.entryMethods.get(i);
142       accept(method);
143       newline();
144       newline();
145     }
146     for (int i = 0; i < x.getDeclaredTypes().size(); ++i) {
147       JReferenceType type = (JReferenceType) x.getDeclaredTypes().get(i);
148       accept(type);
149       newline();
150       newline();
151     }
152     return false;
153   }
154
155   // @Override
156
public boolean visit(JsniMethod x, Context ctx) {
157     super.visit(x, ctx);
158     space();
159     print(CHARS_SLASHSTAR);
160     String JavaDoc jsniCode = x.getFunc().getBody().toString();
161     String JavaDoc[] splits = jsniCode.split("\r|\n");
162     for (int i = 0, c = splits.length; i < c; ++i) {
163       if (i > 0) {
164         newline();
165       }
166       print(splits[i]);
167     }
168     print(CHARS_STARSLASH);
169     semi();
170
171     return false;
172   }
173
174   // @Override
175
protected void printMemberFinalFlag(CanBeFinal x) {
176     // suppress final flags
177
}
178
179   // @Override
180
protected void printTypeName(JType type) {
181     if (type instanceof JNullType) {
182       print("Object");
183     } else {
184       super.printTypeName(type);
185     }
186   }
187
188   private boolean isEmptyInitializer(JMethod x) {
189     return isInitializer(x) && (x.body.statements.size() == 0);
190   }
191
192   private boolean isInitializer(JMethod x) {
193     return x.getName().equals("$clinit") || x.getName().equals("$init");
194   }
195
196 }
197
Popular Tags