Skip to content

Commit

Permalink
Issue checkstyle#12409: Inconsistentency In Allowed Abbreviations
Browse files Browse the repository at this point in the history
  • Loading branch information
arinmodi committed Dec 19, 2022
1 parent 8382d88 commit 24fd768
Show file tree
Hide file tree
Showing 14 changed files with 607 additions and 65 deletions.
187 changes: 142 additions & 45 deletions config/archunit-store/slices_should_be_free_of_cycles_suppressions

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion config/suppressions.xml
Expand Up @@ -105,7 +105,6 @@
<suppress id="noUsageOfGetFileContentsMethod" files="ImportControlCheck.java"/>
<suppress id="noUsageOfGetFileContentsMethod" files="UnusedImportsCheck.java"/>
<suppress id="noUsageOfGetFileContentsMethod" files="JavadocMethodCheck.java"/>
<suppress id="noUsageOfGetFileContentsMethod" files="JavadocStyleCheck.java"/>
<suppress id="noUsageOfGetFileContentsMethod" files="JavadocTypeCheck.java"/>
<suppress id="noUsageOfGetFileContentsMethod" files="JavadocVariableCheck.java"/>
<suppress id="noUsageOfGetFileContentsMethod" files="MissingJavadocMethodCheck.java"/>
Expand Down
Expand Up @@ -29,13 +29,14 @@
import com.puppycrawl.tools.checkstyle.JavadocDetailNodeParser;
import com.puppycrawl.tools.checkstyle.StatelessCheck;
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.Comment;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.FileContents;
import com.puppycrawl.tools.checkstyle.api.Scope;
import com.puppycrawl.tools.checkstyle.api.TextBlock;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
import com.puppycrawl.tools.checkstyle.utils.CheckUtil;
import com.puppycrawl.tools.checkstyle.utils.CommonUtil;
import com.puppycrawl.tools.checkstyle.utils.JavadocUtil;
import com.puppycrawl.tools.checkstyle.utils.ScopeUtil;

/**
Expand Down Expand Up @@ -147,7 +148,9 @@
* <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#RECORD_DEF">
* RECORD_DEF</a>,
* <a href="https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#COMPACT_CTOR_DEF">
* COMPACT_CTOR_DEF</a>.
* COMPACT_CTOR_DEF</a>,
* <a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#COMMENT_CONTENT">
* COMMENT_CONTENT</a>.
* </li>
* </ul>
* <p>
Expand Down Expand Up @@ -367,6 +370,10 @@ public class JavadocStyleCheck
*/
private boolean checkEmptyJavadoc;

private TextBlock packageTextBlock = null;

private boolean isFirstRun = true;

@Override
public int[] getDefaultTokens() {
return getAcceptableTokens();
Expand All @@ -387,28 +394,195 @@ public int[] getAcceptableTokens() {
TokenTypes.VARIABLE_DEF,
TokenTypes.RECORD_DEF,
TokenTypes.COMPACT_CTOR_DEF,
TokenTypes.COMMENT_CONTENT
};
}

private boolean isNewToken(DetailAST ast) {
int[] tokens = getAcceptableTokens();

for (int token : tokens) {
if (token == ast.getType()) {
return true;
}
}

return ast.getType() == TokenTypes.OBJBLOCK;
}

@Override
public boolean isCommentNodesRequired() {
return true;
}

@Override
public int[] getRequiredTokens() {
return CommonUtil.EMPTY_INT_ARRAY;
}

// suppress deprecation until https://github.com/checkstyle/checkstyle/issues/11166
@SuppressWarnings("deprecation")
@Override
public void visitToken(DetailAST ast) {
if (isFirstRun) {
isFirstRun = false;
getJavaDocForPackage(ast);
}

if (shouldCheck(ast)) {
final FileContents contents = getFileContents();
// Need to start searching for the comment before the annotations
// that may exist. Even if annotations are not defined on the
// package, the ANNOTATIONS AST is defined.
final TextBlock textBlock =
contents.getJavadocBefore(ast.getFirstChild().getLineNo());
TextBlock newTextBlock;

if (ast.getType() != TokenTypes.PACKAGE_DEF || packageTextBlock == null) {
newTextBlock = getJavaDoc(ast);
} else {
newTextBlock = packageTextBlock;
}

checkComment(ast, newTextBlock);
}
}

/**
* Get The Javadoc For Package Token.
*
* @param ast as Package Token
* @return TextBlock of Javadoc comment
*/
private void getJavaDocForPackage(DetailAST ast) {
DetailAST temp = ast;
String comment = null;
int startColNo = 0, endLineNo = 0, endColNo = 0, startLine = 0;

while (temp != null) {
DetailAST toVisit = temp.getFirstChild();

if (temp.getType() == TokenTypes.BLOCK_COMMENT_BEGIN
&& JavadocUtil.isJavadocComment(temp)) {
comment = JavadocUtil.getJavadocCommentContent(temp);
startColNo = temp.getColumnNo();
endLineNo = temp.getFirstChild().getNextSibling().getLineNo();
endColNo = temp.getFirstChild().getNextSibling().getColumnNo();
startLine = temp.getLineNo();
}

if (temp.getType() == TokenTypes.PACKAGE_DEF) break;

while (toVisit == null && temp != null) {
toVisit = temp.getNextSibling();
temp = temp.getParent();
}

temp = toVisit;
}

packageTextBlock = generateTextBlock(comment, startColNo, endLineNo, endColNo, startLine);
}

/**
* Generate The TextBlock From Given Comment.
*
* @param comment as DetailAst of The Found JavaDoc Comment
* @return TextBlock of Javadoc comment
*/
public TextBlock generateTextBlock(String comment, int startColNo, int endLineNo,
int endColNo, int startLine) {
final TextBlock textBlock;

if (comment == null) {
textBlock = null;
}
else {

final String[] comments = comment.split("\n");
comments[0] = "/**" + comments[0];
endColNo++;

int tempEndLine = startLine + (comments.length - 1);

if (tempEndLine == endLineNo) {
comments[comments.length-1] += "*/";
if (comments.length == 1) {
endColNo++;
}
textBlock = new Comment(comments, startColNo, endLineNo, endColNo);
}
else {
String[] modifiedComments = new String[comments.length + 1];
System.arraycopy(comments, 0, modifiedComments, 0, comments.length);
modifiedComments[comments.length] = "*/";
textBlock = new Comment(modifiedComments, startColNo, endLineNo, endColNo);
}
}

return textBlock;
}

/**
* Find The Javadoc comments Associated With given ast.
*
* @param ast a given node
* @return TextBlock of Javadoc comment
*/
public TextBlock getJavaDoc(DetailAST ast) {
DetailAST temp = ast;
String comment = "";
int startColNo = 0, endLineNo = 0, endColNo = 0, startLine = 0;
String key = ast.getType() + "-" + ast.getLineNo() + "-" + ast.getColumnNo();

while (temp != null) {
DetailAST toVisit = temp.getFirstChild();

if (isNewToken(temp)) {
String tempKey = temp.getType() + "-" + temp.getLineNo() + "-" + temp.getColumnNo();
if (!tempKey.equals(key)) {
toVisit = null;
}
}

if (toVisit == null) {
String parentKey = temp.getParent().getType() + "-" +
temp.getParent().getLineNo() + "-" + temp.getParent().getColumnNo();

if (parentKey.equals(key) && temp.getNextSibling() == null) {
break;
}
}

if (temp.getType() == TokenTypes.BLOCK_COMMENT_BEGIN) {
int end = temp.getFirstChild().getNextSibling().getLineNo();
if (JavadocUtil.isJavadocComment(temp) && end < ast.getLineNo()) {
comment = JavadocUtil.getJavadocCommentContent(temp);
startColNo = temp.getColumnNo();
endLineNo = end;
endColNo = temp.getFirstChild().getNextSibling().getColumnNo();
startLine = temp.getLineNo();
}
else if(!JavadocUtil.isJavadocComment(temp) && end < ast.getLineNo()) {
comment = "";
startColNo = 0;
endLineNo = 0;
endColNo = 0;
startLine = 0;
}
}

while(toVisit == null && temp != null) {
toVisit = temp.getNextSibling();
temp = temp.getParent();
}

temp = toVisit;

checkComment(ast, textBlock);
}

TextBlock textBlock = null;

if (!comment.isEmpty()) {
textBlock = generateTextBlock(comment, startColNo, endLineNo, endColNo, startLine);
}

return textBlock;
}

/**
Expand All @@ -420,19 +594,20 @@ public void visitToken(DetailAST ast) {
private boolean shouldCheck(final DetailAST ast) {
boolean check = false;

if (ast.getType() == TokenTypes.PACKAGE_DEF) {
check = CheckUtil.isPackageInfo(getFilePath());
}
else if (!ScopeUtil.isInCodeBlock(ast)) {
final Scope customScope = ScopeUtil.getScope(ast);
final Scope surroundingScope = ScopeUtil.getSurroundingScope(ast);
if (ast.getType() != TokenTypes.COMMENT_CONTENT) {
if (ast.getType() == TokenTypes.PACKAGE_DEF) {
check = CheckUtil.isPackageInfo(getFilePath());
} else if (!ScopeUtil.isInCodeBlock(ast)) {
final Scope customScope = ScopeUtil.getScope(ast);
final Scope surroundingScope = ScopeUtil.getSurroundingScope(ast);

check = customScope.isIn(scope)
&& (surroundingScope == null || surroundingScope.isIn(scope))
&& (excludeScope == null
check = customScope.isIn(scope)
&& (surroundingScope == null || surroundingScope.isIn(scope))
&& (excludeScope == null
|| !customScope.isIn(excludeScope)
|| surroundingScope != null
&& !surroundingScope.isIn(excludeScope));
&& !surroundingScope.isIn(excludeScope));
}
}
return check;
}
Expand Down
Expand Up @@ -76,7 +76,7 @@
<property default-value="true" name="checkHtml" type="boolean">
<description>Control whether to check for incomplete HTML tags.</description>
</property>
<property default-value="ANNOTATION_DEF,ANNOTATION_FIELD_DEF,CLASS_DEF,CTOR_DEF,ENUM_CONSTANT_DEF,ENUM_DEF,INTERFACE_DEF,METHOD_DEF,PACKAGE_DEF,VARIABLE_DEF,RECORD_DEF,COMPACT_CTOR_DEF"
<property default-value="ANNOTATION_DEF,ANNOTATION_FIELD_DEF,CLASS_DEF,CTOR_DEF,ENUM_CONSTANT_DEF,ENUM_DEF,INTERFACE_DEF,METHOD_DEF,PACKAGE_DEF,VARIABLE_DEF,RECORD_DEF,COMPACT_CTOR_DEF,COMMENT_CONTENT"
name="tokens"
type="java.lang.String[]"
validation-type="tokenSet">
Expand Down
Expand Up @@ -61,6 +61,7 @@ public void testGetAcceptableTokens() {
TokenTypes.VARIABLE_DEF,
TokenTypes.RECORD_DEF,
TokenTypes.COMPACT_CTOR_DEF,
TokenTypes.COMMENT_CONTENT
};

assertWithMessage("Default acceptable tokens are invalid")
Expand Down Expand Up @@ -590,4 +591,70 @@ public void testLowerCasePropertyForTag() throws Exception {
verifyWithInlineConfigParser(
getPath("InputJavadocStyleCheckOptionLowercaseProperty.java"), expected);
}

@Test
public void packageInfoAnnotation2() throws Exception {
final String[] expected = {
"17:1: " + getCheckMessage(MSG_JAVADOC_MISSING),
};

verifyWithInlineConfigParser(
getPath("pkginfo" + File.separator + "annotation2" + File.separator
+ "package-info.java"),
expected);
}

@Test
public void packageInvalidFormat2() throws Exception {
final String[] expected = {
"17:1: " + getCheckMessage(MSG_JAVADOC_MISSING),
};

verifyWithInlineConfigParser(
getPath("pkginfo" + File.separator + "invalidformat2" + File.separator
+ "package-info.java"),
expected);
}

@Test
public void packageInvalidFormat3() throws Exception {
final String[] expected = {
"17:1: " + getCheckMessage(MSG_JAVADOC_MISSING),
};

verifyWithInlineConfigParser(
getPath("pkginfo" + File.separator + "invalidformat3" + File.separator
+ "package-info.java"),
expected);
}

@Test
public void packageInvalidFormat4() throws Exception {
final String[] expected = {
"17:1: " + getCheckMessage(MSG_JAVADOC_MISSING),
};

verifyWithInlineConfigParser(
getPath("pkginfo" + File.separator + "invalidformat4" + File.separator
+ "package-info.java"),
expected);
}

@Test
public void testDefaultSettingFive() throws Exception {
final String[] expected = {
"21: " + getCheckMessage(MSG_NO_PERIOD),
"33: " + getCheckMessage(MSG_NO_PERIOD),
"37: " + getCheckMessage(MSG_NO_PERIOD),
"51: " + getCheckMessage(MSG_NO_PERIOD),
"56: " + getCheckMessage(MSG_NO_PERIOD),
"67: " + getCheckMessage(MSG_NO_PERIOD),
"71: " + getCheckMessage(MSG_NO_PERIOD),
"81: " + getCheckMessage(MSG_NO_PERIOD),
"100:3: " + getCheckMessage(MSG_EXTRA_HTML, "</body>"),
};

verifyWithInlineConfigParser(
getPath("InputJavadocStyleDefaultSettingsFive.java"), expected);
}
}

0 comments on commit 24fd768

Please sign in to comment.