The DuplicateImportsRule handles static and normal imports absolutely the same way which is already wrong IMHO.
As the result, isDisambiguationImport method when it checks that a specific import can be a disambiguation checks if it can find a class of a given name. But in case of static imports, we are importing methods not classes.
As the result, this sequence of imports generates "duplicate import" warning:
import static org.hamcrest.Matchers.*; import static org.mockito.Matchers.any; import static org.mockito.Matchers.*;
although the last line is disambiguation because hamcrest.Matchers also has "any".
I would suggesthandling static imports separately from non-static ones. Or at the very least just ignore all static imports in DuplicateImportsRule.
Diff:
This will be fixed with the next version.
It uses type resolution to verify the existence of the static method - if it exists in both types, the rule won't trigger anymore.
Are you sure it is fixed?
I upgraded to version 3.4 maven-pmd-plugin and if I am not mistaken, it uses pmd-core 5.2.6 and the following block of imports still generates an error:
[INFO] PMD Failure: Xxx.java:26 Rule:DuplicateImports Priority:3 Avoid duplicate imports such as 'org.mockito.Matchers.any'.
Just by looking at the rule sources I am not sure I understand how it works
Why does it check classNameExists? Static imports are not about class names, they are about method names...
Last edit: Dimas 2015-04-22
Well, I hope it is fixed :)
As maven-pmd-plugin 3.4 uses only pmd 5.2.6 - it is not fixed there. The bug has been fixed with pmd 5.3 (you can see this at the milestone field of this issue). So, it should be fixed when the next maven-pmd-plugin version is released and uses pmd >= 5.3.0.