Skip to content

Commit

Permalink
Issue checkstyle#6462: Remove PARAMETER_DEF from AnnotationLocation
Browse files Browse the repository at this point in the history
  • Loading branch information
Vampire committed May 2, 2019
1 parent 883b07f commit 438ff2a
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 37 deletions.
4 changes: 0 additions & 4 deletions config/checkstyle_checks.xml
Expand Up @@ -149,10 +149,6 @@
<property name="tokens" value="ENUM_CONSTANT_DEF"/>
<property name="allowSamelineSingleParameterlessAnnotation" value="false"/>
</module>
<module name="AnnotationLocation">
<property name="tokens" value="PARAMETER_DEF"/>
<property name="allowSamelineMultipleAnnotations" value="true"/>
</module>
<module name="AnnotationOnSameLine">
<!-- we can not use it as it conflicts with AnnotationLocation -->
<property name="severity" value="ignore"/>
Expand Down
Expand Up @@ -141,8 +141,8 @@
* &lt;/module&gt;
* </pre>
* <p>
* The following example demonstrates how the check validates annotations of method parameters,
* catch parameters, foreach, for-loop variable definitions.
* The following example demonstrates how the check validates annotations of
* for-each and for-loop variable definitions.
* </p>
* <p>
* Configuration:
Expand All @@ -153,24 +153,19 @@
* &lt;property name=&quot;allowSamelineSingleParameterlessAnnotation&quot;
* value=&quot;false&quot;/&gt;
* &lt;property name=&quot;allowSamelineParameterizedAnnotation&quot; value=&quot;false&quot;/&gt;
* &lt;property name=&quot;tokens&quot; value=&quot;VARIABLE_DEF, PARAMETER_DEF&quot;/&gt;
* &lt;property name=&quot;tokens&quot; value=&quot;VARIABLE_DEF&quot;/&gt;
* &lt;/module&gt;
* </pre>
* <p>
* Code example:
* </p>
* <pre>
* public void test(&#64;MyAnnotation String s) { // OK
* public void test(String s) {
* ...
* for (&#64;MyAnnotation char c : s.toCharArray()) { ... } // OK
* ...
* try { ... }
* catch (&#64;MyAnnotation Exception ex) { ... } // OK
* ...
* for (&#64;MyAnnotation int i = 0; i &lt; 10; i++) { ... } // OK
* ...
* MathOperation c = (&#64;MyAnnotation int a, &#64;MyAnnotation int b) -&gt; a + b; // OK
* ...
* }
* </pre>
*
Expand All @@ -193,7 +188,6 @@ public class AnnotationLocationCheck extends AbstractCheck {

/** Array of single line annotation parents. */
private static final int[] SINGLELINE_ANNOTATION_PARENTS = {TokenTypes.FOR_EACH_CLAUSE,
TokenTypes.PARAMETER_DEF,
TokenTypes.FOR_INIT, };

/**
Expand Down Expand Up @@ -266,7 +260,6 @@ public int[] getAcceptableTokens() {
TokenTypes.METHOD_DEF,
TokenTypes.CTOR_DEF,
TokenTypes.VARIABLE_DEF,
TokenTypes.PARAMETER_DEF,
TokenTypes.ANNOTATION_DEF,
TokenTypes.ANNOTATION_FIELD_DEF,
};
Expand Down
Expand Up @@ -89,7 +89,7 @@ public void testIncorrect() throws Exception {
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, ANNOTATION_FIELD_DEF, "
+ "CTOR_DEF, VARIABLE_DEF, ANNOTATION_DEF, ANNOTATION_FIELD_DEF, "
+ "ENUM_CONSTANT_DEF, PACKAGE_DEF");
final String[] expected = {
"6: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnn"),
Expand All @@ -116,7 +116,6 @@ public void testIncorrectAllTokens() throws Exception {
"88: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 10, 8),
"91: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation1"),
"98: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "MyAnnotation2", 0, 3),
"100: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "MyAnnotation2"),
};
verify(checkConfig, getPath("InputAnnotationLocationIncorrect.java"), expected);
}
Expand All @@ -134,7 +133,6 @@ public void testGetAcceptableTokens() {
TokenTypes.METHOD_DEF,
TokenTypes.CTOR_DEF,
TokenTypes.VARIABLE_DEF,
TokenTypes.PARAMETER_DEF,
TokenTypes.ANNOTATION_DEF,
TokenTypes.ANNOTATION_FIELD_DEF,
};
Expand Down Expand Up @@ -189,7 +187,7 @@ public void testWithMultipleAnnotations() throws Exception {
public void testAllTokens() 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, ANNOTATION_FIELD_DEF");
+ "CTOR_DEF, VARIABLE_DEF, ANNOTATION_DEF, ANNOTATION_FIELD_DEF");
final String[] expected = CommonUtil.EMPTY_STRING_ARRAY;
verify(checkConfig, getPath("InputAnnotationLocationWithoutAnnotations.java"), expected);
}
Expand Down Expand Up @@ -238,13 +236,12 @@ public void testEnum() throws Exception {
@Test
public void testInterface() throws Exception {
final DefaultConfiguration checkConfig = createModuleConfig(AnnotationLocationCheck.class);
checkConfig.addAttribute("tokens", "INTERFACE_DEF, METHOD_DEF, PARAMETER_DEF");
checkConfig.addAttribute("tokens", "INTERFACE_DEF, METHOD_DEF");
final String[] expected = {
"17: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "InterfaceAnnotation", 2, 0),
"18: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "InterfaceAnnotation"),
"21: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "InterfaceAnnotation", 6, 4),
"22: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "InterfaceAnnotation"),
"25: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION, "InterfaceAnnotation", 10, 8),
};
verify(checkConfig, getPath("InputAnnotationLocationInterface.java"), expected);
}
Expand All @@ -264,7 +261,7 @@ public void testPackage() throws Exception {
public void testAnnotationInForEachLoopParameterAndVariableDef() 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, ANNOTATION_FIELD_DEF");
+ " CTOR_DEF, VARIABLE_DEF, ANNOTATION_DEF, ANNOTATION_FIELD_DEF");
checkConfig.addAttribute("allowSamelineMultipleAnnotations", "false");
checkConfig.addAttribute("allowSamelineSingleParameterlessAnnotation", "false");
checkConfig.addAttribute("allowSamelineParameterizedAnnotation", "false");
Expand Down Expand Up @@ -304,11 +301,13 @@ public void testAnnotationSingleParameterless() throws Exception {
checkConfig.addAttribute("allowSamelineSingleParameterlessAnnotation", "true");
checkConfig.addAttribute("allowSamelineParameterizedAnnotation", "false");
final String[] expected = {
"17: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "Annotation"),
"19: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "Annotation"),
"21: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "Annotation"),
"23: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "Annotation"),
"25: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "Annotation"),
"27: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "Annotation"),
"31: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "Annotation"),
"33: " + getCheckMessage(MSG_KEY_ANNOTATION_LOCATION_ALONE, "Annotation"),
};
verify(checkConfig, getPath("InputAnnotationLocationSingleParameterless.java"), expected);
}
Expand Down
Expand Up @@ -173,8 +173,7 @@ public class AllChecksTest extends AbstractModuleTestSupport {
GOOGLE_TOKENS_IN_CONFIG_TO_IGNORE.put("AnnotationLocation", Stream.of(
// state of the configuration when test was made until reason found in
// https://github.com/checkstyle/checkstyle/issues/3730
"ANNOTATION_DEF", "ANNOTATION_FIELD_DEF", "ENUM_CONSTANT_DEF", "PACKAGE_DEF",
"PARAMETER_DEF")
"ANNOTATION_DEF", "ANNOTATION_FIELD_DEF", "ENUM_CONSTANT_DEF", "PACKAGE_DEF")
.collect(Collectors.toSet()));
GOOGLE_TOKENS_IN_CONFIG_TO_IGNORE.put("AbbreviationAsWordInName", Stream.of(
// enum values should be uppercase
Expand Down
Expand Up @@ -98,6 +98,10 @@ void foo2() {}
@MyAnnotation2
class Foo {
public void method1(@MyAnnotation3 @MyAnnotation2 Object param1) {
try {
}
catch (@MyAnnotation3 @MyAnnotation2 Exception e) {
}
return;
}
}
Expand Down
Expand Up @@ -22,7 +22,7 @@
@InterfaceAnnotation("bar") void method( //warn
int param1,
@InterfaceAnnotation(value = "foo")
@InterfaceAnnotation //warn
@InterfaceAnnotation
@InterfaceAnnotation("bar") int param2,
int param3);

Expand Down
@@ -1,6 +1,8 @@
package com.puppycrawl.tools.checkstyle.checks.annotation.annotationlocation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Repeatable;
import java.lang.annotation.Target;

/**
* This test-input is intended to be checked using following configuration:
Expand All @@ -24,11 +26,20 @@ class InputAnnotationLocationSingleParameterless {

@Annotation("") @Annotation(value = "") void multipleParametrized() {} //warn

void parameterlessSamelineInForEach() {
for (@Annotation Object o : new Object[0]) break; //ok
for (@Annotation @Annotation Object o : new Object[0]) break; //warn
for (@Annotation Object o;;) break; // ok
for (@Annotation @Annotation Object o;;) break; //warn
}

@Repeatable(Annotations.class)
@Target({ElementType.METHOD, ElementType.LOCAL_VARIABLE})
@interface Annotation {
String value() default "";
}

@Target({ElementType.METHOD, ElementType.LOCAL_VARIABLE})
@interface Annotations {
Annotation[] value();
}
Expand Down
15 changes: 4 additions & 11 deletions src/xdocs/config_annotation.xml
Expand Up @@ -103,8 +103,6 @@ public String getNameIfPresent() { ... }
CTOR_DEF</a>,
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#VARIABLE_DEF">
VARIABLE_DEF</a>,
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#PARAMETER_DEF">
PARAMETER_DEF</a>,
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ANNOTATION_DEF">
ANNOTATION_DEF</a>,
<a href="apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#ANNOTATION_FIELD_DEF">
Expand Down Expand Up @@ -189,8 +187,8 @@ public String getNameIfPresent() { ... }
&lt;/module&gt;
</source>
<p>
The following example demonstrates how the check validates annotations of method
parameters, catch parameters, foreach, for-loop variable definitions.
The following example demonstrates how the check validates annotations of
for-each and for-loop variable definitions.
</p>
<p>Configuration:</p>
<source>
Expand All @@ -199,22 +197,17 @@ public String getNameIfPresent() { ... }
&lt;property name=&quot;allowSamelineSingleParameterlessAnnotation&quot;
value=&quot;false&quot;/&gt;
&lt;property name=&quot;allowSamelineParameterizedAnnotation&quot; value=&quot;false&quot;/&gt;
&lt;property name=&quot;tokens&quot; value=&quot;VARIABLE_DEF, PARAMETER_DEF&quot;/&gt;
&lt;property name=&quot;tokens&quot; value=&quot;VARIABLE_DEF&quot;/&gt;
&lt;/module&gt;
</source>
<p>Code example:</p>
<source>
public void test(&#64;MyAnnotation String s) { // OK
public void test(String s) {
...
for (&#64;MyAnnotation char c : s.toCharArray()) { ... } // OK
...
try { ... }
catch (&#64;MyAnnotation Exception ex) { ... } // OK
...
for (&#64;MyAnnotation int i = 0; i &lt; 10; i++) { ... } // OK
...
MathOperation c = (&#64;MyAnnotation int a, &#64;MyAnnotation int b) -&gt; a + b; // OK
...
}
</source>
</subsection>
Expand Down

0 comments on commit 438ff2a

Please sign in to comment.