Vlad Rozov
Vlad Rozov

Reputation: 131

Checkstyle for Annotation

I want to create a checkstyle rule that enforces Annotations on a separate line except for function parameters and for each loops. Is there a way to create such rule, so that the following code will be valid:

@Deprecated
public class Annotation
{
  @Deprecated
  public void test(@Nullable String s) {
    @Rule
    Integer i;
    for (@Nullable Char c : s.getChars()) {
    }
  }
}

Upvotes: 1

Views: 1395

Answers (1)

Anakin
Anakin

Reputation: 11

You can use AnnotationLocation check, but looks like it is not possible now to create such a rule, as both Integer i and Char c are VARIABLE_DEF, so you can either enforce all variable definitions to have their annotations on a separate line:

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

or allow them on the same line:

    <module name="AnnotationLocation">
      <property name="allowSamelineSingleParameterlessAnnotation" value="false"/>
      <property name="tokens" value="CLASS_DEF, INTERFACE_DEF, ENUM_DEF, METHOD_DEF, CTOR_DEF"/>
    </module>

However, it is a good idea for a new option for AnnotationLocation check. Please, create an issue for this: issue tracking, How to request new feature for existing functionality?.

Upvotes: 1

Related Questions