Skip to content

AnnotationLocation allows same-line parameterless annotation on class declarations (violates Google Style §4.8.5.2) #19559

@vivek-0509

Description

@vivek-0509

I have read check documentation: https://checkstyle.org/checks/annotation/annotationlocation.html
I have downloaded the latest checkstyle from: https://checkstyle.org/cmdline.html#Download_and_Run
I have executed the cli and showed it below, as cli describes the problem better than 1,000 words

vivek@Viveks-MacBook-Air tmp % cat > /var/tmp/TestAnnotationLocation.java << 'EOF'
public class TestAnnotationLocation {
  @Deprecated static class Foo {}
  @Deprecated interface Bar {}
  @Deprecated enum Size { S, M, L }
  @Deprecated record Point(int x) {}

  @Override public int hashCode() {
    return 0;
  }
}
EOF

vivek@Viveks-MacBook-Air tmp % cd /var/tmp
javac TestAnnotationLocation.java

vivek@Viveks-MacBook-Air tmp % RUN_LOCALE="-Duser.language=en -Duser.country=US"

vivek@Viveks-MacBook-Air tmp % java $RUN_LOCALE -jar /Users/vivek/checkstyle/target/checkstyle-13.4.1-SNAPSHOT-all.jar -c /google_checks.xml TestAnnotationLocation.java
Starting audit...
[WARN] /private/var/tmp/TestAnnotationLocation.java:1:1: Missing a Javadoc comment. [MissingJavadocType]
[WARN] /private/var/tmp/TestAnnotationLocation.java:3:3: 'INTERFACE_DEF' should be separated from previous line. [EmptyLineSeparator]
[WARN] /private/var/tmp/TestAnnotationLocation.java:4:3: 'ENUM_DEF' should be separated from previous line. [EmptyLineSeparator]
[WARN] /private/var/tmp/TestAnnotationLocation.java:5:3: 'RECORD_DEF' should be separated from previous line. [EmptyLineSeparator]
Audit done.

Describe what you expect in detail.

I expect violations on lines 2, 3, 4, and 5 because Google Java Style Guide §4.8.5.2 states:

"Annotations applying to a class, package, or module declaration appear immediately after the documentation block, and each annotation is listed on a line of its own (that is, one annotation per line)."

No same-line exception exists for classes/interfaces/enums/records/packages, unlike §4.8.5.3 which explicitly allows a single parameterless annotation on the same line for methods/constructors.

Expected output:

Starting audit...
[WARN] /private/var/tmp/TestAnnotationLocation.java:1:1: Missing a Javadoc comment. [MissingJavadocType]
[WARN] /private/var/tmp/TestAnnotationLocation.java:2:3: Annotation 'Deprecated' should be alone on line. [AnnotationLocationClassPackage]
[WARN] /private/var/tmp/TestAnnotationLocation.java:3:3: 'INTERFACE_DEF' should be separated from previous line. [EmptyLineSeparator]
[WARN] /private/var/tmp/TestAnnotationLocation.java:3:3: Annotation 'Deprecated' should be alone on line. [AnnotationLocationClassPackage]
[WARN] /private/var/tmp/TestAnnotationLocation.java:4:3: 'ENUM_DEF' should be separated from previous line. [EmptyLineSeparator]
[WARN] /private/var/tmp/TestAnnotationLocation.java:4:3: Annotation 'Deprecated' should be alone on line. [AnnotationLocationClassPackage]
[WARN] /private/var/tmp/TestAnnotationLocation.java:5:3: 'RECORD_DEF' should be separated from previous line. [EmptyLineSeparator]
[WARN] /private/var/tmp/TestAnnotationLocation.java:5:3: Annotation 'Deprecated' should be alone on line. [AnnotationLocationClassPackage]
Audit done.

Root cause: The AnnotationLocationMostCases module in google_checks.xml bundles class-like tokens with method/constructor tokens under the same configuration. Since allowSamelineSingleParameterlessAnnotation defaults to true, the §4.8.5.3 method exception is incorrectly applied to classes too.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions