⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 javacodeanalyzer.java

📁 Java的面向对象数据库系统的源代码
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
// You can redistribute this software and/or modify it under the terms of// the Infozone Software License version 2 published by the Infozone Group// (http://www.infozone-group.org).//// Copyright (C) @year@ by The Infozone Group. All rights reserved.//// $Id: JavaCodeAnalyzer.java,v 1.1 2002/05/10 08:59:12 per_nyfelt Exp $package org.infozone.tools.janalyzer;import koala.dynamicjava.parser.wrapper.*;import koala.dynamicjava.tree.*;import java.util.List;import java.util.ListIterator;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.io.BufferedReader;import java.io.BufferedWriter;/** * This class parses a existing JAVA class and format's the source code * after the Code Conventions from Sun and therefore from the Apache Software Foundation. * It uses the Syntax tree from the DynamicJava SourceCodeParser and transform each node of * this tree back into lines of code. * All output is handled by the JavaCodeOutput class. Also comments only handled by the Output * class, because they don't exist in a syntax tree. * * * JCC in the comment stands for * <a href=http://xml.apache.org/source.html>Coding Conventions</a>. * * <pre> * * TODO: *  - forgotten expressions they not used in the prowler package *  - indentation in nested conditional operator statements *  - comments after the last expression in a method and the trailing } are deleted!!! *  - split level in binary expressions *  - label: *  - more documentation *  - quality checking such: *     - constants as ABC_DEF *  - JavaCodeMetricManager *  - metric'S *     - method counting and so on * * Problems: *  - trailling comments can't be inserted after the automatically wrapped lines, so *      they are inserted before the statements *  - At this moment only the trailling comment on the same line as the statement starts *      is checked * </pre> * * @version $Revision: 1.1 $ $Date: 2002/05/10 08:59:12 $ * @author <a href="http://www.softwarebuero.de">SMB</a> */public final class JavaCodeAnalyzer extends java.lang.Object {        //    // data    //        /** The Name of the file to prepare. */    //    private String filename;    /** The string for concating several smaller output strings to one line. */    private String outLine = "";    /** The output class */    private JavaCodeOutput jco;    /** The expression helper class */    private ExpressionHelper eh;    /** The level to help correct parentheses to binary expressions */    private int currentBinaryExpressionLevel = 0;        /** Source File is a interface Declaration, so methods doesnt have a body */    private boolean isInterface = false;            public JavaCodeAnalyzer( String filenameIn, String filenameOut, String lineLength ) {        //        filename = filenameIn;                //        try {            // output class            // copy input file            File tmp = File.createTempFile( "JavaCodeAnalyzer", "tmp" );            BufferedReader br = new BufferedReader( new FileReader( filenameIn ) );            BufferedWriter out = new BufferedWriter( new FileWriter( tmp ) );            while (br.ready()) {                out.write( br.read() );            }             br.close();            out.close();                        jco = new JavaCodeOutput( tmp, filenameOut, lineLength );            SourceCodeParser p = new JavaCCParserFactory().createParser( new FileReader( tmp ), null );            //filenameIn);            List statements = p.parseCompilationUnit();            ListIterator it = statements.listIterator();            eh = new ExpressionHelper( this, jco );            Node n;            printLog( "Parsed file " + filenameIn + "\n" );            while (it.hasNext()) {                n = (Node)it.next();                parseObject( n );            }             tmp.delete();        } catch (Exception e) {            System.err.println( getClass() + ": " + e );        }     }            /**     * This method parses each Node in the syntax tree.     * The String out is used for concatenation of the whole single strings.     *     * Errors:     *   On FieldDeclaration: only one declaration per line is in the tree.     *			JCC 6.1     *     */    protected void parseObject( Node aNode ) {        // often used        ListIterator it;        // linenumber        jco.setCommentEnd( aNode.getBeginLine() );        printLog( aNode, "parseObject" + aNode );                // package ready        if (aNode instanceof PackageDeclaration) {            jco.printPackageComment( aNode );            jco.setCommentStart( aNode.getBeginLine() );                        setToOut( "package " + jco.getHighSplitLevel() + ((PackageDeclaration)aNode).getName() + ";" );            printOut();            return;        }         // import ready        if (aNode instanceof ImportDeclaration) {            jco.printImportComment( aNode );            jco.setCommentStart( aNode.getBeginLine() );                        setToOut( "import " + jco.getHighSplitLevel() + ((ImportDeclaration)aNode).getName() );            if (((ImportDeclaration)aNode).isPackage()) {                addToOut( ".*" );                printLog( aNode, "import statement with *" );            }             printOut( ";" );            return;        }         // class ready        if (aNode instanceof ClassDeclaration) {            ClassDeclaration classNode = (ClassDeclaration)aNode;            jco.printClassComment( classNode );            jco.setCommentStart( aNode.getBeginLine() );                                    //            isInterface = false;            // access_flags            setToOut( ConstantsManager.getModifierString( classNode.getAccessFlags() ) );            // superclass            addToOut( jco.getLowSplitLevel() + "class " + classNode.getName() + " " + jco.getHighSplitLevel()                     + "extends " + classNode.getSuperclass() );            // interfaces?            List interfaces = classNode.getInterfaces();            if (interfaces != null && !interfaces.isEmpty()) {                it = interfaces.listIterator();                addToOut( " " + jco.getHighSplitLevel() + "implements " );                while (it.hasNext()) {                    addToOut( it.next() + "," + jco.getMiddleSplitLevel() + " " );                }                 if (getOut().endsWith( "," + jco.getMiddleSplitLevel() + " " )) {                    setToOut( getOut().substring( 0,                            getOut().length() - ("," + jco.getMiddleSplitLevel() + " ").length() ) );                }             }             printOut( " {" );            // class members            it = classNode.getMembers().listIterator();            jco.increaseIndent();            while (it.hasNext()) {                parseObject( (Node)it.next() );            }             jco.setCommentEnd( aNode.getEndLine() );            jco.printComment();            jco.setCommentStart( aNode.getEndLine() );                        jco.decreaseIndent();            printOut( "}" );            return;        }         // interface        if (aNode instanceof InterfaceDeclaration) {            InterfaceDeclaration interfaceNode = (InterfaceDeclaration)aNode;            jco.printInterfaceComment( interfaceNode );            jco.setCommentStart( aNode.getBeginLine() );                                    //            isInterface = true;            // access_flags            setToOut( ConstantsManager.getModifierString( interfaceNode.getAccessFlags() ) );            // superclass            addToOut( jco.getLowSplitLevel() + "interface " + interfaceNode.getName() + " " );            // interfaces?            List interfaces = interfaceNode.getInterfaces();            if (interfaces != null && !interfaces.isEmpty()) {                it = interfaces.listIterator();                addToOut( jco.getHighSplitLevel() + "extends " );                while (it.hasNext()) {                    addToOut( it.next() + "," + jco.getMiddleSplitLevel() + " " );                }                 if (getOut().endsWith( "," + jco.getMiddleSplitLevel() + " " )) {                    setToOut( getOut().substring( 0,                            getOut().length() - ("," + jco.getMiddleSplitLevel() + " ").length() ) );                }             }             printOut( " {" );            // class members            it = interfaceNode.getMembers().listIterator();            jco.increaseIndent();            while (it.hasNext()) {                parseObject( (Node)it.next() );            }             jco.setCommentEnd( aNode.getEndLine() );            jco.printComment();            jco.setCommentStart( aNode.getEndLine() );                        jco.decreaseIndent();            printOut( "}" );            return;        }         // field working        if (aNode instanceof FieldDeclaration) {            FieldDeclaration fieldNode = (FieldDeclaration)aNode;            jco.printFieldComment( fieldNode );            jco.setCommentStart( aNode.getBeginLine() );                        // access_flags            setToOut( ConstantsManager.getModifierString( fieldNode.getAccessFlags() ) );            // type            addToOut( eh.getTypeString( fieldNode.getType() ) + " " );            // name            addToOut( fieldNode.getName() );            // initializer            if (fieldNode.getInitializer() != null) {                addToOut( " = " + jco.getHighSplitLevel() );                eh.addSuperConditionString( fieldNode.getInitializer() );            }             addToOut( ";" );            printOut();            return;        }         // same as FieldDeclaration + isFinal        if (aNode instanceof VariableDeclaration) {            //	jco.setLastLineNumber(aNode.getEndLine());            VariableDeclaration varNode = (VariableDeclaration)aNode;            jco.printVariableComment( varNode );            jco.setCommentStart( aNode.getBeginLine() );                        setToOut( "" );            if (varNode.isFinal()) {                addToOut( "final " );            }             // type            addToOut( eh.getTypeString( varNode.getType() ) + " " );            // name            addToOut( varNode.getName() );            // initializer            if (varNode.getInitializer() != null) {                addToOut( " = " + jco.getHighSplitLevel() );                eh.addSuperConditionString( varNode.getInitializer() );            }             addToOut( ";" );            printOut();            return;        }                 if (aNode instanceof MethodDeclaration) {            MethodDeclaration methodNode = (MethodDeclaration)aNode;            jco.printMethodComment( methodNode );            jco.setCommentStart( aNode.getBeginLine() );                        // access_flags            setToOut( ConstantsManager.getModifierString( methodNode.getAccessFlags() ) );            // type            addToOut( eh.getTypeString( methodNode.getReturnType() ) + " " );            // name + parameterlist            addToOut( methodNode.getName() + "(" + getParametersString( methodNode.getParameters() ) + ")" );            // Exceptions            it = methodNode.getExceptions().listIterator();            if (it.hasNext()) {                addToOut( " " + jco.getHighSplitLevel() + "throws" );            }             while (it.hasNext()) {                addToOut( " " + (String)it.next() + "," + jco.getMiddleSplitLevel() );            }             if (getOut().endsWith( "," + jco.getMiddleSplitLevel() )) {                setToOut( getOut().substring( 0, getOut().length() - ("," + jco.getMiddleSplitLevel()).length() ) );            }                         // { if right method, ; if abstract method            if (ConstantsManager.getModifierString( methodNode.getAccessFlags() ).indexOf( "abstract" ) == -1                     && !isInterface) {                //                addToOut( " {" );                //                //                printOut();                // all body nodes                //                jco.increaseIndent();                parseObject( methodNode.getBody() );                //				printOut("MethodDeclaration end");                jco.setCommentEnd( aNode.getEndLine() );                jco.printComment();                jco.setCommentStart( aNode.getEndLine() );                                //                jco.decreaseIndent();                //                printOut();            } else {                // abstract                addToOut( ";" );                //                printOut();            }             return;        }         // constructor Declaration similar as MethodDeclaration

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -