KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > generator > JavaGenerator


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.generator;
19
20 import java.io.File JavaDoc;
21 import java.lang.reflect.Modifier JavaDoc;
22 import java.util.Vector JavaDoc;
23
24 public class JavaGenerator implements Generator {
25     private GenOptions genOptions;
26
27     public JavaGenerator(GenOptions genOptions) {
28         this.genOptions = genOptions;
29     }
30
31     public GenOptions getGenOptions() {
32         return genOptions;
33     }
34
35     public void setGenOptions(GenOptions genOptions) {
36         this.genOptions = genOptions;
37     }
38
39     public void generate(JEntity e) {
40         // Meaningless
41
}
42
43     public void generate(JPackage p)
44             throws GenException {
45         if (p == null) {
46             return;
47         }
48
49         Vector JavaDoc v = p.getClasses();
50
51         if (v != null && v.size() > 0) {
52             int i;
53             for (i = 0; i < v.size(); i++) {
54                 generate((JClass) v.elementAt(i));
55             }
56         }
57     }
58
59     public void generate(JClass c)
60             throws GenException {
61         if (c == null) {
62             return;
63         }
64
65         String JavaDoc className = c.getName();
66         String JavaDoc pkgName = c.getPackage().getName();
67
68         pkgName = pkgName.replace('.', File.separatorChar);
69
70         String JavaDoc fullName = pkgName + "/" + className;
71
72         JavaWriter jw = new JavaWriter(genOptions, fullName, ".java");
73
74         jw.openFile();
75         writeClass(jw, c);
76         jw.closeFile();
77     }
78
79     protected void writeClass(JavaWriter jw, JClass c) {
80         writeClassPackage(jw, c);
81         writeClassImports(jw, c);
82
83         writeClassClassDefn(jw, c);
84         jw.begin();
85         writeClassFields(jw, c);
86         writeClassConstructors(jw, c);
87         writeClassMethods(jw, c);
88         jw.end();
89     }
90
91     protected void writeClassPackage(JavaWriter jw, JClass c) {
92         if (c.getPackage().getName().length() > 0) {
93             jw.newln();
94             jw.println("package " + c.getPackage().getName() + ";");
95         }
96     }
97
98     protected void writeClassImports(JavaWriter jw, JClass c) {
99         Vector JavaDoc v = c.getImports();
100         if (v != null && v.size() > 0) {
101             int i;
102
103             jw.newln();
104
105             for (i = 0; i < v.size(); i++) {
106                 jw.println("import " + v.elementAt(i) + ";");
107             }
108         }
109     }
110
111     protected void writeClassClassDefn(JavaWriter jw, JClass c) {
112         jw.newln();
113
114         writeModifiers(jw, c.getModifiers());
115         jw.println("class " + c.getName());
116
117         if (c.getExtends() != null && c.getExtends().length() > 0) {
118             jw.indent();
119             jw.println("extends " + c.getBaseClassName());
120             jw.outdent();
121         }
122
123         Vector JavaDoc v = c.getImplements();
124         if (v != null && v.size() > 0) {
125             int i;
126
127             jw.indent();
128             jw.print("implements ");
129             jw.outdent();
130
131             for (i = 0; i < v.size(); i++) {
132                 jw.print("" + v.elementAt(i));
133
134                 if (i + 1 != v.size()) {
135                     jw.print(", ");
136                 }
137             }
138             jw.println("");
139         }
140     }
141
142     protected void writeClassFields(JavaWriter jw, JClass c) {
143         Vector JavaDoc v = c.getFields();
144         if (v != null && v.size() > 0) {
145             jw.comment("");
146             jw.comment("Fields");
147             jw.comment("");
148
149             int i;
150             JField f;
151             for (i = 0; i < v.size(); i++) {
152                 f = (JField) v.elementAt(i);
153                 writeClassField(jw, c, f);
154             }
155         }
156     }
157
158     protected void writeClassField(JavaWriter jw, JClass c, JField f) {
159         writeModifiers(jw, f.getModifiers());
160         jw.print(f.getTypeDecl() + " " + f.getName());
161
162         if (f.getInitExpression() != null) {
163             jw.print(" = ");
164             writeExpression(jw, f.getInitExpression());
165         }
166
167         jw.println(";");
168     }
169
170     protected void writeClassConstructors(JavaWriter jw, JClass c) {
171         Vector JavaDoc v = c.getConstructors();
172         if (v != null && v.size() > 0) {
173             int i;
174             JMethod m;
175
176             jw.newln();
177
178             jw.comment("");
179             jw.comment("Constructors");
180             jw.comment("");
181
182             for (i = 0; i < v.size(); i++) {
183                 m = (JMethod) v.elementAt(i);
184                 writeClassMethod(jw, c, m);
185             }
186         }
187     }
188
189     protected void writeClassMethods(JavaWriter jw, JClass c) {
190         Vector JavaDoc v = c.getMethods();
191         if (v != null && v.size() > 0) {
192             int i;
193             JMethod m;
194
195             jw.newln();
196
197             jw.comment("");
198             jw.comment("Methods");
199             jw.comment("");
200
201             for (i = 0; i < v.size(); i++) {
202                 jw.newln();
203                 m = (JMethod) v.elementAt(i);
204                 writeClassMethod(jw, c, m);
205             }
206         }
207     }
208
209     protected void writeClassMethod(JavaWriter jw, JClass c, JMethod m) {
210         writeModifiers(jw, m.getModifiers());
211
212         if (m instanceof JConstructor) {
213             jw.print(c.getName());
214         } else {
215             jw.print(m.getRT().getTypeDecl());
216             jw.print(" " + m.getName());
217         }
218         jw.print("(");
219
220         JParameter p[] = m.getParms();
221         if (p != null && p.length > 0) {
222             int i;
223             for (i = 0; i < p.length; i++) {
224                 jw.print(" " + p[i].getTypeDecl() + " " + p[i].getName());
225
226                 if (i + 1 != p.length) {
227                     jw.print(",");
228                 }
229             }
230         }
231
232         jw.print(" )");
233
234         //String s[] = m.getThrownType();
235
Class JavaDoc s[] = m.getThrown();
236         if (s != null && s.length > 0) {
237             int i;
238
239             jw.print(" throws ");
240
241             for (i = 0; i < s.length; i++) {
242                 jw.print(s[i].getName());
243
244                 if (i + 1 != s.length) {
245                     jw.print(", ");
246                 }
247             }
248         }
249         jw.println("");
250
251         jw.begin();
252         writeLocalVariables(jw, m.getLocalVariables());
253         writeStatements(jw, m.getStatements());
254
255         if (m.getBody() != null && m.getBody().length() > 0) {
256             jw.println(m.getBody());
257         }
258         jw.end();
259     }
260
261     protected void writeLocalVariables(JavaWriter jw, Vector JavaDoc lv) {
262         if (lv != null && lv.size() > 0) {
263             int i;
264             for (i = 0; i < lv.size(); i++) {
265                 writeLocalVariable(jw, (JLocalVariable) lv.elementAt(i));
266             }
267         }
268     }
269
270     protected void writeLocalVariable(JavaWriter jw, JLocalVariable lv) {
271         jw.print(lv.getTypeDecl() + " " + lv.getName());
272
273         if (lv.getInitExpression() != null) {
274             jw.print(" = ");
275             writeExpression(jw, lv.getInitExpression());
276         }
277
278         jw.println(";");
279     }
280
281     protected void writeStatements(JavaWriter jw, Vector JavaDoc sv) {
282         if (sv != null && sv.size() > 0) {
283             int i;
284             for (i = 0; i < sv.size(); i++) {
285                 writeStatement(jw, (JStatement) sv.elementAt(i));
286             }
287         }
288     }
289
290     protected void writeModifiers(JavaWriter jw, int m) {
291         String JavaDoc s = Modifier.toString(m);
292
293         if (s != null && s.length() > 0) {
294             jw.print(s + " ");
295         }
296     }
297
298     protected void writeStatement(JavaWriter jw, JStatement s) {
299         if (s instanceof JCaseStatement) {
300             writeCaseStatement(jw, (JCaseStatement) s);
301         } else if (s instanceof JCatchStatement) {
302             writeCatchStatement(jw, (JCatchStatement) s);
303         } else if (s instanceof JCodeStatement) {
304             writeCodeStatement(jw, (JCodeStatement) s, true);
305         } else if (s instanceof JDeclareStatement) {
306             writeDeclareStatement(jw, (JDeclareStatement) s);
307         } else if (s instanceof JElseStatement) {
308             writeElseStatement(jw, (JElseStatement) s);
309         } else if (s instanceof JElseIfStatement) {
310             writeElseIfStatement(jw, (JElseIfStatement) s);
311         } else if (s instanceof JIfElseIfElseStatement) {
312             writeIfElseIfElseStatement(jw, (JIfElseIfElseStatement) s);
313         } else if (s instanceof JFinallyStatement) {
314             writeFinallyStatement(jw, (JFinallyStatement) s);
315         } else if (s instanceof JForStatement) {
316             writeForStatement(jw, (JForStatement) s);
317         } else if (s instanceof JIfStatement) {
318             writeIfStatement(jw, (JIfStatement) s);
319         } else if (s instanceof JTryCatchFinallyStatement) {
320             writeTryCatchFinallyStatement(jw, (JTryCatchFinallyStatement) s);
321         } else if (s instanceof JSwitchStatement) {
322             writeSwitchStatement(jw, (JSwitchStatement) s);
323         } else if (s instanceof JTryStatement) {
324             writeTryStatement(jw, (JTryStatement) s);
325         } else if (s instanceof JBlockStatement) {
326             // BlockStatemnet should be last since there are other subclasses of it.
327
writeBlockStatement(jw, (JBlockStatement) s);
328         } else {
329             jw.comment("");
330             jw.comment("Error: Unknown statement: " + s);
331             jw.comment("");
332         }
333     }
334
335     protected void writeBlockStatement(JavaWriter jw, JBlockStatement bs) {
336         jw.begin();
337         writeLocalVariables(jw, bs.getLocalVariables());
338         writeStatements(jw, bs.getStatements());
339         jw.end();
340     }
341
342     protected void writeCaseStatement(JavaWriter jw, JCaseStatement cs) {
343         jw.print("case ");
344         writeExpression(jw, cs.getExpression());
345         jw.println(":");
346         writeStatement(jw, cs.getStatement());
347         jw.println("break;");
348     }
349
350     protected void writeCatchStatement(JavaWriter jw, JCatchStatement cs) {
351         jw.println("catch( " + cs.getVariable().getTypeDecl() + " " + cs.getVariable().getName() + " )");
352         writeBlockStatement(jw, cs);
353         //writeStatement( jw, cs.getStatement() );
354
}
355
356     protected void writeCodeStatement(JavaWriter jw, JCodeStatement cs, boolean newLine) {
357         jw.print(cs.getCode());
358
359         if (newLine) {
360             jw.newln();
361         }
362
363         //jw.print( cs.getCode() );
364
//jw.println( ";" );
365
}
366
367     protected void writeDeclareStatement(JavaWriter jw, JDeclareStatement ds) {
368         JVariable v = ds.getVariable();
369         jw.print(v.getTypeDecl() + " " + v.getName());
370
371         JExpression e = ds.getInitExpression();
372         if (e != null) {
373             jw.print(" = ");
374             writeExpression(jw, e);
375         }
376
377         jw.println(";");
378     }
379
380     protected void writeElseStatement(JavaWriter jw, JElseStatement es) {
381         if (es.hasStatements()) {
382             jw.println("else");
383             writeBlockStatement(jw, es);
384         }
385     }
386
387     protected void writeElseIfStatement(JavaWriter jw, JElseIfStatement eis) {
388         if (eis.hasStatements()) {
389             jw.print("else ");
390             writeIfStatement(jw, eis);
391         }
392     }
393
394     protected void writeIfElseIfElseStatement(JavaWriter jw, JIfElseIfElseStatement ies) {
395         writeIfStatement(jw, ies.getIfStatement());
396     }
397
398     protected void writeExpression(JavaWriter jw, JExpression e) {
399         // TODO: not sure how I am going to do this but...
400

401         if (e.getStatement() instanceof JCodeStatement) {
402             JCodeStatement cs = (JCodeStatement) e.getStatement();
403             writeCodeStatement(jw, cs, false);
404             //jw.print( cs.getCode() );
405
} else {
406             writeStatement(jw, e.getStatement());
407         }
408     }
409
410     protected void writeFinallyStatement(JavaWriter jw, JFinallyStatement fs) {
411         if (fs.hasStatements()) {
412             jw.println("finally");
413             writeStatement(jw, fs.getStatement());
414         }
415     }
416
417     protected void writeForStatement(JavaWriter jw, JForStatement fs) {
418         jw.newln();
419         jw.print("for (");
420         writeStatement(jw, fs.getInitStatement());
421         jw.print(";");
422         writeExpression(jw, fs.getLoopExpression());
423         writeStatement(jw, fs.getIterStatement());
424         jw.println(")");
425         writeBlockStatement(jw, fs);
426         //writeBlockStatement( jw, fs.getStatement() );
427
}
428
429     protected void writeIfStatement(JavaWriter jw, JIfStatement is) {
430         jw.newln();
431         jw.print("if (");
432         writeExpression(jw, is.getExpression());
433         jw.println(")");
434         writeBlockStatement(jw, is);
435     }
436
437     protected void writeSwitchStatement(JavaWriter jw, JSwitchStatement ss) {
438         jw.newln();
439         jw.print("switch (");
440         writeExpression(jw, ss.getExpression());
441         jw.println(")");
442         jw.begin();
443         writeStatements(jw, ss.getCases());
444         jw.end();
445     }
446
447     protected void writeTryCatchFinallyStatement(JavaWriter jw, JTryCatchFinallyStatement tcfs) {
448         writeStatement(jw, tcfs.getTryStatement());
449         writeStatements(jw, tcfs.getCatches());
450         writeStatement(jw, tcfs.getFinallyStatement());
451     }
452
453     protected void writeTryStatement(JavaWriter jw, JTryStatement ts) {
454         jw.println("");
455         jw.println("try");
456         writeBlockStatement(jw, ts);
457         //writeStatement( jw, ts.getStatement() );
458
}
459
460 }
461
Popular Tags