KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infozone > tools > janalyzer > JavaCodeAnalyzer


1 // You can redistribute this software and/or modify it under the terms of
2
// the Infozone Software License version 2 published by the Infozone Group
3
// (http://www.infozone-group.org).
4
//
5
// Copyright (C) @year@ by The Infozone Group. All rights reserved.
6
//
7
// $Id: JavaCodeAnalyzer.java,v 1.1 2002/05/10 08:59:12 per_nyfelt Exp $
8

9 package org.infozone.tools.janalyzer;
10
11 import koala.dynamicjava.parser.wrapper.*;
12 import koala.dynamicjava.tree.*;
13 import java.util.List JavaDoc;
14 import java.util.ListIterator JavaDoc;
15 import java.io.File JavaDoc;
16 import java.io.FileReader JavaDoc;
17 import java.io.FileWriter JavaDoc;
18 import java.io.BufferedReader JavaDoc;
19 import java.io.BufferedWriter JavaDoc;
20
21
22 /**
23  * This class parses a existing JAVA class and format's the source code
24  * after the Code Conventions from Sun and therefore from the Apache Software Foundation.
25  * It uses the Syntax tree from the DynamicJava SourceCodeParser and transform each node of
26  * this tree back into lines of code.
27  * All output is handled by the JavaCodeOutput class. Also comments only handled by the Output
28  * class, because they don't exist in a syntax tree.
29  *
30  *
31  * JCC in the comment stands for
32  * <a HREF=http://xml.apache.org/source.html>Coding Conventions</a>.
33  *
34  * <pre>
35  *
36  * TODO:
37  * - forgotten expressions they not used in the prowler package
38  * - indentation in nested conditional operator statements
39  * - comments after the last expression in a method and the trailing } are deleted!!!
40  * - split level in binary expressions
41  * - label:
42  * - more documentation
43  * - quality checking such:
44  * - constants as ABC_DEF
45  * - JavaCodeMetricManager
46  * - metric'S
47  * - method counting and so on
48  *
49  * Problems:
50  * - trailling comments can't be inserted after the automatically wrapped lines, so
51  * they are inserted before the statements
52  * - At this moment only the trailling comment on the same line as the statement starts
53  * is checked
54  * </pre>
55  *
56  * @version $Revision: 1.1 $ $Date: 2002/05/10 08:59:12 $
57  * @author <a HREF="http://www.softwarebuero.de">SMB</a>
58  */

59 public final class JavaCodeAnalyzer extends java.lang.Object JavaDoc {
60     
61     //
62
// data
63
//
64

65     /** The Name of the file to prepare. */
66     // private String filename;
67
/** The string for concating several smaller output strings to one line. */
68     private String JavaDoc outLine = "";
69     /** The output class */
70     private JavaCodeOutput jco;
71     /** The expression helper class */
72     private ExpressionHelper eh;
73     /** The level to help correct parentheses to binary expressions */
74     private int currentBinaryExpressionLevel = 0;
75     
76     /** Source File is a interface Declaration, so methods doesnt have a body */
77     private boolean isInterface = false;
78     
79     
80     public JavaCodeAnalyzer( String JavaDoc filenameIn, String JavaDoc filenameOut, String JavaDoc lineLength ) {
81         // filename = filenameIn;
82

83         //
84
try {
85             // output class
86
// copy input file
87
File JavaDoc tmp = File.createTempFile( "JavaCodeAnalyzer", "tmp" );
88             BufferedReader JavaDoc br = new BufferedReader JavaDoc( new FileReader JavaDoc( filenameIn ) );
89             BufferedWriter JavaDoc out = new BufferedWriter JavaDoc( new FileWriter JavaDoc( tmp ) );
90             while (br.ready()) {
91                 out.write( br.read() );
92             }
93             br.close();
94             out.close();
95             
96             jco = new JavaCodeOutput( tmp, filenameOut, lineLength );
97             SourceCodeParser p = new JavaCCParserFactory().createParser( new FileReader JavaDoc( tmp ), null );
98             //filenameIn);
99
List JavaDoc statements = p.parseCompilationUnit();
100             ListIterator JavaDoc it = statements.listIterator();
101             eh = new ExpressionHelper( this, jco );
102             Node n;
103             printLog( "Parsed file " + filenameIn + "\n" );
104             while (it.hasNext()) {
105                 n = (Node)it.next();
106                 parseObject( n );
107             }
108             tmp.delete();
109         } catch (Exception JavaDoc e) {
110             System.err.println( getClass() + ": " + e );
111         }
112     }
113     
114     
115     /**
116      * This method parses each Node in the syntax tree.
117      * The String out is used for concatenation of the whole single strings.
118      *
119      * Errors:
120      * On FieldDeclaration: only one declaration per line is in the tree.
121      * JCC 6.1
122      *
123      */

124     protected void parseObject( Node aNode ) {
125         // often used
126
ListIterator JavaDoc it;
127         // linenumber
128
jco.setCommentEnd( aNode.getBeginLine() );
129         printLog( aNode, "parseObject" + aNode );
130         
131         // package ready
132
if (aNode instanceof PackageDeclaration) {
133             jco.printPackageComment( aNode );
134             jco.setCommentStart( aNode.getBeginLine() );
135             
136             setToOut( "package " + jco.getHighSplitLevel() + ((PackageDeclaration)aNode).getName() + ";" );
137             printOut();
138             return;
139         }
140         // import ready
141
if (aNode instanceof ImportDeclaration) {
142             jco.printImportComment( aNode );
143             jco.setCommentStart( aNode.getBeginLine() );
144             
145             setToOut( "import " + jco.getHighSplitLevel() + ((ImportDeclaration)aNode).getName() );
146             if (((ImportDeclaration)aNode).isPackage()) {
147                 addToOut( ".*" );
148                 printLog( aNode, "import statement with *" );
149             }
150             printOut( ";" );
151             return;
152         }
153         // class ready
154
if (aNode instanceof ClassDeclaration) {
155             ClassDeclaration classNode = (ClassDeclaration)aNode;
156             jco.printClassComment( classNode );
157             jco.setCommentStart( aNode.getBeginLine() );
158             
159             
160             //
161
isInterface = false;
162             // access_flags
163
setToOut( ConstantsManager.getModifierString( classNode.getAccessFlags() ) );
164             // superclass
165
addToOut( jco.getLowSplitLevel() + "class " + classNode.getName() + " " + jco.getHighSplitLevel()
166                     + "extends " + classNode.getSuperclass() );
167             // interfaces?
168
List JavaDoc interfaces = classNode.getInterfaces();
169             if (interfaces != null && !interfaces.isEmpty()) {
170                 it = interfaces.listIterator();
171                 addToOut( " " + jco.getHighSplitLevel() + "implements " );
172                 while (it.hasNext()) {
173                     addToOut( it.next() + "," + jco.getMiddleSplitLevel() + " " );
174                 }
175                 if (getOut().endsWith( "," + jco.getMiddleSplitLevel() + " " )) {
176                     setToOut( getOut().substring( 0,
177                             getOut().length() - ("," + jco.getMiddleSplitLevel() + " ").length() ) );
178                 }
179             }
180             printOut( " {" );
181             // class members
182
it = classNode.getMembers().listIterator();
183             jco.increaseIndent();
184             while (it.hasNext()) {
185                 parseObject( (Node)it.next() );
186             }
187             jco.setCommentEnd( aNode.getEndLine() );
188             jco.printComment();
189             jco.setCommentStart( aNode.getEndLine() );
190             
191             jco.decreaseIndent();
192             printOut( "}" );
193             return;
194         }
195         // interface
196
if (aNode instanceof InterfaceDeclaration) {
197             InterfaceDeclaration interfaceNode = (InterfaceDeclaration)aNode;
198             jco.printInterfaceComment( interfaceNode );
199             jco.setCommentStart( aNode.getBeginLine() );
200             
201             
202             //
203
isInterface = true;
204             // access_flags
205
setToOut( ConstantsManager.getModifierString( interfaceNode.getAccessFlags() ) );
206             // superclass
207
addToOut( jco.getLowSplitLevel() + "interface " + interfaceNode.getName() + " " );
208             // interfaces?
209
List JavaDoc interfaces = interfaceNode.getInterfaces();
210             if (interfaces != null && !interfaces.isEmpty()) {
211                 it = interfaces.listIterator();
212                 addToOut( jco.getHighSplitLevel() + "extends " );
213                 while (it.hasNext()) {
214                     addToOut( it.next() + "," + jco.getMiddleSplitLevel() + " " );
215                 }
216                 if (getOut().endsWith( "," + jco.getMiddleSplitLevel() + " " )) {
217                     setToOut( getOut().substring( 0,
218                             getOut().length() - ("," + jco.getMiddleSplitLevel() + " ").length() ) );
219                 }
220             }
221             printOut( " {" );
222             // class members
223
it = interfaceNode.getMembers().listIterator();
224             jco.increaseIndent();
225             while (it.hasNext()) {
226                 parseObject( (Node)it.next() );
227             }
228             jco.setCommentEnd( aNode.getEndLine() );
229             jco.printComment();
230             jco.setCommentStart( aNode.getEndLine() );
231             
232             jco.decreaseIndent();
233             printOut( "}" );
234             return;
235         }
236         // field working
237
if (aNode instanceof FieldDeclaration) {
238             FieldDeclaration fieldNode = (FieldDeclaration)aNode;
239             jco.printFieldComment( fieldNode );
240             jco.setCommentStart( aNode.getBeginLine() );
241             
242             // access_flags
243
setToOut( ConstantsManager.getModifierString( fieldNode.getAccessFlags() ) );
244             // type
245
addToOut( eh.getTypeString( fieldNode.getType() ) + " " );
246             // name
247
addToOut( fieldNode.getName() );
248             // initializer
249
if (fieldNode.getInitializer() != null) {
250                 addToOut( " = " + jco.getHighSplitLevel() );
251                 eh.addSuperConditionString( fieldNode.getInitializer() );
252             }
253             addToOut( ";" );
254             printOut();
255             return;
256         }
257         // same as FieldDeclaration + isFinal
258
if (aNode instanceof VariableDeclaration) {
259             // jco.setLastLineNumber(aNode.getEndLine());
260
VariableDeclaration varNode = (VariableDeclaration)aNode;
261             jco.printVariableComment( varNode );
262             jco.setCommentStart( aNode.getBeginLine() );
263             
264             setToOut( "" );
265             if (varNode.isFinal()) {
266                 addToOut( "final " );
267             }
268             // type
269
addToOut( eh.getTypeString( varNode.getType() ) + " " );
270             // name
271
addToOut( varNode.getName() );
272             // initializer
273
if (varNode.getInitializer() != null) {
274                 addToOut( " = " + jco.getHighSplitLevel() );
275                 eh.addSuperConditionString( varNode.getInitializer() );
276             }
277             addToOut( ";" );
278             printOut();
279             return;
280         }
281         
282         if (aNode instanceof MethodDeclaration) {
283             MethodDeclaration methodNode = (MethodDeclaration)aNode;
284             jco.printMethodComment( methodNode );
285             jco.setCommentStart( aNode.getBeginLine() );
286             
287             // access_flags
288
setToOut( ConstantsManager.getModifierString( methodNode.getAccessFlags() ) );
289             // type
290
addToOut( eh.getTypeString( methodNode.getReturnType() ) + " " );
291             // name + parameterlist
292
addToOut( methodNode.getName() + "(" + getParametersString( methodNode.getParameters() ) + ")" );
293             // Exceptions
294
it = methodNode.getExceptions().listIterator();
295             if (it.hasNext()) {
296                 addToOut( " " + jco.getHighSplitLevel() + "throws" );
297             }
298             while (it.hasNext()) {
299                 addToOut( " " + (String JavaDoc)it.next() + "," + jco.getMiddleSplitLevel() );
300             }
301             if (getOut().endsWith( "," + jco.getMiddleSplitLevel() )) {
302                 setToOut( getOut().substring( 0, getOut().length() - ("," + jco.getMiddleSplitLevel()).length() ) );
303             }
304             
305             // { if right method, ; if abstract method
306
if (ConstantsManager.getModifierString( methodNode.getAccessFlags() ).indexOf( "abstract" ) == -1
307                     && !isInterface) {
308                 // addToOut( " {" );
309
//
310
// printOut();
311
// all body nodes
312
// jco.increaseIndent();
313
parseObject( methodNode.getBody() );
314                 // printOut("MethodDeclaration end");
315
jco.setCommentEnd( aNode.getEndLine() );
316                 jco.printComment();
317                 jco.setCommentStart( aNode.getEndLine() );
318                 
319                 // jco.decreaseIndent();
320
//
321
printOut();
322             } else {
323                 // abstract
324
addToOut( ";" );
325                 //
326
printOut();
327             }
328             return;
329         }
330         // constructor Declaration similar as MethodDeclaration
331
if (aNode instanceof ConstructorDeclaration) {
332             ConstructorDeclaration constructorNode = (ConstructorDeclaration)aNode;
333             jco.printConstructorComment( constructorNode );
334             jco.setCommentStart( aNode.getBeginLine() );
335             
336             // access_flags
337
setToOut( ConstantsManager.getModifierString( constructorNode.getAccessFlags() ) );
338             // name + parameterlist
339
addToOut( constructorNode.getName() + "(" + getParametersString( constructorNode.getParameters() ) + ") " );
340             // Exceptions
341
it = constructorNode.getExceptions().listIterator();
342             if (it.hasNext()) {
343                 addToOut( jco.getHighSplitLevel() + "throws " );
344             }
345             while (it.hasNext()) {
346                 addToOut( (String JavaDoc)it.next() + "," + jco.getMiddleSplitLevel() + " " );
347             }
348             if (getOut().endsWith( "," + jco.getMiddleSplitLevel() + " " )) {
349                 setToOut( getOut().substring( 0,
350                         getOut().length() - ("," + jco.getMiddleSplitLevel() + " ").length() ) );
351             }
352             addToOut( "{" );
353             printOut();
354             //
355
jco.increaseIndent();
356             // this or super constructor invocation as first statement
357
if (constructorNode.getConstructorInvocation() != null) {
358                 parseObject( constructorNode.getConstructorInvocation() );
359             }
360             //
361
// all constructor statements
362
it = constructorNode.getStatements().listIterator();
363             while (it.hasNext()) {
364                 parseObject( (Node)it.next() );
365             }
366             jco.setCommentEnd( aNode.getEndLine() );
367             jco.printComment();
368             jco.setCommentStart( aNode.getEndLine() );
369             
370             jco.decreaseIndent();
371             printOut( "}" );
372             return;
373         }
374         // return
375
if (aNode instanceof ConstructorInvocation) {
376             ConstructorInvocation ci = (ConstructorInvocation)aNode;
377             
378             if (ci.isSuper()) {
379                 setToOut( "super(" );
380             } else {
381                 setToOut( "this(" );
382             }
383             eh.addConditionListString( ci.getArguments() );
384             printOut( ");" );
385             return;
386         }
387         
388         // class initializer static { }
389
if (aNode instanceof ClassInitializer) {
390             ClassInitializer ci = (ClassInitializer)aNode;
391             jco.printComment();
392             jco.setCommentStart( aNode.getBeginLine() );
393             
394             setToOut( "static" );
395             // printOut();
396
//
397
// jco.increaseIndent();
398
// the contained block
399
if (ci.getBlock() != null) {
400                 parseObject( ci.getBlock() );
401             }
402             //
403
jco.setCommentEnd( aNode.getEndLine() );
404             jco.printComment();
405             jco.setCommentStart( aNode.getEndLine() );
406             // jco.decreaseIndent();
407
printOut();
408             return;
409         }
410         
411         //
412
// statements
413
//
414

415         // common block
416
// not really a statement so no comment must be printed out
417
if (aNode instanceof BlockStatement) {
418             printOut( " {" );
419             jco.increaseIndent();
420             it = ((BlockStatement)aNode).getStatements().listIterator();
421             while (it.hasNext()) {
422                 parseObject( (Node)it.next() );
423             }
424             // printOut();
425
jco.decreaseIndent();
426             setToOut( "} " );
427             // following comments
428
jco.setCommentEnd( aNode.getEndLine() );
429             jco.printComment();
430             jco.setCommentStart( aNode.getEndLine() );
431             
432             return;
433         }
434         
435         // no comment could be created from here, therefore no extra function
436
jco.printComment();
437         jco.setCommentStart( aNode.getBeginLine() );
438         
439         
440         // all known 16 statements
441
// a sweet semikolon only
442
if (aNode instanceof EmptyStatement) {
443             printLog( aNode, "empty statement." );
444             printOut( ";" );
445             return;
446         }
447         // return
448
if (aNode instanceof ReturnStatement) {
449             setToOut( "return" );
450             if (((ReturnStatement)aNode).getExpression() != null) {
451                 addToOut( " " + jco.getMiddleSplitLevel() );
452                 eh.addSuperConditionString( ((ReturnStatement)aNode).getExpression() );
453             }
454             addToOut( ";" );
455             printOut();
456             return;
457         }
458         
459         if (aNode instanceof IfThenElseStatement) {
460             // binary expressions start with ( and end with )
461
// it should be delete in if ((binaryExpression))
462
setToOut( "if (" );
463             eh.addSuperConditionString( ((IfThenElseStatement)aNode).getCondition() );
464             addToOut( ")" );
465             insertBlockStatement( ((IfThenElseStatement)aNode).getThenStatement() );
466             
467             // nested if-then-else
468
printNestedIfThenElse( ((IfThenElseStatement)aNode).getElseStatement() );
469             printOut();
470             return;
471         }
472         if (aNode instanceof IfThenStatement) {
473             // binary expressions start with ( and end with )
474
// it should be delete in if ((binaryExpression))
475
setToOut( "if (" );
476             eh.addSuperConditionString( ((IfThenStatement)aNode).getCondition() );
477             addToOut( ")" );
478             // check if blockstatement otherwise include { }
479
insertBlockStatement( ((IfThenStatement)aNode).getThenStatement() );
480             
481             printOut();
482             return;
483         }
484         // synchronized(var)
485
if (aNode instanceof SynchronizedStatement) {
486             // binary expressions start with ( and end with )
487
// it should be delete in if ((binaryExpression))
488
setToOut( "synchronized (" );
489             eh.addSuperConditionString( ((SynchronizedStatement)aNode).getLock() );
490             addToOut( ")" );
491             // printOut();
492
// jco.increaseIndent();
493
parseObject( ((SynchronizedStatement)aNode).getBody() );
494             // jco.decreaseIndent();
495
printOut();
496             return;
497         }
498         // loops
499
// while
500
if (aNode instanceof WhileStatement) {
501             WhileStatement ws = (WhileStatement)aNode;
502             setToOut( "while (" );
503             eh.addSuperConditionString( ws.getCondition() );
504             addToOut( ")" );
505             // empty while only
506
if (ws.getBody() instanceof EmptyStatement || ws.getBody() instanceof BlockStatement
507                     && ((BlockStatement)ws.getBody()).getStatements().size() < 1) {
508                 printOut( ";" );
509             } else {
510                 insertBlockStatement( ws.getBody() );
511                 printOut();
512             }
513             return;
514         }
515         // do
516
if (aNode instanceof DoStatement) {
517             setToOut( "do" );
518             // jco.increaseIndent();
519
parseObject( ((DoStatement)aNode).getBody() );
520             // jco.decreaseIndent();
521
addToOut( "while (" );
522             eh.addSuperConditionString( ((DoStatement)aNode).getCondition() );
523             addToOut( ");" );
524             printOut();
525             return;
526         }
527         // for(i=0;i<j;i++)
528
if (aNode instanceof ForStatement) {
529             ForStatement fs = (ForStatement)aNode;
530             setToOut( "for (" );
531             
532             // i=0
533
addVariableDeclarationListString( fs.getInitialization() );
534             addToOut( ";" );
535             if (fs.getCondition() != null) {
536                 addToOut( " " );
537             }
538             // i<j
539
eh.addSuperConditionString( fs.getCondition() );
540             addToOut( ";" );
541             // i++
542
eh.addConditionListString( fs.getUpdate(), false );
543             addToOut( ")" );
544             // empty loop?
545
if (fs.getBody() instanceof EmptyStatement || fs.getBody() instanceof BlockStatement
546                     && ((BlockStatement)fs.getBody()).getStatements().size() < 1) {
547                 printOut( ";" );
548             } else {
549                 insertBlockStatement( fs.getBody() );
550                 // addToOut( " {" );
551
printOut();
552             // body
553
// jco.increaseIndent();
554
// parseObject( fs.getBody() );
555
// jco.decreaseIndent();
556
// printOut();
557
}
558             return;
559         }
560         // switch
561
if (aNode instanceof SwitchStatement) {
562             SwitchStatement ss = (SwitchStatement)aNode;
563             setToOut( "switch (" );
564             eh.addSuperConditionString( ss.getSelector() );
565             addToOut( ") {" );
566             printOut();
567             // empty while only
568
it = ss.getBindings().listIterator();
569             // case lines
570
while (it.hasNext()) {
571                 parseObject( (Node)it.next() );
572             }
573             jco.setCommentEnd( aNode.getEndLine() );
574             jco.printComment();
575             jco.setCommentStart( aNode.getEndLine() );
576             printOut( "}" );
577             return;
578         }
579         // switch
580
if (aNode instanceof SwitchBlock) {
581             SwitchBlock sb = (SwitchBlock)aNode;
582             if (sb.getExpression() != null) {
583                 setToOut( "case " );
584                 eh.addSuperConditionString( sb.getExpression() );
585                 addToOut( ":" );
586             } else {
587                 setToOut( "default:" );
588             }
589             // printOut();
590
if (sb.getStatements() != null) {
591                 
592                 // jco.increaseIndent();
593
it = sb.getStatements().listIterator();
594                 // case lines
595
if (it.hasNext()) {
596                     Node node = (Node)it.next();
597                     if (node instanceof BlockStatement) {
598                         // printOut inside blockstatement
599
parseObject( node );
600                         jco.increaseIndent();
601                         printOut();
602                         jco.decreaseIndent();
603                     } else {
604                         printOut();
605                         jco.increaseIndent();
606                         parseObject( node );
607                         jco.decreaseIndent();
608                     }
609                 }
610                 // case lines
611
while (it.hasNext()) {
612                     Node node = (Node)it.next();
613                     jco.increaseIndent();
614                     parseObject( node );
615                     jco.decreaseIndent();
616                     if (node instanceof BlockStatement) {
617                         // printOut inside blockstatement
618
jco.increaseIndent();
619                         printOut();
620                         jco.decreaseIndent();
621                     }
622                 }
623                 // following comments
624
jco.setCommentEnd( aNode.getEndLine() );
625                 jco.printComment();
626                 jco.setCommentStart( aNode.getEndLine() );
627             
628             // jco.decreaseIndent();
629
} else {
630                 // no statements
631
printOut();
632             }
633             return;
634         }
635         
636         // try catch finally
637
if (aNode instanceof TryStatement) {
638             TryStatement tryS = (TryStatement)aNode;
639             // try
640
setToOut( "try" );
641             // jco.increaseIndent();
642
parseObject( tryS.getTryBlock() );
643             // jco.decreaseIndent();
644
// setToOut( "} " );
645
// catch
646
it = tryS.getCatchStatements().listIterator();
647             while (it.hasNext()) {
648                 parseObject( (Node)it.next() );
649             }
650             
651             // finally
652
if (tryS.getFinallyBlock() != null) {
653                 addToOut( "finally" );
654                 // jco.increaseIndent();
655
parseObject( ((TryStatement)aNode).getFinallyBlock() );
656                 
657                 // jco.decreaseIndent();
658
printOut();
659             
660             } else {
661                 printOut();
662             }
663             return;
664         }
665         if (aNode instanceof CatchStatement) {
666             // catch on same line as last closed bracket
667
// JCC 7.9
668
addToOut( "catch (" );
669             // exception as FormalParameter
670
FormalParameter fp = ((CatchStatement)aNode).getException();
671             addToOut( eh.getTypeString( fp.getType() ) + " " + fp.getName() + ")" );
672             // printOut();
673

674             // jco.increaseIndent();
675
parseObject( ((CatchStatement)aNode).getBlock() );
676             // jco.decreaseIndent();
677
// setToOut( "} " );
678
return;
679         }
680         // throw
681
if (aNode instanceof ThrowStatement) {
682             setToOut( "throw " );
683             eh.addSuperConditionString( ((ThrowStatement)aNode).getExpression() );
684             printOut( ";" );
685             return;
686         }
687         
688         // reserved words
689
// continue
690
if (aNode instanceof ContinueStatement) {
691             printOut( "continue;" );
692             return;
693         }
694         // break
695
if (aNode instanceof BreakStatement) {
696             printOut( "break;" );
697             return;
698         }
699         //
700

701         
702         // expression only in other method
703
if (aNode instanceof Expression) {
704             setToOut( "" );
705             eh.addSuperConditionString( (Expression)aNode );
706             addToOut( ";" );
707             printOut();
708             return;
709         }
710         //
711
printErr( aNode, "parseObject Node " + aNode + " not found on line " + aNode.getBeginLine() );
712         return;
713     }
714     
715     
716     /**
717      *
718      */

719     private void insertBlockStatement( Node node ) {
720         // System.err.println("insertBlockStatement");
721
if (node instanceof BlockStatement) {
722             // System.err.println("insertBlockStatement is block");
723
parseObject( node );
724         } else {
725             printOut( " {" );
726             jco.increaseIndent();
727             parseObject( node );
728             jco.decreaseIndent();
729             setToOut( "} " );
730         }
731     }
732     
733     
734     /**
735      * Prints nested if-then-else.
736      */

737     private void printNestedIfThenElse( Node aNode ) {
738         // nested if-then-else
739
if (aNode instanceof IfThenElseStatement) {
740             addToOut( "else if (" );
741             eh.addSuperConditionString( ((IfThenElseStatement)aNode).getCondition() );
742             addToOut( ")" );
743             // jco.increaseIndent();
744
insertBlockStatement( ((IfThenElseStatement)aNode).getThenStatement() );
745             // jco.decreaseIndent();
746
if (((IfThenElseStatement)aNode).getElseStatement() instanceof IfThenElseStatement) {
747                 printNestedIfThenElse( ((IfThenElseStatement)aNode).getElseStatement() );
748             } else {
749                 // the else is an if-then-else
750
addToOut( "else" );
751                 // jco.increaseIndent();
752
insertBlockStatement( ((IfThenElseStatement)aNode).getElseStatement() );
753             // jco.decreaseIndent();
754
}
755         } else {
756             // aNode is not an if-then-else
757
addToOut( "else" );
758             // jco.increaseIndent();
759
insertBlockStatement( aNode );
760         // jco.decreaseIndent();
761
}
762     }
763     
764     
765     /**
766      * Help method to encapsulate a often used loop in parseObject(declarations).
767      *
768      * @return A String consist of several parameters in a List separated by
769      * ',MiddleSplitLevel ' with a LowSplitLevel if more than one element in it
770      *
771      */

772     private String JavaDoc getParametersString( List JavaDoc someParameters ) {
773         boolean findSome = false;
774         String JavaDoc ret = "";
775         if (someParameters != null) {
776             // if (someParameters.size()>1) {
777
ret += jco.getLowSplitLevel();
778             // }
779
ListIterator JavaDoc it = someParameters.listIterator();
780             while (it.hasNext()) {
781                 findSome = true;
782                 ret += " ";
783                 FormalParameter param = (FormalParameter)it.next();
784                 if (param.isFinal()) {
785                     ret += "final ";
786                 }
787                 ret += eh.getTypeString( param.getType() ) + " " + param.getName() + "," + jco.getMiddleSplitLevel();
788             }
789             if (ret.endsWith( "," + jco.getMiddleSplitLevel() )) {
790                 ret = ret.substring( 0, ret.length() - ("," + jco.getMiddleSplitLevel()).length() );
791             }
792         }
793         if (findSome) {
794             ret += " ";
795         }
796         return ret;
797     }
798     
799     
800     /**
801      * Help method to encapsulate a used loop in parseObject(ForStatement).
802      *
803      * A String consist of several parameters in a List separated by
804      * ',MiddleSplitLevel ' with a LowSplitLevel if more than one element in it
805      *
806      */

807     private void addVariableDeclarationListString( List JavaDoc someParameters ) {
808         Node node;
809         if (someParameters != null) {
810             // if (someParameters.size()>1) {
811
addToOut( jco.getLowSplitLevel() );
812             // }
813
ListIterator JavaDoc it = someParameters.listIterator();
814             if (it.hasNext()) {
815                 // VariableDeclarations or SimpleAssignmentExpression
816
node = (Node)it.next();
817                 if (node instanceof VariableDeclaration) {
818                     VariableDeclaration vd = (VariableDeclaration)node;
819                     if (vd.isFinal()) {
820                         addToOut( "final " );
821                     }
822                     // type
823
addToOut( eh.getTypeString( vd.getType() ) + " " );
824                     // name
825
addToOut( vd.getName() );
826                     // initializer
827
if (vd.getInitializer() != null) {
828                         addToOut( " = " + jco.getHighSplitLevel() );
829                         eh.addSuperConditionString( vd.getInitializer() );
830                     }
831                 } else {
832                     eh.addSuperConditionString( (Expression)node );
833                 }
834                 addToOut( "," + jco.getMiddleSplitLevel() + " " );
835             }
836             while (it.hasNext()) {
837                 // VariableDeclarations or SimpleAssignmentExpression
838
node = (Node)it.next();
839                 if (node instanceof VariableDeclaration) {
840                     VariableDeclaration vd = (VariableDeclaration)node;
841                     // name
842
addToOut( vd.getName() );
843                     // initializer
844
if (vd.getInitializer() != null) {
845                         addToOut( " = " + jco.getHighSplitLevel() );
846                         eh.addSuperConditionString( vd.getInitializer() );
847                     }
848                 } else {
849                     eh.addSuperConditionString( (Expression)node );
850                 }
851                 addToOut( "," + jco.getMiddleSplitLevel() + " " );
852             }
853             if (getOut().endsWith( "," + jco.getMiddleSplitLevel() + " " )) {
854                 setToOut( getOut().substring( 0,
855                         getOut().length() - ("," + jco.getMiddleSplitLevel() + " ").length() ) );
856             }
857         }
858     }
859     
860     
861     //
862
// out line management
863
//
864

865     
866     /**
867      * Adds the specified string to the global variable LineOut
868      * @param add The String to add.
869      */

870     public void addToOut( String JavaDoc add ) {
871         outLine += add;
872     }
873     
874     
875     public void setToOut( String JavaDoc string ) {
876         outLine = string;
877     }
878     
879     
880     public void printOut() {
881         jco.println( outLine );
882         setToOut( "" );
883     }
884     
885     
886     /**
887      * Adds the specified String and invoke the println method in the Outputclass.
888      * Set the lineOut class member to ""
889      *
890      * @param add The String to add.
891      */

892     public void printOut( String JavaDoc add ) {
893         addToOut( add );
894         printOut();
895     }
896     
897     
898     public String JavaDoc getOut() {
899         return outLine;
900     }
901     
902     
903     /**
904      * This method logs warnings on code violations.
905      * @param aLine
906      *
907      */

908     private void printLog( Node aNode, String JavaDoc aLine ) {
909         // System.err.println(getClass()+" Log line "+aNode.getBeginLine()+": "+aLine);
910
;
911     }
912     
913     
914     private void printLog( String JavaDoc aLine ) {
915         // System.err.println(getClass()+" Log: "+aLine);
916
;
917     }
918     
919     
920     /**
921      * This method print debug messages.
922      * @param aLine
923      *
924      */

925     private void printErr( Node aNode, String JavaDoc aLine ) {
926         System.err.println( getClass() + " ERROR: " + aLine );
927     }
928     
929     
930     /** */
931     public static void printHelp() {
932         System.out.println( "usage: jAnalyzer [-o=<outputfile>|-] [-linelength=<number>] [-h|-?|-help] inputfile" );
933     }
934     
935     
936     /**
937      *
938      *
939      */

940     public static void main( String JavaDoc[] argv ) {
941         String JavaDoc fileIn = null;
942         String JavaDoc fileOut = null;
943         String JavaDoc lineLength = null;
944         
945         int i = 0;
946         if (argv.length > 0) {
947             for (i = 0; i < argv.length; i++) {
948                 if (argv[i].startsWith( "-o=" )) {
949                     fileOut = argv[i].substring( "-o=".length() );
950                     continue;
951                 }
952                 if (argv[i].startsWith( "-linelength=" )) {
953                     lineLength = argv[i].substring( "-linelength=".length() );
954                     continue;
955                 }
956                 if (argv[i].equals( "-h" ) || argv[i].equals( "-?" ) || argv[i].equals( "-help" )) {
957                     printHelp();
958                     System.exit( 0 );
959                 }
960                 // no parameter, must be inputfile
961
fileIn = argv[i];
962                 break;
963             }
964         }
965         if (fileIn == null) {
966             printHelp();
967             System.exit( 0 );
968         }
969         if (fileOut != null && fileOut.equals( "-" )) {
970             new JavaCodeAnalyzer( fileIn, fileIn, lineLength );
971         } else {
972             new JavaCodeAnalyzer( fileIn, fileOut, lineLength );
973         }
974         
975         
976         // more than one inputfile
977
if (fileOut != null && fileOut.equals( "-" )) {
978             while (i < argv.length) {
979                 new JavaCodeAnalyzer( argv[i], argv[i], lineLength );
980                 i++;
981             }
982         } else {
983             while (i < argv.length) {
984                 new JavaCodeAnalyzer( argv[i], fileOut, lineLength );
985                 i++;
986             }
987         }
988     }
989 }
990
Popular Tags