KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > generator > JavaGenerator


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
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
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

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

512         if (e.getStatement() instanceof JCodeStatement)
513         {
514             JCodeStatement cs = (JCodeStatement)e.getStatement();
515             writeCodeStatement( jw, cs, false );
516             //jw.print( cs.getCode() );
517
}
518         else
519         {
520             writeStatement( jw, e.getStatement() );
521         }
522     }
523
524     protected void writeFinallyStatement( JavaWriter jw, JFinallyStatement fs )
525     {
526         if (fs.hasStatements())
527         {
528             jw.println( "finally" );
529             writeStatement( jw, fs.getStatement() );
530         }
531     }
532
533     protected void writeForStatement( JavaWriter jw, JForStatement fs )
534     {
535         jw.newln();
536         jw.print( "for (" );
537         writeStatement( jw, fs.getInitStatement() );
538         jw.print( ";" );
539         writeExpression( jw, fs.getLoopExpression() );
540         writeStatement( jw, fs.getIterStatement() );
541         jw.println( ")" );
542         writeBlockStatement( jw, fs );
543         //writeBlockStatement( jw, fs.getStatement() );
544
}
545
546     protected void writeIfStatement( JavaWriter jw, JIfStatement is )
547     {
548         jw.newln();
549         jw.print( "if (" );
550         writeExpression( jw, is.getExpression() );
551         jw.println( ")" );
552         writeBlockStatement( jw, is );
553     }
554
555     protected void writeSwitchStatement( JavaWriter jw, JSwitchStatement ss )
556     {
557         jw.newln();
558         jw.print( "switch (" );
559         writeExpression( jw, ss.getExpression() );
560         jw.println( ")" );
561         jw.begin();
562         writeStatements( jw, ss.getCases() );
563         jw.end();
564     }
565
566     protected void writeTryCatchFinallyStatement( JavaWriter jw, JTryCatchFinallyStatement tcfs )
567     {
568         writeStatement( jw, tcfs.getTryStatement() );
569         writeStatements( jw, tcfs.getCatches() );
570         writeStatement( jw, tcfs.getFinallyStatement() );
571     }
572
573     protected void writeTryStatement( JavaWriter jw, JTryStatement ts )
574     {
575         jw.println( "" );
576         jw.println( "try" );
577         writeBlockStatement( jw, ts );
578         //writeStatement( jw, ts.getStatement() );
579
}
580
581 }
582
Popular Tags