A RetroSearch Logo

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

Search Query:

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

[java] UnusedPrivateMethod false-positive / method reference in combination with custom object · Issue #4985 · pmd/pmd · GitHub

Affects PMD Version: 7.1.0

Rule: UnusedPrivateMethod

Description:

The private static method int foo(String s) is incorrectly recognized as an unused private method. The rule applies in the upper block of the code snippet, but not in the lower block - as method int foo2(String s) is not detected as false positive..

It is difficult for me to give a comprehensible explanation (which is also reflected in the title of the issue 😅), but it looks as if the rule applies incorrectly if the method is called via method reference and a custom object is used in the structure of the stream.

Code Sample demonstrating the issue:

Full code example can be found here: https://github.com/kesslerj/pmd-unused-private-method-issue-example-project .

public class Main {

  public static void main(String[] args) {
    System.out.println("list");

    // usage of method reference in combination with a custom object leads to a false positive of PMD.UnusedPrivateMethod
    List.of(new StringWrapper().getString())
        .stream()

        .map(Main::foo)
        .filter(Objects::nonNull)
        .toList();

    // no false positive .. :/
    List.of("s")
        .stream()
        .map(Main::foo2)
        .filter(Objects::nonNull)
        .toList();
  }

  private static int foo(String s) {
    return s.length();
  }

  private static int foo2(String s) {
    return s.length();
  }

}
public class StringWrapper {

  public String getString(){
    return "s";
  }
}

Expected outcome:

PMD reports a violation at line 27, but that's wrong. That's a false positive.

[INFO] PMD Failure: de.jk.examples.pmd.demo.Main:27 Rule:UnusedPrivateMethod Priority:3 Avoid unused private methods such as 'foo(String)'..

Running PMD through: Maven


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