Skip to content

fix all false-negatives about there is no single space between a type annotation and [] or .... for Google Style #8205

@HuGanghui

Description

@HuGanghui

According to the latest Google Style https://checkstyle.org/styleguides/google-java-style-20180523/javaguide.html#s4.6.2-horizontal-whitespace,

Between a type annotation and [] or ....

So valid cases are:
String... String[], String @NotNull [], String @NotNull ...
Invalid cases:
String ... String [], String @NotNull[], String @NotNull...

As we removing ELIPSIS token from config in #6707 to avoid false-positives (unwanted violations)
We need to make new Check to address cases above and do not make any false-positives, as general Check NoWhitespaceBefore should not be over complicated with nuances of annotations before token.

$ cat Test.java

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;

public class Test {

  @Target(ElementType.TYPE_USE)
  @interface NonNull {
  }

  @NonNull int @NonNull[] @NonNull[] fiel1; // false-negative
  @NonNull int @NonNull [] @NonNull [] field2;
  //@NonNull int @NonNull ... field3; // non-compilable
  //@NonNull int @NonNull... field4; // non-compilable


  public void foo2(final char[] param) {
  }

  public void foo1(final char [] param) {
  }

  public void foo3(final char @NonNull[] param) { // false-negative
  }

  public void foo4(final char @NonNull [] param) {
  }

  void test4(String... param) { 
  }

  void test3(String ... param) { // violation on 8.32, no violation is #6707 fixes
  }

  void test2(String @NonNull... param) { // false-negative
  }

  void test1(String @NonNull ... param) { // violation on 8.32, no violation aft #6707
  }
}

$ java -jar /var/tmp/checkstyle-8.32-all.jar -c /google_checks.xml Test.java
Starting audit...
[WARN] /var/tmp/Test.java:31:21: '...' is preceded with whitespace. [NoWhitespaceBefore]
[WARN] /var/tmp/Test.java:37:30: '...' is preceded with whitespace. [NoWhitespaceBefore]
Audit done.

Metadata

Metadata

Assignees

No one assigned

    Labels

    approvedbugfalse negativeissues where check should place violations on code, but does not

    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