google / error-prone

Catch common Java mistakes as compile-time errors

Home Page:https://errorprone.info

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MethodCanBeStatic too aggressive in checking if method is private

anuraaga opened this issue · comments

Since the latest version of errorprone, I found MethodCanBeStatic often flags this sort of usage.

class Something {
  public static Something create() {
    Helper helper = "bar".equals(System.getProperty("foo")) ? new Helper1() : new Helper2();
    return new Something(helper);
  }

  private interface Helper {
    default void help() {}  // <--- Flagged as can be static, but it can't
  }

  private static class Helper1 implements Helper {}

  private static class Helper2 implements Helper {
    @Override public void help() { System.out.println("dive"); };
  }
}

The check seems to recognize the private interface Helper - if I remove private then the check passes. But since this interface is just an implementation detail of Something, private seems appropriate here.

If this behavior is actually desired, it would be nice if it were a different bug pattern so I could disable it as otherwise I'd generally like to keep MethodCanBeStatic on.