diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/DetailNodeTreeStringPrinter.java b/src/main/java/com/puppycrawl/tools/checkstyle/DetailNodeTreeStringPrinter.java index b1bc7d9b251..93df3f8b2bc 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/DetailNodeTreeStringPrinter.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/DetailNodeTreeStringPrinter.java @@ -30,7 +30,7 @@ import com.puppycrawl.tools.checkstyle.api.FileText; import com.puppycrawl.tools.checkstyle.api.JavadocTokenTypes; import com.puppycrawl.tools.checkstyle.api.LocalizedMessage; -import com.puppycrawl.tools.checkstyle.api.TokenTypes; +import com.puppycrawl.tools.checkstyle.utils.CommonUtils; import com.puppycrawl.tools.checkstyle.utils.JavadocUtils; /** @@ -42,9 +42,6 @@ public final class DetailNodeTreeStringPrinter { /** OS specific line separator. */ private static final String LINE_SEPARATOR = System.getProperty("line.separator"); - /** Symbols with which javadoc starts. */ - private static final String JAVADOC_START = "/**"; - /** Prevent instances. */ private DetailNodeTreeStringPrinter() { // no code @@ -80,7 +77,7 @@ public static DetailNode parseJavadocAsDetailNode(DetailAST blockComment) { * @return tree */ private static DetailNode parseJavadocAsDetailNode(String javadocComment) { - final DetailAST blockComment = createFakeBlockComment(javadocComment); + final DetailAST blockComment = CommonUtils.createBlockCommentNode(javadocComment); return parseJavadocAsDetailNode(blockComment); } @@ -174,33 +171,4 @@ private static DetailNode parseFile(File file) throws IOException { return parseJavadocAsDetailNode(text.getFullText().toString()); } - /** - * Creates DetailAST block comment to pass it to the Javadoc parser. - * @param content comment content. - * @return DetailAST block comment - */ - private static DetailAST createFakeBlockComment(String content) { - final DetailAST blockCommentBegin = new DetailAST(); - blockCommentBegin.setType(TokenTypes.BLOCK_COMMENT_BEGIN); - blockCommentBegin.setText("/*"); - blockCommentBegin.setLineNo(0); - blockCommentBegin.setColumnNo(-JAVADOC_START.length()); - - final DetailAST commentContent = new DetailAST(); - commentContent.setType(TokenTypes.COMMENT_CONTENT); - commentContent.setText("*" + content); - commentContent.setLineNo(0); - // javadoc should starts at 0 column, so COMMENT_CONTENT node - // that contains javadoc identificator has -1 column - commentContent.setColumnNo(-1); - - final DetailAST blockCommentEnd = new DetailAST(); - blockCommentEnd.setType(TokenTypes.BLOCK_COMMENT_END); - blockCommentEnd.setText("*/"); - - blockCommentBegin.setFirstChild(commentContent); - commentContent.setNextSibling(blockCommentEnd); - return blockCommentBegin; - } - } diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java b/src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java index 8ad0762623f..8c73b210701 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java @@ -22,12 +22,10 @@ import java.io.File; import java.io.Reader; import java.io.StringReader; -import java.util.AbstractMap.SimpleEntry; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.Locale; -import java.util.Map.Entry; import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; @@ -681,7 +679,7 @@ private static DetailAST createCommentAstFromToken(Token token) { commentAst = createSlCommentNode(token); } else { - commentAst = createBlockCommentNode(token); + commentAst = CommonUtils.createBlockCommentNode(token); } return commentAst; } @@ -714,83 +712,6 @@ private static DetailAST createSlCommentNode(Token token) { return slComment; } - /** - * Create block comment from token. - * @param token - * Token object. - * @return DetailAST with BLOCK_COMMENT type. - */ - private static DetailAST createBlockCommentNode(Token token) { - final DetailAST blockComment = new DetailAST(); - blockComment.initialize(TokenTypes.BLOCK_COMMENT_BEGIN, "/*"); - - // column counting begins from 0 - blockComment.setColumnNo(token.getColumn() - 1); - blockComment.setLineNo(token.getLine()); - - final DetailAST blockCommentContent = new DetailAST(); - blockCommentContent.setType(TokenTypes.COMMENT_CONTENT); - - // column counting begins from 0 - // plus length of '/*' - blockCommentContent.setColumnNo(token.getColumn() - 1 + 2); - blockCommentContent.setLineNo(token.getLine()); - blockCommentContent.setText(token.getText()); - - final DetailAST blockCommentClose = new DetailAST(); - blockCommentClose.initialize(TokenTypes.BLOCK_COMMENT_END, "*/"); - - final Entry linesColumns = countLinesColumns( - token.getText(), token.getLine(), token.getColumn()); - blockCommentClose.setLineNo(linesColumns.getKey()); - blockCommentClose.setColumnNo(linesColumns.getValue()); - - blockComment.addChild(blockCommentContent); - blockComment.addChild(blockCommentClose); - return blockComment; - } - - /** - * Count lines and columns (in last line) in text. - * @param text - * String. - * @param initialLinesCnt - * initial value of lines counter. - * @param initialColumnsCnt - * initial value of columns counter. - * @return entry(pair), first element is lines counter, second - columns - * counter. - */ - private static Entry countLinesColumns( - String text, int initialLinesCnt, int initialColumnsCnt) { - int lines = initialLinesCnt; - int columns = initialColumnsCnt; - boolean foundCr = false; - for (char c : text.toCharArray()) { - if (c == '\n') { - foundCr = false; - lines++; - columns = 0; - } - else { - if (foundCr) { - foundCr = false; - lines++; - columns = 0; - } - if (c == '\r') { - foundCr = true; - } - columns++; - } - } - if (foundCr) { - lines++; - columns = 0; - } - return new SimpleEntry<>(lines, columns); - } - /** * State of AST. * Indicates whether tree contains certain nodes. diff --git a/src/main/java/com/puppycrawl/tools/checkstyle/utils/CommonUtils.java b/src/main/java/com/puppycrawl/tools/checkstyle/utils/CommonUtils.java index 588a2a105c6..14b3e03b5a1 100644 --- a/src/main/java/com/puppycrawl/tools/checkstyle/utils/CommonUtils.java +++ b/src/main/java/com/puppycrawl/tools/checkstyle/utils/CommonUtils.java @@ -30,13 +30,18 @@ import java.net.URL; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.AbstractMap; +import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.regex.PatternSyntaxException; import org.apache.commons.beanutils.ConversionException; +import antlr.Token; import com.puppycrawl.tools.checkstyle.api.CheckstyleException; +import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.api.TokenTypes; /** * Contains utility methods. @@ -61,6 +66,13 @@ public final class CommonUtils { /** Prefix for the exception when unable to find resource. */ private static final String UNABLE_TO_FIND_EXCEPTION_PREFIX = "Unable to find: "; + /** Symbols with which javadoc starts. */ + private static final String JAVADOC_START = "/**"; + /** Symbols with which multiple comment starts. */ + private static final String BLOCK_MULTIPLE_COMMENT_BEGIN = "/*"; + /** Symbols with which multiple comment ends. */ + private static final String BLOCK_MULTIPLE_COMMENT_END = "*/"; + /** Stop instances being created. **/ private CommonUtils() { @@ -100,6 +112,112 @@ public static Pattern createPattern(String pattern, int flags) { } } + /** + * Create block comment from string content. + * @param content comment content. + * @return DetailAST block comment + */ + public static DetailAST createBlockCommentNode(String content) { + final DetailAST blockCommentBegin = new DetailAST(); + blockCommentBegin.setType(TokenTypes.BLOCK_COMMENT_BEGIN); + blockCommentBegin.setText(BLOCK_MULTIPLE_COMMENT_BEGIN); + blockCommentBegin.setLineNo(0); + blockCommentBegin.setColumnNo(-JAVADOC_START.length()); + + final DetailAST commentContent = new DetailAST(); + commentContent.setType(TokenTypes.COMMENT_CONTENT); + commentContent.setText("*" + content); + commentContent.setLineNo(0); + // javadoc should starts at 0 column, so COMMENT_CONTENT node + // that contains javadoc identificator has -1 column + commentContent.setColumnNo(-1); + + final DetailAST blockCommentEnd = new DetailAST(); + blockCommentEnd.setType(TokenTypes.BLOCK_COMMENT_END); + blockCommentEnd.setText(BLOCK_MULTIPLE_COMMENT_END); + + blockCommentBegin.setFirstChild(commentContent); + commentContent.setNextSibling(blockCommentEnd); + return blockCommentBegin; + } + + /** + * Create block comment from token. + * @param token + * Token object. + * @return DetailAST with BLOCK_COMMENT type. + */ + public static DetailAST createBlockCommentNode(Token token) { + final DetailAST blockComment = new DetailAST(); + blockComment.initialize(TokenTypes.BLOCK_COMMENT_BEGIN, BLOCK_MULTIPLE_COMMENT_BEGIN); + + // column counting begins from 0 + blockComment.setColumnNo(token.getColumn() - 1); + blockComment.setLineNo(token.getLine()); + + final DetailAST blockCommentContent = new DetailAST(); + blockCommentContent.setType(TokenTypes.COMMENT_CONTENT); + + // column counting begins from 0 + // plus length of '/*' + blockCommentContent.setColumnNo(token.getColumn() - 1 + 2); + blockCommentContent.setLineNo(token.getLine()); + blockCommentContent.setText(token.getText()); + + final DetailAST blockCommentClose = new DetailAST(); + blockCommentClose.initialize(TokenTypes.BLOCK_COMMENT_END, BLOCK_MULTIPLE_COMMENT_END); + + final Map.Entry linesColumns = countLinesColumns( + token.getText(), token.getLine(), token.getColumn()); + blockCommentClose.setLineNo(linesColumns.getKey()); + blockCommentClose.setColumnNo(linesColumns.getValue()); + + blockComment.addChild(blockCommentContent); + blockComment.addChild(blockCommentClose); + return blockComment; + } + + /** + * Count lines and columns (in last line) in text. + * @param text + * String. + * @param initialLinesCnt + * initial value of lines counter. + * @param initialColumnsCnt + * initial value of columns counter. + * @return entry(pair), first element is lines counter, second - columns + * counter. + */ + private static Map.Entry countLinesColumns( + String text, int initialLinesCnt, int initialColumnsCnt) { + int lines = initialLinesCnt; + int columns = initialColumnsCnt; + boolean foundCr = false; + for (char c : text.toCharArray()) { + if (c == '\n') { + foundCr = false; + lines++; + columns = 0; + } + else { + if (foundCr) { + foundCr = false; + lines++; + columns = 0; + } + if (c == '\r') { + foundCr = true; + } + columns++; + } + } + if (foundCr) { + lines++; + columns = 0; + } + return new AbstractMap.SimpleEntry<>(lines, columns); + } + /** * Returns whether the file extension matches what we are meant to process. * diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/DetailNodeTreeStringPrinterTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/DetailNodeTreeStringPrinterTest.java index c4fa52447cc..9c50429998f 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/DetailNodeTreeStringPrinterTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/DetailNodeTreeStringPrinterTest.java @@ -34,9 +34,7 @@ import org.junit.Test; import org.powermock.reflect.Whitebox; -import com.puppycrawl.tools.checkstyle.api.DetailAST; import com.puppycrawl.tools.checkstyle.api.LocalizedMessage; -import com.puppycrawl.tools.checkstyle.api.TokenTypes; public class DetailNodeTreeStringPrinterTest extends AbstractTreeTestSupport { @@ -78,31 +76,6 @@ public void testParseFileWithError() throws Exception { } } - @Test - public void testCreationOfFakeCommentBlock() throws Exception { - final Method createFakeBlockComment = - Whitebox.getMethod(DetailNodeTreeStringPrinter.class, - "createFakeBlockComment", String.class); - - final DetailAST testCommentBlock = - (DetailAST) createFakeBlockComment.invoke(null, "test_comment"); - assertEquals("Invalid token type", - TokenTypes.BLOCK_COMMENT_BEGIN, testCommentBlock.getType()); - assertEquals("Invalid text", "/*", testCommentBlock.getText()); - assertEquals("Invalid line number", 0, testCommentBlock.getLineNo()); - - final DetailAST contentCommentBlock = testCommentBlock.getFirstChild(); - assertEquals("Invalid tiken type", - TokenTypes.COMMENT_CONTENT, contentCommentBlock.getType()); - assertEquals("Invalid text", "*test_comment", contentCommentBlock.getText()); - assertEquals("Invalid line number", 0, contentCommentBlock.getLineNo()); - assertEquals("Invalid column number", -1, contentCommentBlock.getColumnNo()); - - final DetailAST endCommentBlock = contentCommentBlock.getNextSibling(); - assertEquals("Invalid tiken type", TokenTypes.BLOCK_COMMENT_END, endCommentBlock.getType()); - assertEquals("Invalid text", "*/", endCommentBlock.getText()); - } - @Test public void testNoUnnecessaryTextinJavadocAst() throws Exception { verifyJavadocTree( diff --git a/src/test/java/com/puppycrawl/tools/checkstyle/utils/CommonUtilsTest.java b/src/test/java/com/puppycrawl/tools/checkstyle/utils/CommonUtilsTest.java index 7bf7014c57f..1a7cfae5490 100644 --- a/src/test/java/com/puppycrawl/tools/checkstyle/utils/CommonUtilsTest.java +++ b/src/test/java/com/puppycrawl/tools/checkstyle/utils/CommonUtilsTest.java @@ -45,6 +45,8 @@ import org.powermock.modules.junit4.PowerMockRunner; import com.puppycrawl.tools.checkstyle.api.CheckstyleException; +import com.puppycrawl.tools.checkstyle.api.DetailAST; +import com.puppycrawl.tools.checkstyle.api.TokenTypes; @RunWith(PowerMockRunner.class) public class CommonUtilsTest { @@ -113,6 +115,27 @@ public void testBadRegex2() { } } + @Test + public void testCreationOfFakeCommentBlock() { + final DetailAST testCommentBlock = + CommonUtils.createBlockCommentNode("test_comment"); + assertEquals("Invalid token type", + TokenTypes.BLOCK_COMMENT_BEGIN, testCommentBlock.getType()); + assertEquals("Invalid text", "/*", testCommentBlock.getText()); + assertEquals("Invalid line number", 0, testCommentBlock.getLineNo()); + + final DetailAST contentCommentBlock = testCommentBlock.getFirstChild(); + assertEquals("Invalid tiken type", + TokenTypes.COMMENT_CONTENT, contentCommentBlock.getType()); + assertEquals("Invalid text", "*test_comment", contentCommentBlock.getText()); + assertEquals("Invalid line number", 0, contentCommentBlock.getLineNo()); + assertEquals("Invalid column number", -1, contentCommentBlock.getColumnNo()); + + final DetailAST endCommentBlock = contentCommentBlock.getNextSibling(); + assertEquals("Invalid tiken type", TokenTypes.BLOCK_COMMENT_END, endCommentBlock.getType()); + assertEquals("Invalid text", "*/", endCommentBlock.getText()); + } + @Test public void testFileExtensions() { final String[] fileExtensions = {"java"};