PackageAnnotation

Since Checkstyle 5.0

Description

Checks that all package annotations are in the package-info.java file.

For Java SE8 and above, placement of package annotations in the package-info.java file is enforced by the compiler and this check is not necessary.

For Java SE7 and below, the Java Language Specification highly recommends but doesn't require that annotations are placed in the package-info.java file, and this check can help to enforce that placement.

See Java Language Specification, §7.4.1 for more info.

Examples

To configure the check:

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

Example of class with violation:

@Deprecated
package com.puppycrawl.tools.checkstyle.checks.annotation.packageannotation;
// violation above, 'Package annotations must be in the package-info.java info.'

class Example1 {}
        

Example of class without violation:

package com.puppycrawl.tools.checkstyle.checks.annotation.packageannotation;

class Example2 {}
        

Example of validating package-info.java:

@Deprecated
package com.puppycrawl.tools.checkstyle.checks.annotation.packageannotation.example3;
        

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