Source Code Cross Referenced for JavaCodeAnalyzer.java in  » Database-DBMS » Ozone-1.1 » org » infozone » tools » janalyzer » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database DBMS » Ozone 1.1 » org.infozone.tools.janalyzer 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


0001:        // You can redistribute this software and/or modify it under the terms of
0002:        // the Infozone Software License version 2 published by the Infozone Group
0003:        // (http://www.infozone-group.org).
0004:        //
0005:        // Copyright (C) @year@ by The Infozone Group. All rights reserved.
0006:        //
0007:        // $Id: JavaCodeAnalyzer.java,v 1.1 2002/05/10 08:59:12 per_nyfelt Exp $
0008:
0009:        package org.infozone.tools.janalyzer;
0010:
0011:        import koala.dynamicjava.parser.wrapper.*; //import koala.dynamicjava.interpreter.context.wrapper.*;
0012:        import koala.dynamicjava.interpreter.context.*;
0013:        import koala.dynamicjava.interpreter.*;
0014:        import koala.dynamicjava.tree.*;
0015:        import koala.dynamicjava.tree.visitor.*;
0016:
0017:        import java.util.List;
0018:        import java.util.ListIterator;
0019:        import java.io.File;
0020:        import java.io.FileReader;
0021:        import java.io.FileWriter;
0022:        import java.io.BufferedReader;
0023:        import java.io.BufferedWriter;
0024:        import java.io.Serializable;
0025:        import java.util.Vector;
0026:
0027:        /**
0028:         * This class parses a existing JAVA class and format's the source code
0029:         * after the Code Conventions from Sun and therefore from the Apache Software Foundation.
0030:         * It uses the Syntax tree from the DynamicJava SourceCodeParser and transform each node of
0031:         * this tree back into lines of code.
0032:         * All output is handled by the JavaCodeOutput class. Also comments only handled by the Output
0033:         * class, because they don't exist in a syntax tree.
0034:         *
0035:         *
0036:         * JCC in the comment stands for
0037:         * <a href=http://xml.apache.org/source.html>Coding Conventions</a>.
0038:         *
0039:         * <pre>
0040:         *
0041:         * TODO:
0042:         *  - forgotten expressions they not used in the prowler package
0043:         *  - indentation in nested conditional operator statements
0044:         *  - comments after the last expression in a method and the trailing } are deleted!!!
0045:         *  - split level in binary expressions
0046:         *  - label:
0047:         *  - more documentation
0048:         *  - quality checking such:
0049:         *     - constants as ABC_DEF
0050:         *  - JavaCodeMetricManager
0051:         *  - metric'S
0052:         *     - method counting and so on
0053:         *
0054:         * Problems:
0055:         *  - trailling comments can't be inserted after the automatically wrapped lines, so
0056:         *      they are inserted before the statements
0057:         *  - At this moment only the trailling comment on the same line as the statement starts
0058:         *      is checked
0059:         * </pre>
0060:         *
0061:         * @version $Revision: 1.1 $ $Date: 2002/05/10 08:59:12 $
0062:         * @author <a href="http://www.softwarebuero.de">SMB</a>
0063:         */
0064:        public final class JavaCodeAnalyzer extends java.lang.Object {
0065:
0066:            //
0067:            // data
0068:            //
0069:
0070:            /** The Name of the file to prepare. */
0071:            //    private String filename;
0072:            /** The string for concating several smaller output strings to one line. */
0073:            private String outLine = "";
0074:            /** The output class */
0075:            private JavaCodeOutput jco;
0076:            /** The expression helper class */
0077:            private ExpressionHelper eh;
0078:            /** The level to help correct parentheses to binary expressions */
0079:            private int currentBinaryExpressionLevel = 0;
0080:
0081:            /** Source File is a interface Declaration, so methods doesnt have a body */
0082:            private boolean isInterface = false;
0083:
0084:            public JavaCodeAnalyzer(String filenameIn, String filenameOut,
0085:                    String lineLength) {
0086:                //        filename = filenameIn;
0087:
0088:                //
0089:                try {
0090:                    // output class
0091:                    // copy input file
0092:                    File tmp = File.createTempFile("JavaCodeAnalyzer", "tmp");
0093:                    BufferedReader br = new BufferedReader(new FileReader(
0094:                            filenameIn));
0095:                    BufferedWriter out = new BufferedWriter(new FileWriter(tmp));
0096:                    while (br.ready()) {
0097:                        out.write(br.read());
0098:                    }
0099:                    br.close();
0100:                    out.close();
0101:
0102:                    jco = new JavaCodeOutput(tmp, filenameOut, lineLength);
0103:                    SourceCodeParser p = new JavaCCParserFactory()
0104:                            .createParser(new FileReader(tmp), null);
0105:                    //filenameIn);
0106:                    List statements = p.parseCompilationUnit();
0107:                    ListIterator it = statements.listIterator();
0108:                    eh = new ExpressionHelper(this , jco);
0109:                    Node n;
0110:                    printLog("Parsed file " + filenameIn + "\n");
0111:                    while (it.hasNext()) {
0112:                        n = (Node) it.next();
0113:                        parseObject(n);
0114:                    }
0115:                    tmp.delete();
0116:                } catch (Exception e) {
0117:                    System.err.println(getClass() + ": " + e);
0118:                }
0119:            }
0120:
0121:            /**
0122:             * This method parses each Node in the syntax tree.
0123:             * The String out is used for concatenation of the whole single strings.
0124:             *
0125:             * Errors:
0126:             *   On FieldDeclaration: only one declaration per line is in the tree.
0127:             *			JCC 6.1
0128:             *
0129:             */
0130:            protected void parseObject(Node aNode) {
0131:                // often used
0132:                ListIterator it;
0133:                // linenumber
0134:                jco.setCommentEnd(aNode.getBeginLine());
0135:                printLog(aNode, "parseObject" + aNode);
0136:
0137:                // package ready
0138:                if (aNode instanceof  PackageDeclaration) {
0139:                    jco.printPackageComment(aNode);
0140:                    jco.setCommentStart(aNode.getBeginLine());
0141:
0142:                    setToOut("package " + jco.getHighSplitLevel()
0143:                            + ((PackageDeclaration) aNode).getName() + ";");
0144:                    printOut();
0145:                    return;
0146:                }
0147:                // import ready
0148:                if (aNode instanceof  ImportDeclaration) {
0149:                    jco.printImportComment(aNode);
0150:                    jco.setCommentStart(aNode.getBeginLine());
0151:
0152:                    setToOut("import " + jco.getHighSplitLevel()
0153:                            + ((ImportDeclaration) aNode).getName());
0154:                    if (((ImportDeclaration) aNode).isPackage()) {
0155:                        addToOut(".*");
0156:                        printLog(aNode, "import statement with *");
0157:                    }
0158:                    printOut(";");
0159:                    return;
0160:                }
0161:                // class ready
0162:                if (aNode instanceof  ClassDeclaration) {
0163:                    ClassDeclaration classNode = (ClassDeclaration) aNode;
0164:                    jco.printClassComment(classNode);
0165:                    jco.setCommentStart(aNode.getBeginLine());
0166:
0167:                    //
0168:                    isInterface = false;
0169:                    // access_flags
0170:                    setToOut(ConstantsManager.getModifierString(classNode
0171:                            .getAccessFlags()));
0172:                    // superclass
0173:                    addToOut(jco.getLowSplitLevel() + "class "
0174:                            + classNode.getName() + " "
0175:                            + jco.getHighSplitLevel() + "extends "
0176:                            + classNode.getSuperclass());
0177:                    // interfaces?
0178:                    List interfaces = classNode.getInterfaces();
0179:                    if (interfaces != null && !interfaces.isEmpty()) {
0180:                        it = interfaces.listIterator();
0181:                        addToOut(" " + jco.getHighSplitLevel() + "implements ");
0182:                        while (it.hasNext()) {
0183:                            addToOut(it.next() + ","
0184:                                    + jco.getMiddleSplitLevel() + " ");
0185:                        }
0186:                        if (getOut().endsWith(
0187:                                "," + jco.getMiddleSplitLevel() + " ")) {
0188:                            setToOut(getOut()
0189:                                    .substring(
0190:                                            0,
0191:                                            getOut().length()
0192:                                                    - (","
0193:                                                            + jco
0194:                                                                    .getMiddleSplitLevel() + " ")
0195:                                                            .length()));
0196:                        }
0197:                    }
0198:                    printOut(" {");
0199:                    // class members
0200:                    it = classNode.getMembers().listIterator();
0201:                    jco.increaseIndent();
0202:                    while (it.hasNext()) {
0203:                        parseObject((Node) it.next());
0204:                    }
0205:                    jco.setCommentEnd(aNode.getEndLine());
0206:                    jco.printComment();
0207:                    jco.setCommentStart(aNode.getEndLine());
0208:
0209:                    jco.decreaseIndent();
0210:                    printOut("}");
0211:                    return;
0212:                }
0213:                // interface
0214:                if (aNode instanceof  InterfaceDeclaration) {
0215:                    InterfaceDeclaration interfaceNode = (InterfaceDeclaration) aNode;
0216:                    jco.printInterfaceComment(interfaceNode);
0217:                    jco.setCommentStart(aNode.getBeginLine());
0218:
0219:                    //
0220:                    isInterface = true;
0221:                    // access_flags
0222:                    setToOut(ConstantsManager.getModifierString(interfaceNode
0223:                            .getAccessFlags()));
0224:                    // superclass
0225:                    addToOut(jco.getLowSplitLevel() + "interface "
0226:                            + interfaceNode.getName() + " ");
0227:                    // interfaces?
0228:                    List interfaces = interfaceNode.getInterfaces();
0229:                    if (interfaces != null && !interfaces.isEmpty()) {
0230:                        it = interfaces.listIterator();
0231:                        addToOut(jco.getHighSplitLevel() + "extends ");
0232:                        while (it.hasNext()) {
0233:                            addToOut(it.next() + ","
0234:                                    + jco.getMiddleSplitLevel() + " ");
0235:                        }
0236:                        if (getOut().endsWith(
0237:                                "," + jco.getMiddleSplitLevel() + " ")) {
0238:                            setToOut(getOut()
0239:                                    .substring(
0240:                                            0,
0241:                                            getOut().length()
0242:                                                    - (","
0243:                                                            + jco
0244:                                                                    .getMiddleSplitLevel() + " ")
0245:                                                            .length()));
0246:                        }
0247:                    }
0248:                    printOut(" {");
0249:                    // class members
0250:                    it = interfaceNode.getMembers().listIterator();
0251:                    jco.increaseIndent();
0252:                    while (it.hasNext()) {
0253:                        parseObject((Node) it.next());
0254:                    }
0255:                    jco.setCommentEnd(aNode.getEndLine());
0256:                    jco.printComment();
0257:                    jco.setCommentStart(aNode.getEndLine());
0258:
0259:                    jco.decreaseIndent();
0260:                    printOut("}");
0261:                    return;
0262:                }
0263:                // field working
0264:                if (aNode instanceof  FieldDeclaration) {
0265:                    FieldDeclaration fieldNode = (FieldDeclaration) aNode;
0266:                    jco.printFieldComment(fieldNode);
0267:                    jco.setCommentStart(aNode.getBeginLine());
0268:
0269:                    // access_flags
0270:                    setToOut(ConstantsManager.getModifierString(fieldNode
0271:                            .getAccessFlags()));
0272:                    // type
0273:                    addToOut(eh.getTypeString(fieldNode.getType()) + " ");
0274:                    // name
0275:                    addToOut(fieldNode.getName());
0276:                    // initializer
0277:                    if (fieldNode.getInitializer() != null) {
0278:                        addToOut(" = " + jco.getHighSplitLevel());
0279:                        eh.addSuperConditionString(fieldNode.getInitializer());
0280:                    }
0281:                    addToOut(";");
0282:                    printOut();
0283:                    return;
0284:                }
0285:                // same as FieldDeclaration + isFinal
0286:                if (aNode instanceof  VariableDeclaration) {
0287:                    //	jco.setLastLineNumber(aNode.getEndLine());
0288:                    VariableDeclaration varNode = (VariableDeclaration) aNode;
0289:                    jco.printVariableComment(varNode);
0290:                    jco.setCommentStart(aNode.getBeginLine());
0291:
0292:                    setToOut("");
0293:                    if (varNode.isFinal()) {
0294:                        addToOut("final ");
0295:                    }
0296:                    // type
0297:                    addToOut(eh.getTypeString(varNode.getType()) + " ");
0298:                    // name
0299:                    addToOut(varNode.getName());
0300:                    // initializer
0301:                    if (varNode.getInitializer() != null) {
0302:                        addToOut(" = " + jco.getHighSplitLevel());
0303:                        eh.addSuperConditionString(varNode.getInitializer());
0304:                    }
0305:                    addToOut(";");
0306:                    printOut();
0307:                    return;
0308:                }
0309:
0310:                if (aNode instanceof  MethodDeclaration) {
0311:                    MethodDeclaration methodNode = (MethodDeclaration) aNode;
0312:                    jco.printMethodComment(methodNode);
0313:                    jco.setCommentStart(aNode.getBeginLine());
0314:
0315:                    // access_flags
0316:                    setToOut(ConstantsManager.getModifierString(methodNode
0317:                            .getAccessFlags()));
0318:                    // type
0319:                    addToOut(eh.getTypeString(methodNode.getReturnType()) + " ");
0320:                    // name + parameterlist
0321:                    addToOut(methodNode.getName() + "("
0322:                            + getParametersString(methodNode.getParameters())
0323:                            + ")");
0324:                    // Exceptions
0325:                    it = methodNode.getExceptions().listIterator();
0326:                    if (it.hasNext()) {
0327:                        addToOut(" " + jco.getHighSplitLevel() + "throws");
0328:                    }
0329:                    while (it.hasNext()) {
0330:                        addToOut(" " + (String) it.next() + ","
0331:                                + jco.getMiddleSplitLevel());
0332:                    }
0333:                    if (getOut().endsWith("," + jco.getMiddleSplitLevel())) {
0334:                        setToOut(getOut().substring(
0335:                                0,
0336:                                getOut().length()
0337:                                        - ("," + jco.getMiddleSplitLevel())
0338:                                                .length()));
0339:                    }
0340:
0341:                    // { if right method, ; if abstract method
0342:                    if (ConstantsManager.getModifierString(
0343:                            methodNode.getAccessFlags()).indexOf("abstract") == -1
0344:                            && !isInterface) {
0345:                        //                addToOut( " {" );
0346:                        //
0347:                        //                printOut();
0348:                        // all body nodes
0349:                        //                jco.increaseIndent();
0350:                        parseObject(methodNode.getBody());
0351:                        //				printOut("MethodDeclaration end");
0352:                        jco.setCommentEnd(aNode.getEndLine());
0353:                        jco.printComment();
0354:                        jco.setCommentStart(aNode.getEndLine());
0355:
0356:                        //                jco.decreaseIndent();
0357:                        //
0358:                        printOut();
0359:                    } else {
0360:                        // abstract
0361:                        addToOut(";");
0362:                        //
0363:                        printOut();
0364:                    }
0365:                    return;
0366:                }
0367:                // constructor Declaration similar as MethodDeclaration
0368:                if (aNode instanceof  ConstructorDeclaration) {
0369:                    ConstructorDeclaration constructorNode = (ConstructorDeclaration) aNode;
0370:                    jco.printConstructorComment(constructorNode);
0371:                    jco.setCommentStart(aNode.getBeginLine());
0372:
0373:                    // access_flags
0374:                    setToOut(ConstantsManager.getModifierString(constructorNode
0375:                            .getAccessFlags()));
0376:                    // name + parameterlist
0377:                    addToOut(constructorNode.getName()
0378:                            + "("
0379:                            + getParametersString(constructorNode
0380:                                    .getParameters()) + ") ");
0381:                    // Exceptions
0382:                    it = constructorNode.getExceptions().listIterator();
0383:                    if (it.hasNext()) {
0384:                        addToOut(jco.getHighSplitLevel() + "throws ");
0385:                    }
0386:                    while (it.hasNext()) {
0387:                        addToOut((String) it.next() + ","
0388:                                + jco.getMiddleSplitLevel() + " ");
0389:                    }
0390:                    if (getOut()
0391:                            .endsWith("," + jco.getMiddleSplitLevel() + " ")) {
0392:                        setToOut(getOut()
0393:                                .substring(
0394:                                        0,
0395:                                        getOut().length()
0396:                                                - (","
0397:                                                        + jco
0398:                                                                .getMiddleSplitLevel() + " ")
0399:                                                        .length()));
0400:                    }
0401:                    addToOut("{");
0402:                    printOut();
0403:                    //
0404:                    jco.increaseIndent();
0405:                    // this or super constructor invocation as first statement
0406:                    if (constructorNode.getConstructorInvocation() != null) {
0407:                        parseObject(constructorNode.getConstructorInvocation());
0408:                    }
0409:                    //
0410:                    // all constructor statements
0411:                    it = constructorNode.getStatements().listIterator();
0412:                    while (it.hasNext()) {
0413:                        parseObject((Node) it.next());
0414:                    }
0415:                    jco.setCommentEnd(aNode.getEndLine());
0416:                    jco.printComment();
0417:                    jco.setCommentStart(aNode.getEndLine());
0418:
0419:                    jco.decreaseIndent();
0420:                    printOut("}");
0421:                    return;
0422:                }
0423:                // return
0424:                if (aNode instanceof  ConstructorInvocation) {
0425:                    ConstructorInvocation ci = (ConstructorInvocation) aNode;
0426:
0427:                    if (ci.isSuper()) {
0428:                        setToOut("super(");
0429:                    } else {
0430:                        setToOut("this(");
0431:                    }
0432:                    eh.addConditionListString(ci.getArguments());
0433:                    printOut(");");
0434:                    return;
0435:                }
0436:
0437:                // class initializer static { }
0438:                if (aNode instanceof  ClassInitializer) {
0439:                    ClassInitializer ci = (ClassInitializer) aNode;
0440:                    jco.printComment();
0441:                    jco.setCommentStart(aNode.getBeginLine());
0442:
0443:                    setToOut("static");
0444:                    //            printOut();
0445:                    //
0446:                    //            jco.increaseIndent();
0447:                    // the contained block
0448:                    if (ci.getBlock() != null) {
0449:                        parseObject(ci.getBlock());
0450:                    }
0451:                    //
0452:                    jco.setCommentEnd(aNode.getEndLine());
0453:                    jco.printComment();
0454:                    jco.setCommentStart(aNode.getEndLine());
0455:                    //            jco.decreaseIndent();
0456:                    printOut();
0457:                    return;
0458:                }
0459:
0460:                //
0461:                // statements
0462:                //
0463:
0464:                // common block
0465:                // not really a statement so no comment must be printed out
0466:                if (aNode instanceof  BlockStatement) {
0467:                    printOut(" {");
0468:                    jco.increaseIndent();
0469:                    it = ((BlockStatement) aNode).getStatements()
0470:                            .listIterator();
0471:                    while (it.hasNext()) {
0472:                        parseObject((Node) it.next());
0473:                    }
0474:                    //            printOut();
0475:                    jco.decreaseIndent();
0476:                    setToOut("} ");
0477:                    // following comments
0478:                    jco.setCommentEnd(aNode.getEndLine());
0479:                    jco.printComment();
0480:                    jco.setCommentStart(aNode.getEndLine());
0481:
0482:                    return;
0483:                }
0484:
0485:                // no comment could be created from here, therefore no extra function
0486:                jco.printComment();
0487:                jco.setCommentStart(aNode.getBeginLine());
0488:
0489:                // all known 16 statements
0490:                // a sweet semikolon only
0491:                if (aNode instanceof  EmptyStatement) {
0492:                    printLog(aNode, "empty statement.");
0493:                    printOut(";");
0494:                    return;
0495:                }
0496:                // return
0497:                if (aNode instanceof  ReturnStatement) {
0498:                    setToOut("return");
0499:                    if (((ReturnStatement) aNode).getExpression() != null) {
0500:                        addToOut(" " + jco.getMiddleSplitLevel());
0501:                        eh.addSuperConditionString(((ReturnStatement) aNode)
0502:                                .getExpression());
0503:                    }
0504:                    addToOut(";");
0505:                    printOut();
0506:                    return;
0507:                }
0508:
0509:                if (aNode instanceof  IfThenElseStatement) {
0510:                    // binary expressions start with ( and end with )
0511:                    // it should be delete in if ((binaryExpression))
0512:                    setToOut("if (");
0513:                    eh.addSuperConditionString(((IfThenElseStatement) aNode)
0514:                            .getCondition());
0515:                    addToOut(")");
0516:                    insertBlockStatement(((IfThenElseStatement) aNode)
0517:                            .getThenStatement());
0518:
0519:                    // nested if-then-else
0520:                    printNestedIfThenElse(((IfThenElseStatement) aNode)
0521:                            .getElseStatement());
0522:                    printOut();
0523:                    return;
0524:                }
0525:                if (aNode instanceof  IfThenStatement) {
0526:                    // binary expressions start with ( and end with )
0527:                    // it should be delete in if ((binaryExpression))
0528:                    setToOut("if (");
0529:                    eh.addSuperConditionString(((IfThenStatement) aNode)
0530:                            .getCondition());
0531:                    addToOut(")");
0532:                    // check if blockstatement otherwise include { }
0533:                    insertBlockStatement(((IfThenStatement) aNode)
0534:                            .getThenStatement());
0535:
0536:                    printOut();
0537:                    return;
0538:                }
0539:                // synchronized(var)
0540:                if (aNode instanceof  SynchronizedStatement) {
0541:                    // binary expressions start with ( and end with )
0542:                    // it should be delete in if ((binaryExpression))
0543:                    setToOut("synchronized (");
0544:                    eh.addSuperConditionString(((SynchronizedStatement) aNode)
0545:                            .getLock());
0546:                    addToOut(")");
0547:                    //            printOut();
0548:                    //            jco.increaseIndent();
0549:                    parseObject(((SynchronizedStatement) aNode).getBody());
0550:                    //            jco.decreaseIndent();
0551:                    printOut();
0552:                    return;
0553:                }
0554:                // loops
0555:                // while
0556:                if (aNode instanceof  WhileStatement) {
0557:                    WhileStatement ws = (WhileStatement) aNode;
0558:                    setToOut("while (");
0559:                    eh.addSuperConditionString(ws.getCondition());
0560:                    addToOut(")");
0561:                    // empty while only
0562:                    if (ws.getBody() instanceof  EmptyStatement
0563:                            || ws.getBody() instanceof  BlockStatement
0564:                            && ((BlockStatement) ws.getBody()).getStatements()
0565:                                    .size() < 1) {
0566:                        printOut(";");
0567:                    } else {
0568:                        insertBlockStatement(ws.getBody());
0569:                        printOut();
0570:                    }
0571:                    return;
0572:                }
0573:                // do
0574:                if (aNode instanceof  DoStatement) {
0575:                    setToOut("do");
0576:                    //            jco.increaseIndent();
0577:                    parseObject(((DoStatement) aNode).getBody());
0578:                    //            jco.decreaseIndent();
0579:                    addToOut("while (");
0580:                    eh.addSuperConditionString(((DoStatement) aNode)
0581:                            .getCondition());
0582:                    addToOut(");");
0583:                    printOut();
0584:                    return;
0585:                }
0586:                // for(i=0;i<j;i++)
0587:                if (aNode instanceof  ForStatement) {
0588:                    ForStatement fs = (ForStatement) aNode;
0589:                    setToOut("for (");
0590:
0591:                    // i=0
0592:                    addVariableDeclarationListString(fs.getInitialization());
0593:                    addToOut(";");
0594:                    if (fs.getCondition() != null) {
0595:                        addToOut(" ");
0596:                    }
0597:                    // i<j
0598:                    eh.addSuperConditionString(fs.getCondition());
0599:                    addToOut(";");
0600:                    // i++
0601:                    eh.addConditionListString(fs.getUpdate(), false);
0602:                    addToOut(")");
0603:                    // empty loop?
0604:                    if (fs.getBody() instanceof  EmptyStatement
0605:                            || fs.getBody() instanceof  BlockStatement
0606:                            && ((BlockStatement) fs.getBody()).getStatements()
0607:                                    .size() < 1) {
0608:                        printOut(";");
0609:                    } else {
0610:                        insertBlockStatement(fs.getBody());
0611:                        //                addToOut( " {" );
0612:                        printOut();
0613:                        // body
0614:                        //                jco.increaseIndent();
0615:                        //                parseObject( fs.getBody() );
0616:                        //                jco.decreaseIndent();
0617:                        //                printOut();
0618:                    }
0619:                    return;
0620:                }
0621:                // switch
0622:                if (aNode instanceof  SwitchStatement) {
0623:                    SwitchStatement ss = (SwitchStatement) aNode;
0624:                    setToOut("switch (");
0625:                    eh.addSuperConditionString(ss.getSelector());
0626:                    addToOut(") {");
0627:                    printOut();
0628:                    // empty while only
0629:                    it = ss.getBindings().listIterator();
0630:                    // case lines
0631:                    while (it.hasNext()) {
0632:                        parseObject((Node) it.next());
0633:                    }
0634:                    jco.setCommentEnd(aNode.getEndLine());
0635:                    jco.printComment();
0636:                    jco.setCommentStart(aNode.getEndLine());
0637:                    printOut("}");
0638:                    return;
0639:                }
0640:                // switch
0641:                if (aNode instanceof  SwitchBlock) {
0642:                    SwitchBlock sb = (SwitchBlock) aNode;
0643:                    if (sb.getExpression() != null) {
0644:                        setToOut("case ");
0645:                        eh.addSuperConditionString(sb.getExpression());
0646:                        addToOut(":");
0647:                    } else {
0648:                        setToOut("default:");
0649:                    }
0650:                    //            printOut();
0651:                    if (sb.getStatements() != null) {
0652:
0653:                        //                jco.increaseIndent();
0654:                        it = sb.getStatements().listIterator();
0655:                        // case lines
0656:                        if (it.hasNext()) {
0657:                            Node node = (Node) it.next();
0658:                            if (node instanceof  BlockStatement) {
0659:                                // printOut inside blockstatement
0660:                                parseObject(node);
0661:                                jco.increaseIndent();
0662:                                printOut();
0663:                                jco.decreaseIndent();
0664:                            } else {
0665:                                printOut();
0666:                                jco.increaseIndent();
0667:                                parseObject(node);
0668:                                jco.decreaseIndent();
0669:                            }
0670:                        }
0671:                        // case lines
0672:                        while (it.hasNext()) {
0673:                            Node node = (Node) it.next();
0674:                            jco.increaseIndent();
0675:                            parseObject(node);
0676:                            jco.decreaseIndent();
0677:                            if (node instanceof  BlockStatement) {
0678:                                // printOut inside blockstatement
0679:                                jco.increaseIndent();
0680:                                printOut();
0681:                                jco.decreaseIndent();
0682:                            }
0683:                        }
0684:                        // following comments
0685:                        jco.setCommentEnd(aNode.getEndLine());
0686:                        jco.printComment();
0687:                        jco.setCommentStart(aNode.getEndLine());
0688:
0689:                        //                jco.decreaseIndent();
0690:                    } else {
0691:                        // no statements
0692:                        printOut();
0693:                    }
0694:                    return;
0695:                }
0696:
0697:                // try catch finally
0698:                if (aNode instanceof  TryStatement) {
0699:                    TryStatement tryS = (TryStatement) aNode;
0700:                    // try
0701:                    setToOut("try");
0702:                    //            jco.increaseIndent();
0703:                    parseObject(tryS.getTryBlock());
0704:                    //            jco.decreaseIndent();
0705:                    //            setToOut( "} " );
0706:                    // catch
0707:                    it = tryS.getCatchStatements().listIterator();
0708:                    while (it.hasNext()) {
0709:                        parseObject((Node) it.next());
0710:                    }
0711:
0712:                    // finally
0713:                    if (tryS.getFinallyBlock() != null) {
0714:                        addToOut("finally");
0715:                        //                jco.increaseIndent();
0716:                        parseObject(((TryStatement) aNode).getFinallyBlock());
0717:
0718:                        //                jco.decreaseIndent();
0719:                        printOut();
0720:
0721:                    } else {
0722:                        printOut();
0723:                    }
0724:                    return;
0725:                }
0726:                if (aNode instanceof  CatchStatement) {
0727:                    // catch on same line as last closed bracket
0728:                    // JCC 7.9
0729:                    addToOut("catch (");
0730:                    // exception as FormalParameter
0731:                    FormalParameter fp = (FormalParameter) ((CatchStatement) aNode)
0732:                            .getException();
0733:                    addToOut(eh.getTypeString(fp.getType()) + " "
0734:                            + fp.getName() + ")");
0735:                    //            printOut();
0736:
0737:                    //            jco.increaseIndent();
0738:                    parseObject(((CatchStatement) aNode).getBlock());
0739:                    //            jco.decreaseIndent();
0740:                    //            setToOut( "} " );
0741:                    return;
0742:                }
0743:                // throw
0744:                if (aNode instanceof  ThrowStatement) {
0745:                    setToOut("throw ");
0746:                    eh.addSuperConditionString(((ThrowStatement) aNode)
0747:                            .getExpression());
0748:                    printOut(";");
0749:                    return;
0750:                }
0751:
0752:                // reserved words
0753:                // continue
0754:                if (aNode instanceof  ContinueStatement) {
0755:                    printOut("continue;");
0756:                    return;
0757:                }
0758:                // break
0759:                if (aNode instanceof  BreakStatement) {
0760:                    printOut("break;");
0761:                    return;
0762:                }
0763:                //
0764:
0765:                // expression only in other method
0766:                if (aNode instanceof  Expression) {
0767:                    setToOut("");
0768:                    eh.addSuperConditionString((Expression) aNode);
0769:                    addToOut(";");
0770:                    printOut();
0771:                    return;
0772:                }
0773:                //
0774:                printErr(aNode, "parseObject Node " + aNode
0775:                        + " not found on line " + aNode.getBeginLine());
0776:                return;
0777:            }
0778:
0779:            /**
0780:             *
0781:             */
0782:            private void insertBlockStatement(Node node) {
0783:                //        System.err.println("insertBlockStatement");
0784:                if (node instanceof  BlockStatement) {
0785:                    //            System.err.println("insertBlockStatement is block");
0786:                    parseObject(node);
0787:                } else {
0788:                    printOut(" {");
0789:                    jco.increaseIndent();
0790:                    parseObject(node);
0791:                    jco.decreaseIndent();
0792:                    setToOut("} ");
0793:                }
0794:            }
0795:
0796:            /**
0797:             * Prints nested if-then-else.
0798:             */
0799:            private void printNestedIfThenElse(Node aNode) {
0800:                // nested if-then-else
0801:                if (aNode instanceof  IfThenElseStatement) {
0802:                    addToOut("else if (");
0803:                    eh.addSuperConditionString(((IfThenElseStatement) aNode)
0804:                            .getCondition());
0805:                    addToOut(")");
0806:                    //            jco.increaseIndent();
0807:                    insertBlockStatement(((IfThenElseStatement) aNode)
0808:                            .getThenStatement());
0809:                    //            jco.decreaseIndent();
0810:                    if (((IfThenElseStatement) aNode).getElseStatement() instanceof  IfThenElseStatement) {
0811:                        printNestedIfThenElse(((IfThenElseStatement) aNode)
0812:                                .getElseStatement());
0813:                    } else {
0814:                        // the else is an if-then-else
0815:                        addToOut("else");
0816:                        //                jco.increaseIndent();
0817:                        insertBlockStatement(((IfThenElseStatement) aNode)
0818:                                .getElseStatement());
0819:                        //                jco.decreaseIndent();
0820:                    }
0821:                } else {
0822:                    // aNode is not an if-then-else
0823:                    addToOut("else");
0824:                    //            jco.increaseIndent();
0825:                    insertBlockStatement(aNode);
0826:                    //            jco.decreaseIndent();
0827:                }
0828:            }
0829:
0830:            /**
0831:             * Help method to encapsulate a often used loop in parseObject(declarations).
0832:             *
0833:             * @return A String consist of several parameters in a List separated by
0834:             * ',MiddleSplitLevel ' with a LowSplitLevel if more than one element in it
0835:             *
0836:             */
0837:            private String getParametersString(List someParameters) {
0838:                boolean findSome = false;
0839:                String ret = "";
0840:                if (someParameters != null) {
0841:                    //		  if (someParameters.size()>1) {
0842:                    ret += jco.getLowSplitLevel();
0843:                    //			}
0844:                    ListIterator it = someParameters.listIterator();
0845:                    while (it.hasNext()) {
0846:                        findSome = true;
0847:                        ret += " ";
0848:                        FormalParameter param = (FormalParameter) it.next();
0849:                        if (param.isFinal()) {
0850:                            ret += "final ";
0851:                        }
0852:                        ret += eh.getTypeString(param.getType()) + " "
0853:                                + param.getName() + ","
0854:                                + jco.getMiddleSplitLevel();
0855:                    }
0856:                    if (ret.endsWith("," + jco.getMiddleSplitLevel())) {
0857:                        ret = ret.substring(0, ret.length()
0858:                                - ("," + jco.getMiddleSplitLevel()).length());
0859:                    }
0860:                }
0861:                if (findSome) {
0862:                    ret += " ";
0863:                }
0864:                return ret;
0865:            }
0866:
0867:            /**
0868:             * Help method to encapsulate a used loop in parseObject(ForStatement).
0869:             *
0870:             * @return A String consist of several parameters in a List separated by
0871:             * ',MiddleSplitLevel ' with a LowSplitLevel if more than one element in it
0872:             *
0873:             */
0874:            private void addVariableDeclarationListString(List someParameters) {
0875:                Node node;
0876:                if (someParameters != null) {
0877:                    //		  if (someParameters.size()>1) {
0878:                    addToOut(jco.getLowSplitLevel());
0879:                    //			}
0880:                    ListIterator it = someParameters.listIterator();
0881:                    if (it.hasNext()) {
0882:                        // VariableDeclarations or SimpleAssignmentExpression
0883:                        node = (Node) it.next();
0884:                        if (node instanceof  VariableDeclaration) {
0885:                            VariableDeclaration vd = (VariableDeclaration) node;
0886:                            if (vd.isFinal()) {
0887:                                addToOut("final ");
0888:                            }
0889:                            // type
0890:                            addToOut(eh.getTypeString(vd.getType()) + " ");
0891:                            // name
0892:                            addToOut(vd.getName());
0893:                            // initializer
0894:                            if (vd.getInitializer() != null) {
0895:                                addToOut(" = " + jco.getHighSplitLevel());
0896:                                eh.addSuperConditionString(vd.getInitializer());
0897:                            }
0898:                        } else {
0899:                            eh.addSuperConditionString((Expression) node);
0900:                        }
0901:                        addToOut("," + jco.getMiddleSplitLevel() + " ");
0902:                    }
0903:                    while (it.hasNext()) {
0904:                        // VariableDeclarations or SimpleAssignmentExpression
0905:                        node = (Node) it.next();
0906:                        if (node instanceof  VariableDeclaration) {
0907:                            VariableDeclaration vd = (VariableDeclaration) node;
0908:                            // name
0909:                            addToOut(vd.getName());
0910:                            // initializer
0911:                            if (vd.getInitializer() != null) {
0912:                                addToOut(" = " + jco.getHighSplitLevel());
0913:                                eh.addSuperConditionString(vd.getInitializer());
0914:                            }
0915:                        } else {
0916:                            eh.addSuperConditionString((Expression) node);
0917:                        }
0918:                        addToOut("," + jco.getMiddleSplitLevel() + " ");
0919:                    }
0920:                    if (getOut()
0921:                            .endsWith("," + jco.getMiddleSplitLevel() + " ")) {
0922:                        setToOut(getOut()
0923:                                .substring(
0924:                                        0,
0925:                                        getOut().length()
0926:                                                - (","
0927:                                                        + jco
0928:                                                                .getMiddleSplitLevel() + " ")
0929:                                                        .length()));
0930:                    }
0931:                }
0932:            }
0933:
0934:            //
0935:            // out line management
0936:            //
0937:
0938:            /**
0939:             * Adds the specified string to the global variable LineOut
0940:             * @param The String to add.
0941:             */
0942:            public void addToOut(String add) {
0943:                outLine += add;
0944:            }
0945:
0946:            public void setToOut(String string) {
0947:                outLine = string;
0948:            }
0949:
0950:            public void printOut() {
0951:                jco.println(outLine);
0952:                setToOut("");
0953:            }
0954:
0955:            /**
0956:             * Adds the specified String and invoke the println method in the Outputclass.
0957:             * Set the lineOut class member to ""
0958:             *
0959:             * @param The String to add.
0960:             */
0961:            public void printOut(String add) {
0962:                addToOut(add);
0963:                printOut();
0964:            }
0965:
0966:            public String getOut() {
0967:                return outLine;
0968:            }
0969:
0970:            /**
0971:             * This method logs warnings on code violations.
0972:             * @param aLine
0973:             *
0974:             */
0975:            private void printLog(Node aNode, String aLine) {
0976:                //		 System.err.println(getClass()+" Log line "+aNode.getBeginLine()+": "+aLine);
0977:                ;
0978:            }
0979:
0980:            private void printLog(String aLine) {
0981:                //		 System.err.println(getClass()+" Log: "+aLine);
0982:                ;
0983:            }
0984:
0985:            /**
0986:             * This method print debug messages.
0987:             * @param aLine
0988:             *
0989:             */
0990:            private void printErr(Node aNode, String aLine) {
0991:                System.err.println(getClass() + " ERROR: " + aLine);
0992:            }
0993:
0994:            /** */
0995:            public static void printHelp() {
0996:                System.out
0997:                        .println("usage: jAnalyzer [-o=<outputfile>|-] [-linelength=<number>] [-h|-?|-help] inputfile");
0998:            }
0999:
1000:            /**
1001:             *
1002:             *
1003:             */
1004:            public static void main(String[] argv) {
1005:                String fileIn = null;
1006:                String fileOut = null;
1007:                String lineLength = null;
1008:
1009:                int i = 0;
1010:                if (argv.length > 0) {
1011:                    for (i = 0; i < argv.length; i++) {
1012:                        if (argv[i].startsWith("-o=")) {
1013:                            fileOut = argv[i].substring("-o=".length());
1014:                            continue;
1015:                        }
1016:                        if (argv[i].startsWith("-linelength=")) {
1017:                            lineLength = argv[i].substring("-linelength="
1018:                                    .length());
1019:                            continue;
1020:                        }
1021:                        if (argv[i].equals("-h") || argv[i].equals("-?")
1022:                                || argv[i].equals("-help")) {
1023:                            printHelp();
1024:                            System.exit(0);
1025:                        }
1026:                        // no parameter, must be inputfile
1027:                        fileIn = argv[i];
1028:                        break;
1029:                    }
1030:                }
1031:                if (fileIn == null) {
1032:                    printHelp();
1033:                    System.exit(0);
1034:                }
1035:                if (fileOut != null && fileOut.equals("-")) {
1036:                    new JavaCodeAnalyzer(fileIn, fileIn, lineLength);
1037:                } else {
1038:                    new JavaCodeAnalyzer(fileIn, fileOut, lineLength);
1039:                }
1040:
1041:                // more than one inputfile
1042:                if (fileOut != null && fileOut.equals("-")) {
1043:                    while (i < argv.length) {
1044:                        new JavaCodeAnalyzer(argv[i], argv[i], lineLength);
1045:                        i++;
1046:                    }
1047:                } else {
1048:                    while (i < argv.length) {
1049:                        new JavaCodeAnalyzer(argv[i], fileOut, lineLength);
1050:                        i++;
1051:                    }
1052:                }
1053:            }
1054:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.