AnnotatedMethodVisibilityModifier

Since Checkstyle 13.4.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.4.0
visibility Setter for allowed visibility modifiers. Allowed values: public, protected, private, package-private String[] package-private, protected 13.4.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.4.0

Examples

To configure the check:


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

Example:


import com.google.common.annotations.VisibleForTesting;

public class Example1 {

  @VisibleForTesting
  protected void allowedProtectedMethod() {}

  @VisibleForTesting
  void allowedPackagePrivateMethod() {}

  @VisibleForTesting
  protected int allowedProtectedField;

  @VisibleForTesting
  public void violationPublicMethod() {}
  // violation above 'Annotated element has disallowed visibility 'public'.'

  @VisibleForTesting
  private void violationPrivateMethod() {}
  // violation above 'Annotated element has disallowed visibility 'private'.'

  @VisibleForTesting
  @Deprecated
  public int violationPublicField;
  // violation above 'Annotated element has disallowed visibility 'public'.'

  @VisibleForTesting
  public Example1() {}
  // violation above 'Annotated element has disallowed visibility 'public'.'

}

To configure the check for only methods:


<module name="Checker">
  <module name="TreeWalker">
    <module name="AnnotatedMethodVisibilityModifier">
      <property name="tokens" value="PACKAGE_DEF, IMPORT, METHOD_DEF"/>
    </module>
  </module>
</module>

Example:


import com.google.common.annotations.VisibleForTesting;

public class Example2 {

  @VisibleForTesting
  protected void allowedProtectedMethod() {}

  @VisibleForTesting
  void allowedPackagePrivateMethod() {}

  @VisibleForTesting
  protected int allowedProtectedField;

  @VisibleForTesting
  public void violationPublicMethod() {}
  // violation above 'Annotated element has disallowed visibility 'public'.'

  @VisibleForTesting
  private void violationPrivateMethod() {}
  // violation above 'Annotated element has disallowed visibility 'private'.'

  @VisibleForTesting
  @Deprecated
  public int allowedPublicField;

  @VisibleForTesting
  public Example2() {}

}

To configure the annotations and allowed visibility:


<module name="Checker">
  <module name="TreeWalker">
    <module name="AnnotatedMethodVisibilityModifier">
      <property name="annotations"
        value="com.google.common.annotations.Beta, com.google.common.annotations.VisibleForTesting"/>
      <property name="visibility" value="package-private"/>
    </module>
  </module>
</module>

Example:


import com.google.common.annotations.VisibleForTesting;

public class Example3 {

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

  @VisibleForTesting
  public void violationPublicMethod() {}
  // violation above 'Annotated element has disallowed visibility 'public'.'

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

  @com.google.common.annotations.Beta
  int allowedPrivateField;

  @VisibleForTesting
  public Example3() {}
  // violation above 'Annotated element has disallowed visibility 'public'.'

}

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

Parent Module

TreeWalker