Skip to content

Commit

Permalink
Issue #7721: Update AbstractChecks to log DetailAST - AnnotationOnSam…
Browse files Browse the repository at this point in the history
…eLine
  • Loading branch information
wilcoln authored and rnveach committed Mar 28, 2020
1 parent c69ea7e commit ff3ba01
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
////////////////////////////////////////////////////////////////////////////////
// checkstyle: Checks Java source code for adherence to a set of rules.
// Copyright (C) 2001-2020 the original author or authors.
//
// 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 org.checkstyle.suppressionxpathfilter;

import java.io.File;
import java.util.Arrays;
import java.util.List;

import org.junit.jupiter.api.Test;

import com.puppycrawl.tools.checkstyle.DefaultConfiguration;
import com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationOnSameLineCheck;

public class XpathRegressionAnnotationOnSameLineTest extends AbstractXpathTestSupport {

private final String checkName = AnnotationOnSameLineCheck.class.getSimpleName();

@Override
protected String getCheckName() {
return checkName;
}

@Test
public void testOne() throws Exception {
final File fileToProcess =
new File(getPath(
"SuppressionXpathRegressionAnnotationOnSameLineOne.java"));

final DefaultConfiguration moduleConfig =
createModuleConfig(AnnotationOnSameLineCheck.class);

moduleConfig.addAttribute("tokens",
"CLASS_DEF, INTERFACE_DEF, ENUM_DEF, METHOD_DEF, "
+ "CTOR_DEF, VARIABLE_DEF, PARAMETER_DEF, ANNOTATION_DEF, TYPECAST, "
+ "LITERAL_THROWS, IMPLEMENTS_CLAUSE, TYPE_ARGUMENT, LITERAL_NEW, DOT, "
+ "ANNOTATION_FIELD_DEF");

final String[] expectedViolation = {
"6:5: " + getCheckMessage(AnnotationOnSameLineCheck.class,
AnnotationOnSameLineCheck.MSG_KEY_ANNOTATION_ON_SAME_LINE,
"Deprecated"),
};

final List<String> expectedXpathQueries = Arrays.asList(
"/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionAnnotationOnSameLineOne']]"
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='getX']]",
"/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionAnnotationOnSameLineOne']]"
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='getX']]/MODIFIERS",
"/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionAnnotationOnSameLineOne']]"
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='getX']]/MODIFIERS"
+ "/ANNOTATION[./IDENT[@text='Deprecated']]",
"/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionAnnotationOnSameLineOne']]"
+ "/OBJBLOCK/METHOD_DEF[./IDENT[@text='getX']]"
+ "/MODIFIERS/ANNOTATION[./IDENT[@text='Deprecated']]/AT"
);

runVerifications(moduleConfig, fileToProcess, expectedViolation,
expectedXpathQueries);
}

@Test
public void testTwo() throws Exception {
final File fileToProcess =
new File(getPath(
"SuppressionXpathRegressionAnnotationOnSameLineTwo.java"));

final DefaultConfiguration moduleConfig =
createModuleConfig(AnnotationOnSameLineCheck.class);

final String[] expectedViolation = {
"7:5: " + getCheckMessage(AnnotationOnSameLineCheck.class,
AnnotationOnSameLineCheck.MSG_KEY_ANNOTATION_ON_SAME_LINE,
"Deprecated"),
};

final List<String> expectedXpathQueries = Arrays.asList(
"/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionAnnotationOnSameLineTwo']]"
+ "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='names']]",
"/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionAnnotationOnSameLineTwo']]"
+ "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='names']]/MODIFIERS",
"/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionAnnotationOnSameLineTwo']]"
+ "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='names']]/MODIFIERS"
+ "/ANNOTATION[./IDENT[@text='Deprecated']]",
"/CLASS_DEF[./IDENT[@text='SuppressionXpathRegressionAnnotationOnSameLineTwo']]"
+ "/OBJBLOCK/VARIABLE_DEF[./IDENT[@text='names']]/MODIFIERS"
+ "/ANNOTATION[./IDENT[@text='Deprecated']]/AT"
);

runVerifications(moduleConfig, fileToProcess, expectedViolation,
expectedXpathQueries);
}

@Test
public void testThree() throws Exception {
final File fileToProcess =
new File(getPath(
"SuppressionXpathRegressionAnnotationOnSameLineThree.java"));

final DefaultConfiguration moduleConfig =
createModuleConfig(AnnotationOnSameLineCheck.class);
moduleConfig.addAttribute("tokens", "CLASS_DEF, INTERFACE_DEF, ENUM_DEF, METHOD_DEF, "
+ "CTOR_DEF, VARIABLE_DEF, PARAMETER_DEF, ANNOTATION_DEF, TYPECAST, "
+ "LITERAL_THROWS, IMPLEMENTS_CLAUSE, TYPE_ARGUMENT, LITERAL_NEW, DOT, "
+ "ANNOTATION_FIELD_DEF");

final String[] expectedViolation = {
"3:1: " + getCheckMessage(AnnotationOnSameLineCheck.class,
AnnotationOnSameLineCheck.MSG_KEY_ANNOTATION_ON_SAME_LINE,
"Deprecated"),
};

final List<String> expectedXpathQueries = Arrays.asList(
"/INTERFACE_DEF["
+ "./IDENT[@text='SuppressionXpathRegressionAnnotationOnSameLineThree']]",
"/INTERFACE_DEF["
+ "./IDENT[@text='SuppressionXpathRegressionAnnotationOnSameLineThree']]"
+ "/MODIFIERS",
"/INTERFACE_DEF["
+ "./IDENT[@text='SuppressionXpathRegressionAnnotationOnSameLineThree']]"
+ "/MODIFIERS/ANNOTATION[./IDENT[@text='Deprecated']]",
"/INTERFACE_DEF["
+ "./IDENT[@text='SuppressionXpathRegressionAnnotationOnSameLineThree']]"
+ "/MODIFIERS/ANNOTATION[./IDENT[@text='Deprecated']]/AT"
);

runVerifications(moduleConfig, fileToProcess, expectedViolation,
expectedXpathQueries);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.checkstyle.suppressionxpathfilter.annotationonsameline;

public class SuppressionXpathRegressionAnnotationOnSameLineOne {
@Deprecated int x;

@Deprecated //warn
public int getX() {
return (int) x;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.checkstyle.suppressionxpathfilter.annotationonsameline;

@Deprecated //warn
interface SuppressionXpathRegressionAnnotationOnSameLineThree {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.checkstyle.suppressionxpathfilter.annotationonsameline;

import java.util.ArrayList;
import java.util.List;

public class SuppressionXpathRegressionAnnotationOnSameLineTwo {
@Deprecated //warn
private List<String> names = new ArrayList<>();
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public void visitToken(DetailAST ast) {
annotationNode = annotationNode.getNextSibling()) {
if (annotationNode.getType() == TokenTypes.ANNOTATION
&& !TokenUtil.areOnSameLine(annotationNode, getNextNode(annotationNode))) {
log(annotationNode.getLineNo(), MSG_KEY_ANNOTATION_ON_SAME_LINE,
log(annotationNode, MSG_KEY_ANNOTATION_ON_SAME_LINE,
getAnnotationName(annotationNode));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@
* </p>
* <ul id="SuppressionXpathFilter_IncompatibleChecks">
* <li>
* AnnotationOnSameLine
* </li>
* <li>
* AnnotationUseStyle
* </li>
* <li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ public void testGetAcceptableTokens() {
public void testCheck() throws Exception {
final DefaultConfiguration config = createModuleConfig(AnnotationOnSameLineCheck.class);
final String[] expected = {
"9: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation"),
"10: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation"),
"11: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Deprecated"),
"16: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation"),
"9:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation"),
"10:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation"),
"11:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Deprecated"),
"16:18: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation"),
};
verify(config, getPath("InputAnnotationOnSameLineCheck.java"), expected);
}
Expand All @@ -87,10 +87,10 @@ public void testCheckAcceptableTokens() throws Exception {
+ "LITERAL_THROWS, IMPLEMENTS_CLAUSE, TYPE_ARGUMENT, LITERAL_NEW, DOT, "
+ "ANNOTATION_FIELD_DEF");
final String[] expected = {
"9: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation"),
"10: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation"),
"11: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Deprecated"),
"16: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation"),
"9:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation"),
"10:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation"),
"11:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Deprecated"),
"16:18: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Annotation"),
};
verify(config, getPath("InputAnnotationOnSameLineCheck.java"), expected);
}
Expand All @@ -99,10 +99,10 @@ public void testCheckAcceptableTokens() throws Exception {
public void testCheck2() throws Exception {
final DefaultConfiguration config = createModuleConfig(AnnotationOnSameLineCheck.class);
final String[] expected = {
"11: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"16: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
"19: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
"20: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"11:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"16:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
"19:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "SuppressWarnings"),
"20:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
};
verify(config, getPath("InputAnnotationOnSameLineCheck2.java"), expected);
}
Expand All @@ -115,22 +115,22 @@ public void testCheckOnDifferentTokens() throws Exception {
+ "LITERAL_THROWS, IMPLEMENTS_CLAUSE, TYPE_ARGUMENT, LITERAL_NEW, DOT, "
+ "ANNOTATION_FIELD_DEF");
final String[] expected = {
"5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"8: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"13: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"14: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"17: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"18: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"21: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"24: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"29: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"33: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"34: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"35: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"37: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"43: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"53: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"56: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"5:1: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"8:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"13:8: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"14:72: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"17:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"18:35: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"21:18: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"24:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"29:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"33:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"34:28: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"35:33: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"37:15: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"43:17: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"53:1: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
"56:5: " + getCheckMessage(MSG_KEY_ANNOTATION_ON_SAME_LINE, "Ann"),
};
verify(config, getPath("InputAnnotationOnSameLineCheckOnDifferentTokens.java"), expected);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ public class XpathRegressionTest extends AbstractModuleTestSupport {
// till https://github.com/checkstyle/checkstyle/issues/5777
public static final Set<String> INCOMPATIBLE_CHECK_NAMES =
Collections.unmodifiableSet(new HashSet<>(Arrays.asList(
"AnnotationOnSameLine",
"AnnotationUseStyle",
"AvoidEscapedUnicodeCharacters",
"CommentsIndentation",
Expand Down
1 change: 0 additions & 1 deletion src/xdocs/config_filters.xml
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,6 @@ public class UserService {
Currently, filter does not support the following checks:
</p>
<ul id="SuppressionXpathFilter_IncompatibleChecks">
<li>AnnotationOnSameLine</li>
<li>AnnotationUseStyle</li>
<li>AvoidEscapedUnicodeCharacters</li>
<li>CommentsIndentation</li>
Expand Down

0 comments on commit ff3ba01

Please sign in to comment.