Skip to content

Commit

Permalink
Issue checkstyle#6320: added REMOVE_CONDITIONALS mutator for annotation
Browse files Browse the repository at this point in the history
  • Loading branch information
rnveach committed Dec 30, 2018
1 parent e1fb0b5 commit 3a4884c
Show file tree
Hide file tree
Showing 10 changed files with 80 additions and 23 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1856,6 +1856,7 @@
<mutator>INVERT_NEGS</mutator>
<mutator>MATH</mutator>
<mutator>NEGATE_CONDITIONALS</mutator>
<mutator>REMOVE_CONDITIONALS</mutator>
<mutator>RETURN_VALS</mutator>
<mutator>TRUE_RETURNS</mutator>
<mutator>VOID_METHOD_CALLS</mutator>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,21 +264,11 @@ public int[] getRequiredTokens() {
public void visitToken(DetailAST ast) {
final DetailAST modifiersNode = ast.findFirstToken(TokenTypes.MODIFIERS);

if (hasAnnotations(modifiersNode)) {
if (modifiersNode != null) {
checkAnnotations(modifiersNode, getExpectedAnnotationIndentation(modifiersNode));
}
}

/**
* Checks whether a given modifier node has an annotation.
* @param modifierNode modifier node.
* @return true if the given modifier node has the annotation.
*/
private static boolean hasAnnotations(DetailAST modifierNode) {
return modifierNode != null
&& modifierNode.findFirstToken(TokenTypes.ANNOTATION) != null;
}

/**
* Returns an expected annotation indentation.
* The expected indentation should be the same as the indentation of the node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -464,12 +464,12 @@ private void logCommaViolation(final DetailAST ast) {
//comma can be null if array is empty
final DetailAST comma = rCurly.getPreviousSibling();

if (trailingArrayComma == TrailingArrayComma.ALWAYS
&& (comma == null || comma.getType() != TokenTypes.COMMA)) {
log(rCurly, MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING);
if (trailingArrayComma == TrailingArrayComma.ALWAYS) {
if (comma == null || comma.getType() != TokenTypes.COMMA) {
log(rCurly, MSG_KEY_ANNOTATION_TRAILING_COMMA_MISSING);
}
}
else if (trailingArrayComma == TrailingArrayComma.NEVER
&& comma != null && comma.getType() == TokenTypes.COMMA) {
else if (comma != null && comma.getType() == TokenTypes.COMMA) {
log(comma, MSG_KEY_ANNOTATION_TRAILING_COMMA_PRESENT);
}
}
Expand All @@ -485,14 +485,13 @@ private void checkCheckClosingParens(final DetailAST ast) {
final DetailAST paren = ast.getLastChild();
final boolean parenExists = paren.getType() == TokenTypes.RPAREN;

if (closingParens == ClosingParens.ALWAYS
&& !parenExists) {
log(ast.getLineNo(), MSG_KEY_ANNOTATION_PARENS_MISSING);
if (closingParens == ClosingParens.ALWAYS) {
if (!parenExists) {
log(ast.getLineNo(), MSG_KEY_ANNOTATION_PARENS_MISSING);
}
}
else if (closingParens == ClosingParens.NEVER
&& parenExists
else if (parenExists
&& !ast.branchContains(TokenTypes.EXPR)
&& !ast.branchContains(TokenTypes.ANNOTATION_MEMBER_VALUE_PAIR)
&& !ast.branchContains(TokenTypes.ANNOTATION_ARRAY_INIT)) {
log(ast.getLineNo(), MSG_KEY_ANNOTATION_PARENS_PRESENT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,38 @@ public void testIncorrect() throws Exception {
verify(checkConfig, getPath("InputAnnotationLocationIncorrect.java"), expected);
}

@Test
public void testIncorrectAllTokens() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(AnnotationLocationCheck.class);
checkConfig.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[] expected = {
"6: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnn"),
"11: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
"17: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation1", 8, 4),
"25: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation1", 8, 4),
"29: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
"29: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation2"),
"32: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 7, 4),
"36: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 8, 4),
"37: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation3", 6, 4),
"38: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation4", 10, 4),
"41: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
"48: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation1", 12, 8),
"61: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 12, 8),
"65: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 12, 8),
"70: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 7, 4),
"73: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
"85: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 11, 8),
"88: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 10, 8),
"98: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 0, 3),
"100: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation2"),
};
verify(checkConfig, getPath("InputAnnotationLocationIncorrect.java"), expected);
}

@Test
public void testGetAcceptableTokens() {
final AnnotationLocationCheck constantNameCheckObj = new AnnotationLocationCheck();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public void testCheck() throws Exception {
"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"),
};
verify(config, getPath("InputAnnotationOnSameLineCheck.java"), expected);
}
Expand All @@ -90,6 +91,7 @@ public void testCheckAcceptableTokens() throws Exception {
"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"),
};
verify(config, getPath("InputAnnotationOnSameLineCheck.java"), expected);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public void testStyleCompact() throws Exception {
final String[] expected = {
"43: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT"),
"47: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT"),
"67: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT"),
};

verify(checkConfig, getPath("InputAnnotationUseStyleDifferentStyles.java"), expected);
Expand Down Expand Up @@ -227,6 +228,21 @@ public void testCommaAlwaysNoViolationsNonCompilable() throws Exception {
getNonCompilablePath("InputAnnotationUseStyleWithTrailingComma.java"), expected);
}

/**
* Test that annotation parens are always present.
*/
@Test
public void testTrailingArrayIgnore() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(AnnotationUseStyleCheck.class);
checkConfig.addAttribute("trailingArrayComma", "ignore");
final String[] expected = {
"9: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
"16: " + getCheckMessage(MSG_KEY_ANNOTATION_INCORRECT_STYLE, "COMPACT_NO_ARRAY"),
};

verify(checkConfig, getPath("InputAnnotationUseStyleWithTrailingComma.java"), expected);
}

@Test
public void testCommaNeverViolations() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(AnnotationUseStyleCheck.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,11 @@ void foo2() {}
@MyAnnotation1 //warn
(value = "")
@MyAnnotation2
class Foo {}
class Foo {
public void method1(@MyAnnotation3 @MyAnnotation2 Object param1) {
return;
}
}

@interface MyAnnotation1 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public int getX() {
return (int) x;
}

@Annotation2 @Annotation
public int field;

public
@Annotation int field2;
}

class SomeClass {
Expand All @@ -24,3 +29,5 @@ class SomeClass {

@interface Annotation {
}
@interface Annotation2 {
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,8 @@ enum E {
@SomeArrays(pooches = {})
@Another({})
class Closing {
static final String UN_U = "UN_U";

@SuppressWarnings(value = UN_U)
int d;
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,6 @@ enum Bleh3 {
public String toString() {
return "B";
}

private static void test() {}
}

0 comments on commit 3a4884c

Please sign in to comment.