Menu

#1436 [java] UnnecessaryFullyQualifiedName: false positive on clashing static imports with enums

PMD-5.3.6
closed
java (55)
PMD
4-Minor
Bug
5.3.4
Import Statements / UnnecessaryFullyQualifiedName
2016-11-15
2015-10-27
Thrawn
No

The UnnecessaryFullyQualifiedName rule fires on a class that performs static imports from several enums (so that it can use the enum constants directly), and then references the values() method of each enum using its qualified name.

It is not possible to use the unqualified values() method, since the different enums all provide it.

Discussion

  • Andreas Dangel

    Andreas Dangel - 2015-11-20

    I've fixed it to ignore the enum case. However, this only works, if PMD can detect the actual type of the statically imported class. This means, typeresolution/auxclasspath needs to be enabled and the compiled classes need to be on the classpath during PMD execution.

    The fix will be included in PMD 5.3.6 and later.

    Commit: https://github.com/pmd/pmd/commit/b4c2a517811f12528fb1cd81a45c465aef81fd27

     
  • Andreas Dangel

    Andreas Dangel - 2015-11-20
    • summary: UnnecessaryFullyQualifiedName false positive on clashing static imports --> UnnecessaryFullyQualifiedName false positive on clashing static imports with enums
    • status: open --> closed
    • assigned_to: Andreas Dangel
    • Milestone: New Tickets --> PMD-5.3.6
     
  • Andreas Dangel

    Andreas Dangel - 2016-06-25
    • labels: static imports qualified --> java
    • summary: UnnecessaryFullyQualifiedName false positive on clashing static imports with enums --> [java] UnnecessaryFullyQualifiedName: false positive on clashing static imports with enums
     
  • Juan Sotuyo

    Juan Sotuyo - 2016-11-15

    This issue is actually much broader than enums. Any static import can cause collisions that require an explicit access, for instance:

    import static org.mockito.Mockito;
    import static org.mockito.Mockito.*;
    import static org.powermock.api.mockito.PowerMockito.*;
    
    public class Foo {
      private Bar bar = Mockito.mock(Bar.class); // doing simply mock(Bar.class) is ambiguous (compile error)
    

    Also could happen if PowerMockito was not a star import:

    import static org.mockito.Mockito;
    import static org.mockito.Mockito.*;
    import static org.powermock.api.mockito.PowerMockito.mock;
    
    public class Foo {
      private Bar bar = Mockito.mock(Bar.class); // doing simply mock(Bar.class) would use a differen method than intended
    

    I'm opening a new issue to track this, and reworking this rule.

     

Log in to post a comment.