AnnotationLocation

Since Checkstyle 6.0

Description

Checks location of annotation on language elements. By default, Check enforce to locate annotations immediately after documentation block and before target element, annotation should be located on separate line from target element. This check also verifies that the annotations are on the same indenting level as the annotated element if they are not on the same line.

Attention: Elements that cannot have JavaDoc comments like local variables are not in the scope of this check even though a token type like VARIABLE_DEF would match them.

Attention: Annotations among modifiers are ignored (looks like false-negative) as there might be a problem with annotations for return types:

public @Nullable Long getStartTimeOrNull() { ... }

Such annotations are better to keep close to type. Due to limitations, Checkstyle can not examine the target of an annotation.

Example:

@Override
@Nullable
public String getNameIfPresent() { ... }
        

Properties

name description type default value since
allowSamelineMultipleAnnotations Allow annotation(s) to be located on the same line as target element. boolean false 6.0
allowSamelineSingleParameterlessAnnotation Allow single parameterless annotation to be located on the same line as target element. boolean true 6.1
allowSamelineParameterizedAnnotation Allow one and only parameterized annotation to be located on the same line as target element. boolean false 6.4
tokens tokens to check subset of tokens CLASS_DEF , INTERFACE_DEF , PACKAGE_DEF , ENUM_CONSTANT_DEF , ENUM_DEF , METHOD_DEF , CTOR_DEF , VARIABLE_DEF , ANNOTATION_DEF , ANNOTATION_FIELD_DEF , RECORD_DEF , COMPACT_CTOR_DEF . CLASS_DEF , INTERFACE_DEF , PACKAGE_DEF , ENUM_CONSTANT_DEF , ENUM_DEF , METHOD_DEF , CTOR_DEF , VARIABLE_DEF , RECORD_DEF , COMPACT_CTOR_DEF . 6.0

Examples

To configure the default check to allow one single parameterless annotation on the same line:

<module name="AnnotationLocation"/>
        

Example for above configuration:

@NotNull private boolean field1; //ok
@Override public int hashCode() { return 1; } //ok
@NotNull //ok
private boolean field2;
@Override //ok
public boolean equals(Object obj) { return true; }
@Mock DataLoader loader; //ok
@SuppressWarnings("deprecation") DataLoader loader; //violation
@SuppressWarnings("deprecation") public int foo() { return 1; } //violation
@NotNull @Mock DataLoader loader; //violation
        

Use the following configuration to allow multiple annotations on the same line:

<module name="AnnotationLocation">
  <property name="allowSamelineMultipleAnnotations" value="true"/>
  <property name="allowSamelineSingleParameterlessAnnotation"
    value="false"/>
  <property name="allowSamelineParameterizedAnnotation" value="false"/>
</module>
        

Example to allow any location multiple annotations:

@NotNull private boolean field1; //ok
@Override public int hashCode() { return 1; } //ok
@NotNull //ok
private boolean field2;
@Override //ok
public boolean equals(Object obj) { return true; }
@Mock DataLoader loader; //ok
@SuppressWarnings("deprecation") DataLoader loader; //ok
@SuppressWarnings("deprecation") public int foo() { return 1; } //ok
@NotNull @Mock DataLoader loader; //ok
        

Use the following configuration to allow only one and only parameterized annotation on the same line:

<module name="AnnotationLocation">
  <property name="allowSamelineMultipleAnnotations" value="false"/>
  <property name="allowSamelineSingleParameterlessAnnotation"
    value="false"/>
  <property name="allowSamelineParameterizedAnnotation" value="true"/>
</module>
        

Example to allow only one and only parameterized annotation on the same line:

@NotNull private boolean field1; //violation
@Override public int hashCode() { return 1; } //violation
@NotNull //ok
private boolean field2;
@Override //ok
public boolean equals(Object obj) { return true; }
@Mock DataLoader loader; //violation
@SuppressWarnings("deprecation") DataLoader loader; //ok
@SuppressWarnings("deprecation") public int foo() { return 1; } //ok
@NotNull @Mock DataLoader loader; //violation
        

Use the following configuration to only validate annotations on methods to allow one single parameterless annotation on the same line:

<module name="AnnotationLocation">
  <property name="tokens" value="METHOD_DEF"/>
  <property name="allowSamelineMultipleAnnotations" value="false"/>
  <property name="allowSamelineSingleParameterlessAnnotation"
    value="true"/>
  <property name="allowSamelineParameterizedAnnotation" value="false"/>
</module>
        

Example for above configuration to check only methods:

@NotNull private boolean field1; //ok
@Override public int hashCode() { return 1; } //ok
@NotNull //ok
private boolean field2;
@Override //ok
public boolean equals(Object obj) { return true; }
@Mock DataLoader loader; //ok
@SuppressWarnings("deprecation") DataLoader loader; //ok
@SuppressWarnings("deprecation") public int foo() { return 1; } //violation
@NotNull @Mock DataLoader loader; //ok
        

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Package

com.puppycrawl.tools.checkstyle.checks.annotation

Parent Module

TreeWalker