OpenjdkAnnotationLocation

Since Checkstyle 13.9.0

Description

Verifies that annotations are properly placed by OpenJDK Style. Declaration annotations must either reside entirely on a single line or have each annotation placed on its own separate line. Annotations should not share a line with the target declaration, except for single-line methods and fields.

Attention: Checkstyle ignores annotations placed among modifiers due to a technical limitation. The parser cannot distinguish whether an annotation applies to the method itself or to its return type.

Properties

Examples

To configure the check to follow openjdk guidelines for annotations:


<module name="Checker">
  <module name="TreeWalker">
    <module name="OpenjdkAnnotationLocation"/>
  </module>
</module>

Example:


class Example1 {
  @Nonnull @Deprecated
  public void foo1() {
  }

  @Deprecated public void test() {}

  @Nonnull
  @Deprecated @SafeVarargs
  public final void badMethodOne(String... str) {
  // violation above """Annotations on 'badMethodOne' must be all on one line
  // or all on separate lines."""
  }

  @Nonnull
  @Deprecated
  @SafeVarargs
  public final void goodMethodOne(String... str) {
  }

  @Nonnull @Deprecated @SafeVarargs
  public final void goodMethodTwo(String... str) {
  }

  @Nonnull @Deprecated public void fooBad() {
  // violation above 'Annotations must be on a separate line from 'fooBad'.'
  }

  @Deprecated @Nonnull public void fooGood() {}
  @Deprecated int test;

  // ok for Nullable and Deprecated due to checkstyle limitations
  public @Nullable @Deprecated List<String> getItemsOrNull(List<String> items) {
    return items;
  }

}

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.

Fully Qualified Name

com.puppycrawl.tools.checkstyle.checks.annotation.OpenjdkAnnotationLocationCheck

Use this fully qualified class name in configuration when an exact class reference is required.

Parent Module

TreeWalker