AnnotatedDeclarationVisibility

Since Checkstyle 13.9.0

Description

Checks that elements annotated with specified annotations have only allowed visibility modifiers.

This check enforces consistency between annotation presence and declared visibility. If a configured annotation is found on a target element, its visibility modifier must match one of the allowed values.

Properties

name description type default value since
annotations Setter for annotation canonical names. String[] com.google.common.annotations.VisibleForTesting 13.9.0
visibility Setter for allowed visibility modifiers. Allowed values: public, protected, package, private. AccessModifierOption[] protected, package 13.9.0
tokens tokens to check subset of tokens CLASS_DEF , INTERFACE_DEF , ENUM_DEF , RECORD_DEF , METHOD_DEF , CTOR_DEF , VARIABLE_DEF , ANNOTATION_DEF . CLASS_DEF , INTERFACE_DEF , ENUM_DEF , RECORD_DEF , METHOD_DEF , CTOR_DEF , VARIABLE_DEF , ANNOTATION_DEF . 13.9.0

Examples

To configure the check:


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

Example:


import com.google.common.annotations.VisibleForTesting;

public class Example1 {

  // ok, default visibility includes protected
  @VisibleForTesting
  @com.google.common.annotations.Beta
  protected void protectedMethod() {}

  // ok, default visibility includes package-private
  @VisibleForTesting
  @com.google.common.annotations.Beta
  void packagePrivateMethod() {}

  // ok, default visibility includes protected
  @VisibleForTesting
  @com.google.common.annotations.Beta
  protected int protectedField;

  // violation below 'Annotated element has disallowed visibility 'public'.'
  @VisibleForTesting
  @com.google.common.annotations.Beta
  public void publicMethod() {}

  // ok, no VisibleForTesting annotation
  @com.google.common.annotations.Beta
  private void privateMethod() {}

  // violation below 'Annotated element has disallowed visibility 'public'.'
  @VisibleForTesting
  @com.google.common.annotations.Beta
  @Deprecated
  public int publicField;

  // violation below 'Annotated element has disallowed visibility 'public'.'
  @VisibleForTesting
  @com.google.common.annotations.Beta
  public Example1() {}

}

To configure the check for only methods:


<module name="Checker">
  <module name="TreeWalker">
    <module name="AnnotatedDeclarationVisibility">
      <property name="tokens" value="METHOD_DEF"/>
    </module>
  </module>
</module>

Example:


import com.google.common.annotations.VisibleForTesting;

public class Example2 {

  // ok, default visibility includes protected
  @VisibleForTesting
  @com.google.common.annotations.Beta
  protected void protectedMethod() {}

  // ok, default visibility includes package-private
  @VisibleForTesting
  @com.google.common.annotations.Beta
  void packagePrivateMethod() {}

  // ok, fields are not checked when tokens only include METHOD_DEF
  @VisibleForTesting
  @com.google.common.annotations.Beta
  protected int protectedField;

  // violation below 'Annotated element has disallowed visibility 'public'.'
  @VisibleForTesting
  @com.google.common.annotations.Beta
  public void publicMethod() {}

  // ok, no VisibleForTesting annotation
  @com.google.common.annotations.Beta
  private void privateMethod() {}

  // ok, fields are not checked when tokens only include METHOD_DEF
  @VisibleForTesting
  @com.google.common.annotations.Beta
  @Deprecated
  public int publicField;

  // ok, constructors are not checked when tokens only include METHOD_DEF
  @VisibleForTesting
  @com.google.common.annotations.Beta
  public Example2() {}

}

To configure the annotations:


<module name="Checker">
  <module name="TreeWalker">
    <module name="AnnotatedDeclarationVisibility">
      <property name="annotations"
        value="com.google.common.annotations.Beta, com.google.common.annotations.VisibleForTesting"/>
    </module>
  </module>
</module>

Example:


import com.google.common.annotations.VisibleForTesting;

public class Example3 {

  // ok, default visibility includes protected
  @VisibleForTesting
  @com.google.common.annotations.Beta
  protected void protectedMethod() {}

  // ok, visibility includes package-private
  @VisibleForTesting
  @com.google.common.annotations.Beta
  void packagePrivateMethod() {}

  // ok, default visibility includes protected
  @VisibleForTesting
  @com.google.common.annotations.Beta
  protected int protectedField;

  // violation below 'Annotated element has disallowed visibility 'public'.'
  @VisibleForTesting
  @com.google.common.annotations.Beta
  public void publicMethod() {}

  // violation below 'Annotated element has disallowed visibility 'private'.'
  @com.google.common.annotations.Beta
  private void privateMethod() {}

  // violation below 'Annotated element has disallowed visibility 'public'.'
  @VisibleForTesting
  @com.google.common.annotations.Beta
  @Deprecated
  public int publicField;

  // violation below 'Annotated element has disallowed visibility 'public'.'
  @VisibleForTesting
  @com.google.common.annotations.Beta
  public Example3() {}

}

To configure the allowed visibility:


<module name="Checker">
  <module name="TreeWalker">
    <module name="AnnotatedDeclarationVisibility">
      <property name="visibility" value="package"/>
    </module>
  </module>
</module>

Example:


import com.google.common.annotations.VisibleForTesting;

public class Example4 {

  // violation below 'Annotated element has disallowed visibility 'protected'.'
  @VisibleForTesting
  @com.google.common.annotations.Beta
  protected void protectedMethod() {}

  // ok, visibility includes package-private
  @VisibleForTesting
  @com.google.common.annotations.Beta
  void packagePrivateMethod() {}

  // violation below 'Annotated element has disallowed visibility 'protected'.'
  @VisibleForTesting
  @com.google.common.annotations.Beta
  protected int protectedField;

  // violation below 'Annotated element has disallowed visibility 'public'.'
  @VisibleForTesting
  @com.google.common.annotations.Beta
  public void publicMethod() {}

  // ok, no VisibleForTesting annotation
  @com.google.common.annotations.Beta
  private void privateMethod() {}

  // violation below 'Annotated element has disallowed visibility 'public'.'
  @VisibleForTesting
  @com.google.common.annotations.Beta
  @Deprecated
  public int publicField;

  // violation below 'Annotated element has disallowed visibility 'public'.'
  @VisibleForTesting
  @com.google.common.annotations.Beta
  public Example4() {}

}

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.modifier. AnnotatedDeclarationVisibilityCheck

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

Parent Module

TreeWalker