From 803dafc96a33bd987f84d930b9ae3ebf7a23e097 Mon Sep 17 00:00:00 2001 From: Oleg Sukhodolsky Date: Thu, 29 Jan 2004 02:41:50 +0000 Subject: [PATCH] Implemented 885993 (Wrap all comments into TextBlocks). Draft implementation for 744970 (Forbid endline comments) --- .../tools/checkstyle/api/Comment.java | 5 +- .../tools/checkstyle/api/FileContents.java | 15 ++- .../checkstyle/checks/TodoCommentCheck.java | 5 +- .../checks/TrailingCommentCheck.java | 108 ++++++++++++++++++ .../checkstyle/checks/messages.properties | 1 + .../checkstyle/InputTrailingComment.java | 18 +++ .../tools/checkstyle/checks/AllTests.java | 1 + .../checks/TrailingCommentCheckTest.java | 23 ++++ 8 files changed, 166 insertions(+), 10 deletions(-) create mode 100644 src/checkstyle/com/puppycrawl/tools/checkstyle/checks/TrailingCommentCheck.java create mode 100644 src/testinputs/com/puppycrawl/tools/checkstyle/InputTrailingComment.java create mode 100644 src/tests/com/puppycrawl/tools/checkstyle/checks/TrailingCommentCheckTest.java diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/api/Comment.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/Comment.java index fe44092c5..a78bd2636 100755 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/api/Comment.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/Comment.java @@ -45,9 +45,10 @@ class Comment implements TextBlock * @param aText the lines that make up the comment. * @param aFirstCol number of the first column of the comment. * @param aLastLine number of the last line of the comment. + * @param aLastCol number of the last column of the comment. */ public Comment(final String[] aText, final int aFirstCol, - final int aLastLine) + final int aLastLine, final int aLastCol) { mText = new String[aText.length]; for (int i = 0; i < mText.length; i++) { @@ -56,7 +57,7 @@ class Comment implements TextBlock mFirstLine = aLastLine - mText.length + 1; mLastLine = aLastLine; mFirstCol = aFirstCol; - mLastCol = mText[mText.length - 1].length() - 1; + mLastCol = aLastCol; } /** diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/api/FileContents.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/FileContents.java index df63ea5b0..ddedf5974 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/api/FileContents.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/api/FileContents.java @@ -99,8 +99,11 @@ public final class FileContents implements CommentListener **/ public void reportCppComment(int aStartLineNo, int aStartColNo) { - final String cmt = mLines[aStartLineNo - 1].substring(aStartColNo); - mCPlusPlusComments.put(new Integer(aStartLineNo), cmt); + final String line = mLines[aStartLineNo - 1]; + final String[] txt = new String[] {line.substring(aStartColNo)}; + final Comment comment = + new Comment(txt, aStartColNo, aStartLineNo, line.length() - 1); + mCPlusPlusComments.put(new Integer(aStartLineNo), comment); } /** @@ -125,23 +128,23 @@ public final class FileContents implements CommentListener { final String[] cc = extractCComment(aStartLineNo, aStartColNo, aEndLineNo, aEndColNo); + final Comment comment = new Comment(cc, aStartColNo, aEndLineNo, + aEndColNo); // save the comment final Integer key = new Integer(aStartLineNo); if (mCComments.containsKey(key)) { final List entries = (List) mCComments.get(key); - entries.add(cc); + entries.add(comment); } else { final List entries = new ArrayList(); - entries.add(cc); + entries.add(comment); mCComments.put(key, entries); } // Remember if possible Javadoc comment if (mLines[aStartLineNo - 1].indexOf("/**", aStartColNo) != -1) { - Comment comment = new Comment(cc, aStartColNo, aEndLineNo); - mJavadocComments.put(new Integer(aEndLineNo - 1), comment); } } diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/TodoCommentCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/TodoCommentCheck.java index a23665b6b..ae3cee67b 100755 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/TodoCommentCheck.java +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/TodoCommentCheck.java @@ -22,6 +22,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import com.puppycrawl.tools.checkstyle.api.TextBlock; import com.puppycrawl.tools.checkstyle.api.DetailAST; import com.puppycrawl.tools.checkstyle.api.FileContents; @@ -85,7 +86,7 @@ public class TodoCommentCheck final Map comments = aContents.getCppComments(); for (final Iterator it = comments.keySet().iterator(); it.hasNext();) { final Integer key = (Integer) it.next(); - final String cmt = (String) comments.get(key); + final String cmt = ((TextBlock) comments.get(key)).getText()[0]; if (getRegexp().match(cmt)) { log(key.intValue(), "todo.match", getFormat()); } @@ -105,7 +106,7 @@ public class TodoCommentCheck final List lineComments = (List) allComments.get(key); final Iterator lineIter = lineComments.iterator(); while (lineIter.hasNext()) { - final String[] cmt = (String[]) lineIter.next(); + final String[] cmt = ((TextBlock) lineIter.next()).getText(); for (int i = 0; i < cmt.length; i++) { if (getRegexp().match(cmt[i])) { log(key.intValue() + i, "todo.match", getFormat()); diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/TrailingCommentCheck.java b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/TrailingCommentCheck.java new file mode 100644 index 000000000..27de3ea56 --- /dev/null +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/TrailingCommentCheck.java @@ -0,0 +1,108 @@ +//////////////////////////////////////////////////////////////////////////////// +// checkstyle: Checks Java source code for adherence to a set of rules. +// Copyright (C) 2001-2004 Oliver Burn +// +// This library is free software; you can redistribute it and/or +// modify it under the terms of the GNU Lesser General Public +// License as published by the Free Software Foundation; either +// version 2.1 of the License, or (at your option) any later version. +// +// This library is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +// Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public +// License along with this library; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +//////////////////////////////////////////////////////////////////////////////// +package com.puppycrawl.tools.checkstyle.checks; + +import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.api.TextBlock; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.apache.commons.beanutils.ConversionException; +import org.apache.regexp.RE; + +/** + * The check. + * @author o_sukhodolsky + */ +public class TrailingCommentCheck extends AbstractFormatCheck +{ + /** default format for allowed blank line. */ + private static final String DEFAULT_FORMAT = "^[\\s\\}\\);]*$"; + /** + * Creates new instance of the check. + * @throws ConversionException unable to parse DEFAULT_FORMAT. + */ + public TrailingCommentCheck() throws ConversionException + { + super(DEFAULT_FORMAT); + } + + /** {@inheritDoc} */ + public int[] getDefaultTokens() + { + return new int[0]; + } + + /** {@inheritDoc} */ + public void visitToken(DetailAST aAST) + { + // do nothing + } + + /** {@inheritDoc} */ + public void beginTree(DetailAST aRootAST) + { + final RE blankLinePattern = getRegexp(); + final Map cppComments = getFileContents().getCppComments(); + final Map cComments = getFileContents().getCComments(); + final Set lines = new HashSet(); + lines.addAll(cppComments.keySet()); + lines.addAll(cComments.keySet()); + + final Iterator linesIter = lines.iterator(); + while (linesIter.hasNext()) { + final Integer lineNo = (Integer) linesIter.next(); + // I don't want handle several comments on one line :( + // Perhaps I'm wrong :) + if (cppComments.containsKey(lineNo) + && cComments.containsKey(lineNo) + || cComments.containsKey(lineNo) + && ((List) cComments.get(lineNo)).size() > 1) + { + log(lineNo.intValue(), "Too many comments."); + continue; + } + + final String line = getLines()[lineNo.intValue() - 1]; + String lineBefore = ""; + String lineAfter = ""; + if (cppComments.containsKey(lineNo)) { + final TextBlock comment = (TextBlock) cppComments.get(lineNo); + lineBefore = line.substring(0, comment.getStartColNo()); + } + else if (cComments.containsKey(lineNo)) { + final List commentList = (List) cComments.get(lineNo); + final TextBlock comment = + (TextBlock) commentList.iterator().next(); + lineBefore = line.substring(0, comment.getStartColNo()); + if (comment.getText().length == 1) { + lineAfter = line.substring(comment.getEndColNo() + 1); + } + } + lineAfter = lineAfter.trim(); + if (!blankLinePattern.match(lineBefore) || !"".equals(lineAfter)) { + log(lineNo.intValue(), "trailing.comments"); + } + } + } +} diff --git a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/messages.properties b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/messages.properties index 12718c339..1930e0e58 100644 --- a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/messages.properties +++ b/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/messages.properties @@ -25,3 +25,4 @@ descendant.token.min=Count of {0} for ''{2}'' descendant ''{3}'' is less than mi descendant.token.max=Count of {0} for ''{2}'' descendant ''{3}'' exceeds maximum count {1}. final.parameter=Parameter {0} should be final. +trailing.comments=Don''t use trailing comments. diff --git a/src/testinputs/com/puppycrawl/tools/checkstyle/InputTrailingComment.java b/src/testinputs/com/puppycrawl/tools/checkstyle/InputTrailingComment.java new file mode 100644 index 000000000..d4bccf450 --- /dev/null +++ b/src/testinputs/com/puppycrawl/tools/checkstyle/InputTrailingComment.java @@ -0,0 +1,18 @@ +public class InputTrailingComment { + int i; // don't use trailing comments :) + // it fine to have comment w/o any statement + /* good c-style comment. */ + int j; /* bad c-style comment. */ + void method1() { /* some c-style multi-line + comment*/ + Runnable r = (new Runnable() { + public void run() { + } + }); /* we should allow this */ + } // we should allow this + /* + Let's check multi-line comments. + */ + /* c-style */ // we do not allow several comments on one line + /* c-style 1 */ /*c-style 2 */ +} diff --git a/src/tests/com/puppycrawl/tools/checkstyle/checks/AllTests.java b/src/tests/com/puppycrawl/tools/checkstyle/checks/AllTests.java index b1f597a48..cbd8c961d 100644 --- a/src/tests/com/puppycrawl/tools/checkstyle/checks/AllTests.java +++ b/src/tests/com/puppycrawl/tools/checkstyle/checks/AllTests.java @@ -24,6 +24,7 @@ public class AllTests { suite.addTest(new TestSuite(NewlineAtEndOfFileCheckTest.class)); suite.addTest(new TestSuite(RedundantModifierTest.class)); suite.addTest(new TestSuite(TodoCommentCheckTest.class)); + suite.addTest(new TestSuite(TrailingCommentCheckTest.class)); suite.addTest(new TestSuite(TranslationCheckTest.class)); suite.addTest(new TestSuite(UncommentedMainCheckTest.class)); suite.addTest(new TestSuite(UpperEllCheckTest.class)); diff --git a/src/tests/com/puppycrawl/tools/checkstyle/checks/TrailingCommentCheckTest.java b/src/tests/com/puppycrawl/tools/checkstyle/checks/TrailingCommentCheckTest.java new file mode 100644 index 000000000..0b955711d --- /dev/null +++ b/src/tests/com/puppycrawl/tools/checkstyle/checks/TrailingCommentCheckTest.java @@ -0,0 +1,23 @@ +package com.puppycrawl.tools.checkstyle.checks; + +import com.puppycrawl.tools.checkstyle.BaseCheckTestCase; +import com.puppycrawl.tools.checkstyle.DefaultConfiguration; + +public class TrailingCommentCheckTest extends BaseCheckTestCase +{ + public void testDefaultTokens() + throws Exception + { + final DefaultConfiguration checkConfig = + createCheckConfig(TrailingCommentCheck.class); +// checkConfig.addAttribute("tokens", "CTOR_DEF"); + final String[] expected = { + "2: Don't use trailing comments.", + "5: Don't use trailing comments.", + "6: Don't use trailing comments.", + "16: Too many comments.", + "17: Too many comments.", + }; + verify(checkConfig, getPath("InputTrailingComment.java"), expected); + } +}