Search code examples
javaxmlpmd

PMD: Why there are 2 rules called UnusedImports and which use different java classes?


As I'm cleaning the PMD file we are using, I was surprised to see that in PMD5, there are 2 rules called UnusedImports : - One from imports.xml - One from typeresolution.xml

The description isn't exactly the same but the meaning of it seems identical. So does anyone know why 2 rules and why the oldest one hasn't been deprecated if it couldn't handle static imports ?

Same question for LooseCoupling (coupling.xml & typeresolution.xml), CloneMethodMustImplementCloneable (clone.xml & typeresolution.xml), SignatureDeclareThrowsException (strictexception.xml & typeresolution.xml.


Solution

  • The typeresolution ruleset was a temporary ruleset created as the type resolution support matured in PMD (that is, the ability for PMD to use actual classes instead of simple Strings to determine types of variables and objects). This was done in fear that the experimental feature was introducing issues affecting users of the stable (old) rules' implementations.

    Nowadays however, given that you provide all dependencies in the auxclasspath for PMD, the typeresolution rules should provide better results (can avoid lots of false positives / false negatives). It also means that analysis is somewhat more costly.

    Over the last several versions (5.1.0 and up), PMD has improved a lot on type resolution, both in it's accuracy and it's performance, to the point where the latest versions of both Gradle and Maven plugins currently enable it by default and make sure PMD can use type resolution ruleset out of the box.

    There is currently a Google Summer of Code project underway (which I personally mentor), to finish all the missing pieces of type resolution support, and with it, remove the typeresolution ruleset altogether, simply overriding all rules with their type resolution capable alternative when available.

    Bottom line, typeresolution rules are equivalent, but better. Using them with a current version of Maven / Gradle is automatic. Doing it with Ant / CLI needs extra configuration.