Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ public int[] getAcceptableTokens() {
TokenTypes.ANNOTATION_FIELD_DEF,
TokenTypes.RECORD_DEF,
TokenTypes.COMPACT_CTOR_DEF,
TokenTypes.TYPECAST,
TokenTypes.TYPE_ARGUMENT,
TokenTypes.DOT,
TokenTypes.LITERAL_NEW,
TokenTypes.LITERAL_THROWS,
TokenTypes.IMPLEMENTS_CLAUSE,
};
}

Expand Down
139 changes: 139 additions & 0 deletions src/site/xdoc/checks/annotation/annotationlocation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ public String getNameIfPresent() { ... }
RECORD_DEF</a>
, <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#COMPACT_CTOR_DEF">
COMPACT_CTOR_DEF</a>
, <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#TYPECAST">
TYPECAST</a>
, <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#TYPE_ARGUMENT">
TYPE_ARGUMENT</a>
, <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#DOT">
DOT</a>
, <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_NEW">
LITERAL_NEW</a>
, <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#LITERAL_THROWS">
LITERAL_THROWS</a>
, <a href="../../apidocs/com/puppycrawl/tools/checkstyle/api/TokenTypes.html#IMPLEMENTS_CLAUSE">
IMPLEMENTS_CLAUSE</a>
.
</td>
<td>
Expand Down Expand Up @@ -278,6 +290,133 @@ class Example4 {
// violation above, 'Annotation 'SuppressWarnings' should be alone on line.'
@NotNull @Mock DataLoader loader2;
}
</code></pre></div><hr class="example-separator"/>
<p id="Example5-config">
To configure the check to validate annotations on type arguments:
</p>
<div class="wrapper"><pre class="prettyprint"><code class="language-xml">
&lt;module name="Checker"&gt;
&lt;module name="TreeWalker"&gt;
&lt;module name="AnnotationLocation"&gt;
&lt;property name="tokens" value="TYPE_ARGUMENT"/&gt;
&lt;/module&gt;
&lt;/module&gt;
&lt;/module&gt;
</code></pre></div>
<p id="Example5-code">Example:</p>
<div class="wrapper"><pre class="prettyprint"><code class="language-java">
class Example5 {
List&lt;@TypeArgAnnotation1 String&gt; names; // ok
List&lt;@TypeArgAnnotation1 @TypeArgAnnotation2 String&gt; data;
// violation above, 'Annotation 'TypeArgAnnotation2' should be alone on line.'
Map&lt;@TypeArgAnnotation1 String, @TypeArgAnnotation1 Integer&gt; map; // ok
}
</code></pre></div><hr class="example-separator"/>
<p id="Example6-config">
To configure the check to validate annotations on typecasts:
</p>
<div class="wrapper"><pre class="prettyprint"><code class="language-xml">
&lt;module name="Checker"&gt;
&lt;module name="TreeWalker"&gt;
&lt;module name="AnnotationLocation"&gt;
&lt;property name="tokens" value="TYPECAST"/&gt;
&lt;/module&gt;
&lt;/module&gt;
&lt;/module&gt;
</code></pre></div>
<p id="Example6-code">Example:</p>
<div class="wrapper"><pre class="prettyprint"><code class="language-java">
class Example6 {
void method(Object obj) {
String s1 = (@TypeCastAnnotation1 String) obj; // ok
String s2 = (@TypeCastAnnotation1 @TypeCastAnnotation2 String) obj;
// violation above, 'Annotation 'TypeCastAnnotation2' should be alone on line.'
}
}
</code></pre></div><hr class="example-separator"/>
<p id="Example7-config">
To configure the check to validate annotations on constructor invocations:
</p>
<div class="wrapper"><pre class="prettyprint"><code class="language-xml">
&lt;module name="Checker"&gt;
&lt;module name="TreeWalker"&gt;
&lt;module name="AnnotationLocation"&gt;
&lt;property name="tokens" value="LITERAL_NEW"/&gt;
&lt;/module&gt;
&lt;/module&gt;
&lt;/module&gt;
</code></pre></div>
<p id="Example7-code">Example:</p>
<div class="wrapper"><pre class="prettyprint"><code class="language-java">
class Example7 {
void method() {
String[] arr1 = new @NewObjectAnnotation1 String[10]; // ok
String[] arr2 = new @NewObjectAnnotation1 @NewObjectAnnotation2 String[10];
// violation above, 'Annotation 'NewObjectAnnotation2' should be alone on line.'
Object obj = new @NewObjectAnnotation1 Object(); // ok
}
}
</code></pre></div><hr class="example-separator"/>
<p id="Example8-config">
To configure the check to validate annotations on exception types in throws clause:
</p>
<div class="wrapper"><pre class="prettyprint"><code class="language-xml">
&lt;module name="Checker"&gt;
&lt;module name="TreeWalker"&gt;
&lt;module name="AnnotationLocation"&gt;
&lt;property name="tokens" value="LITERAL_THROWS"/&gt;
&lt;/module&gt;
&lt;/module&gt;
&lt;/module&gt;
</code></pre></div>
<p id="Example8-code">Example:</p>
<div class="wrapper"><pre class="prettyprint"><code class="language-java">
class Example8 {
void method1() throws @ThrowsAnnotation1 IOException { } // ok
void method2() throws @ThrowsAnnotation1 @ThrowsAnnotation2 IOException { }
// violation above, 'Annotation 'ThrowsAnnotation2' should be alone on line.'
}
</code></pre></div><hr class="example-separator"/>
<p id="Example9-config">
To configure the check to validate annotations on interface types in implements clause:
</p>
<div class="wrapper"><pre class="prettyprint"><code class="language-xml">
&lt;module name="Checker"&gt;
&lt;module name="TreeWalker"&gt;
&lt;module name="AnnotationLocation"&gt;
&lt;property name="tokens" value="IMPLEMENTS_CLAUSE"/&gt;
&lt;/module&gt;
&lt;/module&gt;
&lt;/module&gt;
</code></pre></div>
<p id="Example9-code">Example:</p>
<div class="wrapper"><pre class="prettyprint"><code class="language-java">
class Example9 implements @ImplementsAnnotation1 Serializable { } // ok
class Example9a implements @ImplementsAnnotation1 @ImplementsAnnotation2
Serializable { }
// violation above, 'Annotation 'ImplementsAnnotation2' should be alone on line.'
</code></pre></div><hr class="example-separator"/>
<p id="Example10-config">
To configure the check to validate annotations on qualified types:
</p>
<div class="wrapper"><pre class="prettyprint"><code class="language-xml">
&lt;module name="Checker"&gt;
&lt;module name="TreeWalker"&gt;
&lt;module name="AnnotationLocation"&gt;
&lt;property name="tokens" value="DOT"/&gt;
&lt;/module&gt;
&lt;/module&gt;
&lt;/module&gt;
</code></pre></div>
<p id="Example10-code">Example:</p>
<div class="wrapper"><pre class="prettyprint"><code class="language-java">
class Example10 {
void method() {
java.lang.@DotAnnotation1 String s; // ok
java.lang.@DotAnnotation1 @DotAnnotation2 String s2;
// violation above, 'Annotation 'DotAnnotation2' should be alone on line.'
}
}
</code></pre></div>
</subsection>

Expand Down
84 changes: 84 additions & 0 deletions src/site/xdoc/checks/annotation/annotationlocation.xml.template
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,90 @@
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/annotation/annotationlocation/Example4.java"/>
<param name="type" value="code"/>
</macro><hr class="example-separator"/>
<p id="Example5-config">
To configure the check to validate annotations on type arguments:
</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/annotation/annotationlocation/Example5.java"/>
<param name="type" value="config"/>
</macro>
<p id="Example5-code">Example:</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/annotation/annotationlocation/Example5.java"/>
<param name="type" value="code"/>
</macro><hr class="example-separator"/>
<p id="Example6-config">
To configure the check to validate annotations on typecasts:
</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/annotation/annotationlocation/Example6.java"/>
<param name="type" value="config"/>
</macro>
<p id="Example6-code">Example:</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/annotation/annotationlocation/Example6.java"/>
<param name="type" value="code"/>
</macro><hr class="example-separator"/>
<p id="Example7-config">
To configure the check to validate annotations on constructor invocations:
</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/annotation/annotationlocation/Example7.java"/>
<param name="type" value="config"/>
</macro>
<p id="Example7-code">Example:</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/annotation/annotationlocation/Example7.java"/>
<param name="type" value="code"/>
</macro><hr class="example-separator"/>
<p id="Example8-config">
To configure the check to validate annotations on exception types in throws clause:
</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/annotation/annotationlocation/Example8.java"/>
<param name="type" value="config"/>
</macro>
<p id="Example8-code">Example:</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/annotation/annotationlocation/Example8.java"/>
<param name="type" value="code"/>
</macro><hr class="example-separator"/>
<p id="Example9-config">
To configure the check to validate annotations on interface types in implements clause:
</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/annotation/annotationlocation/Example9.java"/>
<param name="type" value="config"/>
</macro>
<p id="Example9-code">Example:</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/annotation/annotationlocation/Example9.java"/>
<param name="type" value="code"/>
</macro><hr class="example-separator"/>
<p id="Example10-config">
To configure the check to validate annotations on qualified types:
</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/annotation/annotationlocation/Example10.java"/>
<param name="type" value="config"/>
</macro>
<p id="Example10-code">Example:</p>
<macro name="example">
<param name="path"
value="resources/com/puppycrawl/tools/checkstyle/checks/annotation/annotationlocation/Example10.java"/>
<param name="type" value="code"/>
</macro>
</subsection>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ public void testGetAcceptableTokens() {
TokenTypes.ANNOTATION_FIELD_DEF,
TokenTypes.RECORD_DEF,
TokenTypes.COMPACT_CTOR_DEF,
TokenTypes.TYPECAST,
TokenTypes.TYPE_ARGUMENT,
TokenTypes.DOT,
TokenTypes.LITERAL_NEW,
TokenTypes.LITERAL_THROWS,
TokenTypes.IMPLEMENTS_CLAUSE,
};
assertWithMessage("Default acceptable tokens are invalid")
.that(actual)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,10 @@ public class AllChecksTest extends AbstractModuleTestSupport {
CHECKSTYLE_TOKENS_IN_CONFIG_TO_IGNORE.put("AnnotationLocation",
Stream.of("CLASS_DEF", "CTOR_DEF", "ENUM_DEF", "INTERFACE_DEF",
"METHOD_DEF", "VARIABLE_DEF",
"RECORD_DEF", "COMPACT_CTOR_DEF")
"RECORD_DEF", "COMPACT_CTOR_DEF",
// Type annotation tokens - opt-in only, not used by default
"TYPECAST", "TYPE_ARGUMENT", "DOT", "LITERAL_NEW",
"LITERAL_THROWS", "IMPLEMENTS_CLAUSE")
.collect(Collectors.toUnmodifiableSet()));
CHECKSTYLE_TOKENS_IN_CONFIG_TO_IGNORE.put("NoLineWrap", Stream.of(
// method/constructor declaration could be long due to "parameters/exceptions", it
Expand Down Expand Up @@ -188,7 +191,10 @@ 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")
"ANNOTATION_DEF", "ANNOTATION_FIELD_DEF", "ENUM_CONSTANT_DEF", "PACKAGE_DEF",
// Type annotation tokens - opt-in only, not used by default
"TYPECAST", "TYPE_ARGUMENT", "DOT", "LITERAL_NEW",
"LITERAL_THROWS", "IMPLEMENTS_CLAUSE")
.collect(Collectors.toUnmodifiableSet()));
GOOGLE_TOKENS_IN_CONFIG_TO_IGNORE.put("AbbreviationAsWordInName", Stream.of(
// enum values should be uppercase
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*xml
<module name="Checker">
<module name="TreeWalker">
<module name="AnnotationLocation">
<property name="tokens" value="DOT"/>
</module>
</module>
</module>
*/

package com.puppycrawl.tools.checkstyle.checks.annotation.annotationlocation;

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

@Target(ElementType.TYPE_USE)
@interface DotAnnotation1 {}

@Target(ElementType.TYPE_USE)
@interface DotAnnotation2 {}

// xdoc section -- start
class Example10 {
void method() {
java.lang.@DotAnnotation1 String s; // ok
java.lang.@DotAnnotation1 @DotAnnotation2 String s2;
// violation above, 'Annotation 'DotAnnotation2' should be alone on line.'
}
}
// xdoc section -- end
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*xml
<module name="Checker">
<module name="TreeWalker">
<module name="AnnotationLocation">
<property name="tokens" value="TYPE_ARGUMENT"/>
</module>
</module>
</module>
*/

package com.puppycrawl.tools.checkstyle.checks.annotation.annotationlocation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import java.util.List;
import java.util.Map;

@Target(ElementType.TYPE_USE)
@interface TypeArgAnnotation1 {}

@Target(ElementType.TYPE_USE)
@interface TypeArgAnnotation2 {}

// xdoc section -- start
class Example5 {
List<@TypeArgAnnotation1 String> names; // ok
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please remove all OK comments.
in all files.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay

List<@TypeArgAnnotation1 @TypeArgAnnotation2 String> data;
// violation above, 'Annotation 'TypeArgAnnotation2' should be alone on line.'
Map<@TypeArgAnnotation1 String, @TypeArgAnnotation1 Integer> map; // ok
}
// xdoc section -- end
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*xml
<module name="Checker">
<module name="TreeWalker">
<module name="AnnotationLocation">
<property name="tokens" value="TYPECAST"/>
</module>
</module>
</module>
*/

package com.puppycrawl.tools.checkstyle.checks.annotation.annotationlocation;

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

@Target(ElementType.TYPE_USE)
@interface TypeCastAnnotation1 {}

@Target(ElementType.TYPE_USE)
@interface TypeCastAnnotation2 {}

// xdoc section -- start
class Example6 {
void method(Object obj) {
String s1 = (@TypeCastAnnotation1 String) obj; // ok
String s2 = (@TypeCastAnnotation1 @TypeCastAnnotation2 String) obj;
// violation above, 'Annotation 'TypeCastAnnotation2' should be alone on line.'
}
}
// xdoc section -- end
Loading