Menu

#1318 UnusedCode/UnusedPrivateMethod False Alarm

New Tickets
more-info-needed
None
PMD
3-Major
Bug
5.2.3
2016-06-25
2015-03-03
dmgob
No

Example that shows the bug:

Class 1:

public class Parent {  public void parentMethod() {} }

Class 2:

public class Child extends Parent { public void childMethod() {} }

Class 3:

public class Client {

  public void clientMethod() {
    final Child c = new Child();
    privateMethod1( c );
    privateMethod2( c );
  }

  private void privateMethod1( Parent p ) {
    p.parentMethod();
  }

  private void privateMethod2( Child c ) {
    c.childMethod();
  }

}

Compiled with Java 6.

The problem is that "privateMethod1", the one invoked with a var of the Child
type but declared with the supertype, is flagged as "unused" (wrong). The method
privateMethod2, for which the types matched, is not flagged (correct).

Discussion

  • Andreas Dangel

    Andreas Dangel - 2015-03-16
    • Description has changed:

    Diff:

    --- old
    +++ new
    @@ -3,31 +3,31 @@
    
     Class 1:
    
    -public class Parent {  public void parentMethod() {} }
    +    public class Parent {  public void parentMethod() {} }
    
     Class 2:
    
    -public class Child extends Parent { public void childMethod() {} }
    +    public class Child extends Parent { public void childMethod() {} }
    
     Class 3:
    
    -public class Client {
    -
    -  public void clientMethod() {
    -    final Child c = new Child();
    -    privateMethod1( c );
    -    privateMethod2( c );
    -  }
    -
    -  private void privateMethod1( Parent p ) {
    -    p.parentMethod();
    -  }
    -
    -  private void privateMethod2( Child c ) {
    -    c.childMethod();
    -  }
    -
    -}
    +    public class Client {
    +    
    +      public void clientMethod() {
    +        final Child c = new Child();
    +        privateMethod1( c );
    +        privateMethod2( c );
    +      }
    +    
    +      private void privateMethod1( Parent p ) {
    +        p.parentMethod();
    +      }
    +    
    +      private void privateMethod2( Child c ) {
    +        c.childMethod();
    +      }
    +    
    +    }
    
     Compiled with Java 6.
    
    • status: open --> more-info-needed
    • assigned_to: Andreas Dangel
    • Milestone: New Tickets --> PMD-Next
     
  • Andreas Dangel

    Andreas Dangel - 2015-03-16

    Thanks for the bug report.
    I can reproduce your example with PMD 5.2.1 (without auxclasspath option) - but it doesn't show up anymore with PMD 5.2.2 nor with 5.2.3 (regardless of the auxclasspath option).
    Can you verify, which version of PMD you are using?
    Regards,
    Andreas

     
  • Andreas Dangel

    Andreas Dangel - 2015-04-01
    • Milestone: PMD-5.3.0 --> PMD-5.3.1
     
  • Andreas Dangel

    Andreas Dangel - 2015-04-20
    • Milestone: PMD-5.3.1 --> PMD-5.4.0
     
  • Andreas Dangel

    Andreas Dangel - 2015-10-04
    • Milestone: PMD-5.4.0 --> PMD-5.4.1
    • Ruleset / Rule: -->
     
  • Andreas Dangel

    Andreas Dangel - 2015-12-04
    • Milestone: PMD-5.4.1 --> PMD-5.5.0
     
  • Andreas Dangel

    Andreas Dangel - 2016-06-25
    • Milestone: PMD-5.5.0 --> New Tickets
     

Log in to post a comment.