From 7af9e82fc16545a8153b529fba91df0fb310e2e6 Mon Sep 17 00:00:00 2001 From: Oliver Burn Date: Thu, 24 Oct 2002 13:34:30 +0000 Subject: [PATCH] Refactored the comment manager class to having a file contents class. This class has the file name, lines and comments. --- .../puppycrawl/tools/checkstyle/Checker.java | 33 +++---- .../puppycrawl/tools/checkstyle/Scope.java | 19 ++-- .../tools/checkstyle/TreeWalker.java | 16 ++-- .../tools/checkstyle/api/Check.java | 43 ++++----- .../tools/checkstyle/api/FileContents.java | 96 +++++++++++++++++++ .../checkstyle/gui/ParseTreeInfoPanel.java | 26 ++--- .../puppycrawl/tools/checkstyle/java14_new.g | 1 + .../puppycrawl/tools/checkstyle/java_new.g | 13 +-- 8 files changed, 165 insertions(+), 82 deletions(-) create mode 100644 src/checkstyle/com/puppycrawl/tools/checkstyle/api/FileContents.java diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java index 7925c43ed..c591acc58 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/Checker.java @@ -24,6 +24,7 @@ import com.puppycrawl.tools.checkstyle.api.DetailAST; import com.puppycrawl.tools.checkstyle.api.LocalizedMessage; import com.puppycrawl.tools.checkstyle.api.LocalizedMessages; import com.puppycrawl.tools.checkstyle.api.Utils; +import com.puppycrawl.tools.checkstyle.api.FileContents; import org.apache.regexp.RESyntaxException; import org.xml.sax.SAXException; @@ -414,10 +415,9 @@ public class Checker try { fireFileStarted(stripped); final String[] lines = Utils.getLines(aFileName); - final CommentManager cmgr = new CommentManager(lines); - DetailAST rootAST = parse(lines, aFileName, cmgr); - // ParseTreeInfoPanel.show(rootAST); - mWalker.walk(rootAST, lines, aFileName); + final FileContents contents = new FileContents(aFileName, lines); + final DetailAST rootAST = parse(contents); + mWalker.walk(rootAST, contents); } catch (FileNotFoundException fnfe) { mMessages.add(new LocalizedMessage(0, Defn.CHECKSTYLE_BUNDLE, @@ -453,30 +453,27 @@ public class Checker /** * - * @param aLines the individual lines of the java file - * @param aFileName the filename of the file (used for error messages?) - * @param aCmgr the comment manager is informed of comments during parsing + * @param aContents contains the contents of the file * @return the root of the AST * @throws TokenStreamException if lexing failed * @throws RecognitionException if parsing failed */ - public static DetailAST parse( - final String[] aLines, String aFileName, final CommentManager aCmgr) - throws TokenStreamException, RecognitionException + public static DetailAST parse(FileContents aContents) + throws TokenStreamException, RecognitionException { DetailAST rootAST; try { // try the 1.4 grammar first, this will succeed for // all code that compiles without any warnings in JDK 1.4, // that should cover most cases - final Reader sar = new StringArrayReader(aLines); + final Reader sar = new StringArrayReader(aContents.getLines()); final Java14Lexer jl = new Java14Lexer(sar); - jl.setFilename(aFileName); - jl.setCommentManager(aCmgr); + jl.setFilename(aContents.getFilename()); + jl.setFileContents(aContents); final Java14Recognizer jr = new NEWSilentJava14Recognizer(jl); - jr.setFilename(aFileName); + jr.setFilename(aContents.getFilename()); jr.setASTNodeClass(DetailAST.class.getName()); jr.compilationUnit(); rootAST = (DetailAST) jr.getAST(); @@ -488,13 +485,13 @@ public class Checker // and not as a keyword // Arghh - the pain - duplicate code! - final Reader sar = new StringArrayReader(aLines); + final Reader sar = new StringArrayReader(aContents.getLines()); final JavaLexer jl = new JavaLexer(sar); - jl.setFilename(aFileName); - jl.setCommentManager(aCmgr); + jl.setFilename(aContents.getFilename()); + jl.setFileContents(aContents); final JavaRecognizer jr = new JavaRecognizer(jl); - jr.setFilename(aFileName); + jr.setFilename(aContents.getFilename()); jr.setASTNodeClass(DetailAST.class.getName()); jr.compilationUnit(); rootAST = (DetailAST) jr.getAST(); diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/Scope.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/Scope.java index cf0625e9a..4a9ec3990 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/Scope.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/Scope.java @@ -61,27 +61,27 @@ public final class Scope implements Comparable, Serializable private static final String SCOPENAME_ANONINNER = "anoninner"; /** nothing scope */ - static final Scope NOTHING = + public static final Scope NOTHING = new Scope(SCOPECODE_NOTHING, SCOPENAME_NOTHING); /** public scope */ - static final Scope PUBLIC = + public static final Scope PUBLIC = new Scope(SCOPECODE_PUBLIC, SCOPENAME_PUBLIC); /** protected scope */ - static final Scope PROTECTED = + public static final Scope PROTECTED = new Scope(SCOPECODE_PROTECTED, SCOPENAME_PROTECTED); /** package scope */ - static final Scope PACKAGE = + public static final Scope PACKAGE = new Scope(SCOPECODE_PACKAGE, SCOPENAME_PACKAGE); /** private scope */ - static final Scope PRIVATE = + public static final Scope PRIVATE = new Scope(SCOPECODE_PRIVATE, SCOPENAME_PRIVATE); /** anon inner scope */ - static final Scope ANONINNER = + public static final Scope ANONINNER = new Scope(SCOPECODE_ANONINNER, SCOPENAME_ANONINNER); /** map from scope names to the respective Scope */ @@ -112,7 +112,7 @@ public final class Scope implements Comparable, Serializable /** * @return the name of this scope. */ - String getName() + public String getName() { return mName; } @@ -133,7 +133,7 @@ public final class Scope implements Comparable, Serializable * @param aScope a Scope value * @return if this is a subscope of aScope. */ - boolean isIn(Scope aScope) + public boolean isIn(Scope aScope) { return (compareTo(aScope) <= 0); } @@ -156,8 +156,9 @@ public final class Scope implements Comparable, Serializable * @param aScopeName scope name, such as "nothing", "public", etc. * @return the Scope associated with aScopeName */ - static Scope getInstance(String aScopeName) + public static Scope getInstance(String aScopeName) { + // TODO: change scope.... // canonicalize argument final String scopeName = aScopeName.trim().toLowerCase(); diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/TreeWalker.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/TreeWalker.java index bc89432a2..1c4a0d1f1 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/TreeWalker.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/TreeWalker.java @@ -22,6 +22,7 @@ import com.puppycrawl.tools.checkstyle.api.Check; import com.puppycrawl.tools.checkstyle.api.DetailAST; import com.puppycrawl.tools.checkstyle.api.LocalizedMessages; import com.puppycrawl.tools.checkstyle.api.TokenTypes; +import com.puppycrawl.tools.checkstyle.api.FileContents; import java.util.ArrayList; import java.util.HashMap; @@ -115,13 +116,12 @@ class TreeWalker /** * Initiates the walk of an AST. * @param aAST the root AST - * @param aLines the lines of the file the AST was generated from - * @param aFilename the file name of the file the AST was generated from + * @param aContents the contents of the file the AST was generated from */ - void walk(DetailAST aAST, String[] aLines, String aFilename) + void walk(DetailAST aAST, FileContents aContents) { mMessages.reset(); - notifyBegin(aLines, aFilename); + notifyBegin(aContents); aAST.setParent(null); process(aAST); notifyEnd(); @@ -129,10 +129,9 @@ class TreeWalker /** * Notify interested checks that about to begin walking a tree. - * @param aLines the lines of the file the AST was generated from - * @param aFilename the file name of the file the AST was generated from + * @param aContents the contents of the file the AST was generated from */ - private void notifyBegin(String[] aLines, String aFilename) + private void notifyBegin(FileContents aContents) { // TODO: do not track Context properly for token final Iterator it = mAllChecks.iterator(); @@ -140,8 +139,7 @@ class TreeWalker final Check check = (Check) it.next(); final HashMap treeContext = new HashMap(); check.setTreeContext(treeContext); - check.setFilename(aFilename); - check.setLines(aLines); + check.setFileContents(aContents); check.beginTree(); } } diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/api/Check.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/Check.java index 20d0d6c6a..091525b19 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/api/Check.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/Check.java @@ -31,10 +31,8 @@ public abstract class Check /** resuable constant for message formating */ private static final Object[] EMPTY_OBJECT_ARRAY = new Object[0]; - /** name to store lines under */ - private static final String LINES_ATTRIBUTE = "lines"; - /** name to store filename under */ - private static final String FILENAME_ATTRIBUTE = "filename"; + /** name to store file contents under */ + private static final String FILE_CONTENTS_ATTRIBUTE = "fILEcONTENTS"; /** the global context for the check */ private Map mGlobalContext; @@ -171,31 +169,31 @@ public abstract class Check { } - /** - * Set the lines associated with the tree. - * @param aLines the file contents - */ - public final void setLines(String[] aLines) - { - getTreeContext().put(LINES_ATTRIBUTE, aLines); - } - /** * Returns the lines associated with the tree. * @return the file contents */ public final String[] getLines() { - return (String[]) getTreeContext().get(LINES_ATTRIBUTE); + return getFileContents().getLines(); } /** - * Set the name of the file associated with the tree. - * @param aFilename the file name + * Set the file contents associated with the tree. + * @param aContents the manager */ - public final void setFilename(String aFilename) + public final void setFileContents(FileContents aContents) { - getTreeContext().put(FILENAME_ATTRIBUTE, aFilename); + getTreeContext().put(FILE_CONTENTS_ATTRIBUTE, aContents); + } + + /** + * Returns the file contents associated with the tree. + * @return the file contents + */ + public final FileContents getFileContents() + { + return (FileContents) getTreeContext().get(FILE_CONTENTS_ATTRIBUTE); } /** @return the tab width to report errors with */ @@ -213,15 +211,6 @@ public abstract class Check mTabWidth = aTabWidth; } - /** - * Returns the filename associated with the tree. - * @return the file name - */ - public final String getFilename() - { - return (String) getTreeContext().get(FILENAME_ATTRIBUTE); - } - /** * Log an error message. * diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/api/FileContents.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/FileContents.java new file mode 100644 index 000000000..ad017b072 --- /dev/null +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/FileContents.java @@ -0,0 +1,96 @@ +package com.puppycrawl.tools.checkstyle.api; + +import java.util.Map; +import java.util.HashMap; + +public class FileContents +{ + /** the file name */ + private final String mFilename; + + /** the lines */ + private final String[] mLines; + + /** map of the Javadoc comments indexed on the last line of the comment. + * The hack is it assumes that there is only one Javadoc comment per line. + */ + private final Map mJavadocComments = new HashMap(); + + /** map of the C++ comments indexed on the last line of the comment. */ + private final Map mCPlusPlusComments = new HashMap(); + + public FileContents(String aFilename, String[] aLines) + { + mFilename = aFilename; + mLines = aLines; + } + + /** + * Report the location of a C-style comment. + * @param aStartLineNo the starting line number + * @param aStartColNo the starting column number + **/ + public void reportCPPComment(int aStartLineNo, int aStartColNo) + { + final String cmt = mLines[aStartLineNo - 1].substring(aStartColNo); + mCPlusPlusComments.put(new Integer(aStartLineNo - 1), cmt); + } + + /** + * Report the location of a C-style comment. + * @param aStartLineNo the starting line number + * @param aStartColNo the starting column number + * @param aEndLineNo the ending line number + * @param aEndColNo the ending column number + **/ + public void reportCComment(int aStartLineNo, int aStartColNo, + int aEndLineNo, int aEndColNo) + { + final String[] cc = extractCComment(aStartLineNo, aStartColNo, + aEndLineNo, aEndColNo); + + // Remember if possible Javadoc comment + if (mLines[aStartLineNo - 1].indexOf("/**", aStartColNo) != -1) { + mJavadocComments.put(new Integer(aEndLineNo - 1), cc); + } + } + + /** + * Returns the specified C comment as a String array. + * @return C comment as a array + * @param aStartLineNo the starting line number + * @param aStartColNo the starting column number + * @param aEndLineNo the ending line number + * @param aEndColNo the ending column number + **/ + private String[] extractCComment(int aStartLineNo, int aStartColNo, + int aEndLineNo, int aEndColNo) + { + String[] retVal; + if (aStartLineNo == aEndLineNo) { + retVal = new String[1]; + retVal[0] = mLines[aStartLineNo - 1].substring(aStartColNo, + aEndColNo + 1); + } + else { + retVal = new String[aEndLineNo - aStartLineNo + 1]; + retVal[0] = mLines[aStartLineNo - 1].substring(aStartColNo); + for (int i = aStartLineNo; i < aEndLineNo; i++) { + retVal[i - aStartLineNo + 1] = mLines[i]; + } + retVal[retVal.length - 1] = + mLines[aEndLineNo - 1].substring(0, aEndColNo + 1); + } + return retVal; + } + + public String[] getLines() + { + return mLines; + } + + public String getFilename() + { + return mFilename; + } +} diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/gui/ParseTreeInfoPanel.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/gui/ParseTreeInfoPanel.java index 113ac9bab..8fe638256 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/gui/ParseTreeInfoPanel.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/gui/ParseTreeInfoPanel.java @@ -19,11 +19,12 @@ package com.puppycrawl.tools.checkstyle.gui; -import java.awt.BorderLayout; -import java.awt.Component; -import java.awt.event.ActionEvent; -import java.io.File; -import java.io.IOException; +import antlr.ANTLRException; +import com.puppycrawl.tools.checkstyle.Checker; +import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.api.FileContents; +import com.puppycrawl.tools.checkstyle.api.Utils; + import javax.swing.AbstractAction; import javax.swing.JButton; import javax.swing.JFileChooser; @@ -32,12 +33,11 @@ import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.SwingUtilities; import javax.swing.filechooser.FileFilter; - -import antlr.ANTLRException; -import com.puppycrawl.tools.checkstyle.Checker; -import com.puppycrawl.tools.checkstyle.CommentManager; -import com.puppycrawl.tools.checkstyle.api.DetailAST; -import com.puppycrawl.tools.checkstyle.api.Utils; +import java.awt.BorderLayout; +import java.awt.Component; +import java.awt.event.ActionEvent; +import java.io.File; +import java.io.IOException; /** * Displays information about a parse tree. @@ -110,8 +110,8 @@ public class ParseTreeInfoPanel extends JPanel throws IOException, ANTLRException { final String[] lines = Utils.getLines(aFileName); - final CommentManager cmgr = new CommentManager(lines); - return Checker.parse(lines, aFileName, cmgr); + final FileContents contents = new FileContents(aFileName, lines); + return Checker.parse(contents); } /** diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/java14_new.g b/src/checkstyle/com/puppycrawl/tools/checkstyle/java14_new.g index b787bb826..ed890025c 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/java14_new.g +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/java14_new.g @@ -20,6 +20,7 @@ header { package com.puppycrawl.tools.checkstyle; import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.api.FileContents; } diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/java_new.g b/src/checkstyle/com/puppycrawl/tools/checkstyle/java_new.g index 741711612..367e79475 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/java_new.g +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/java_new.g @@ -20,6 +20,7 @@ header { package com.puppycrawl.tools.checkstyle; import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.api.FileContents; } /** Java 1.3 Recognizer @@ -952,13 +953,13 @@ options { setColumn( getColumn() + 1 ); } - private CommentManager mCommentManager = null; + private FileContents mFileContents = null; // TODO: Check visibility of this method one parsing is done in central // utility method - public void setCommentManager(CommentManager aCommentManager) - { - mCommentManager = aCommentManager; + public void setFileContents(FileContents aContents) + { + mFileContents = aContents; } } @@ -1030,7 +1031,7 @@ WS : ( ' ' // Single-line comments SL_COMMENT - : "//" { mCommentManager.reportCPPComment(getLine(), getColumn() - 3); } + : "//" { mFileContents.reportCPPComment(getLine(), getColumn() - 3); } (~('\n'|'\r'))* ('\n'|'\r'('\n')?) {$setType(Token.SKIP); newline();} ; @@ -1061,7 +1062,7 @@ ML_COMMENT )* "*/" { - mCommentManager.reportCComment(startLine, startCol, + mFileContents.reportCComment(startLine, startCol, getLine(), getColumn() - 2); $setType(Token.SKIP); }