[java] UnnecessaryImport false positive for on-demand imports #5315
Closed
Description
Affects PMD Version:
7.7.0
Rule:
UnnecessaryImport
Description:
PMD reports an unnecessary on-demand import. However, if the import is removed the code does not compile
Code Sample demonstrating the issue:
https://github.com/lolo101/pmd-issue
package org.example;
import java.util.Map;
import java.util.Map.*; // this import is reported as unnecessary. However, class Entry is a nested class of Map and requires that import
import static java.util.stream.Collectors.toMap;
public class Main {
public static void main(String[] args) {
Map<String, String> map = Map.of("key1", "value1");
var weirdCopy = map.entrySet().stream().collect(toMap(Entry::getKey, Entry::getValue));
System.out.println(weirdCopy);
}
}
Expected outcome:
PMD reports a violation at line 4, but that's wrong. That's a false positive.
Running PMD through: Maven with plugin maven-pmd-plugin 3.26.0