Skip to content

Commit

Permalink
Issue #4785: Method createBlockCommentNode was created in CommonUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
isturkin authored and romani committed Oct 20, 2017
1 parent 3726439 commit 9b383e0
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 141 deletions.
Expand Up @@ -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;

/**
Expand All @@ -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
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
}

}
81 changes: 1 addition & 80 deletions src/main/java/com/puppycrawl/tools/checkstyle/TreeWalker.java
Expand Up @@ -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;
Expand Down Expand Up @@ -681,7 +679,7 @@ private static DetailAST createCommentAstFromToken(Token token) {
commentAst = createSlCommentNode(token);
}
else {
commentAst = createBlockCommentNode(token);
commentAst = CommonUtils.createBlockCommentNode(token);
}
return commentAst;
}
Expand Down Expand Up @@ -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<Integer, Integer> 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<Integer, Integer> 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.
Expand Down
118 changes: 118 additions & 0 deletions src/main/java/com/puppycrawl/tools/checkstyle/utils/CommonUtils.java
Expand Up @@ -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.
Expand All @@ -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() {

Expand Down Expand Up @@ -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<Integer, Integer> 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<Integer, Integer> 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.
*
Expand Down
Expand Up @@ -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 {

Expand Down Expand Up @@ -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(
Expand Down
Expand Up @@ -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 {
Expand Down Expand Up @@ -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"};
Expand Down

0 comments on commit 9b383e0

Please sign in to comment.