A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://github.com/pmd/pmd/issues/4174 below:

[java] MissingStaticMethodInNonInstantiatableClass does not consider nested builder class · Issue #4174 · pmd/pmd · GitHub

Affects PMD Version: 6.50

Rule: MissingStaticMethodInNonInstantiatableClass

Description: A private final class can be instantiated using a nested static class which might return an interface with the class as the implementation. I think the problem is that when an interface is returned PMD does not understand that it is actually returning an instance of the same class.

Code Sample demonstrating the issue:

file: GenericTest.java

public interface GenericTest {
  void run();
}

file: TestPrivateClassWithFactory.java

public final class TestPrivateClassWithFactory implements GenericTest {
  private final boolean shouldFail;

  private TestPrivateClassWithFactory(TestStep step) {
    this.shouldFail = step.shouldFail;
  }

  public void run() {
    if (shouldFail) {
      throw new AssertionError("fail");
    }
  }

  public static final class TestStep {
    private boolean shouldFail = false;

    public TestStep shouldFail() {
      this.shouldFail = true;
      return this;
    }

    public GenericTest newTest() {
      return new TestPrivateClassWithFactory(this);
    }
  }
}

Expected outcome: PMD does not complain about MissingStaticMethodInNonInstantiatableClass

PMD reports a violation at line 1 column 14 in TestPrivateClassWithFactory.java but that's wrong. That's a false positive.

Running PMD through: Maven and also IntelliJ's PMD-IDEA plugin


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4