#240 Another Bug In UnusedPrivateMethod

release_3.5
closed
Rick Giles
None
5
2012-10-10
2004-07-21
Allan
No

Another bug in UnusedPrivateMethod
When using the following check :

<module name="Checker">
<module name="TreeWalker">
<module name="usage.UnusedPrivateMethod"/>
</module>
</module>

on the following code :


// ArrayMap.java

package pkg;

import java.util.HashMap;

public class ArrayMap extends HashMap
{
public ArrayMap()
{
}
}


// FixedMapImpl.java

package pkg;

import java.util.Map;

public class FixedMapImpl extends ArrayMap
{
public void publicTest()
{
FixedMapImpl fmi = new FixedMapImpl();
fmi.privateTest(this);
}

private void privateTest(final Map p_map)
{
}

}


I get the error :
FixedMapImpl.java:14:18: Unused private method
'privateTest'.

This only seems to occur once I introduce the ArrayMap
class in the hierarchy. If I have FixedmapImpl extend
HashMap, I do not get this error.

Discussion

  • Aaron Jewell
    Aaron Jewell
    2004-07-23

    Logged In: YES
    user_id=1089486

    The check in general seems very unreliable: far too many
    false positives.

    I give false warnings of unused private method in just about
    any case that looks like the following:

    private void privateUsedMethod(Foo param) {...}

    private Foo methodReturningFoo() {...}

    private void callingMethod()
    {
    privateUsedMethod(methodReturningFoo());
    }

    privateUsedMethod() will be falsely reported as unused. The
    workaround is to store the return value into a local
    variable and pass the variable as the parameter. However,
    that's a lot of work when the codebase is 3/4 million lines.

     
  • Rick Giles
    Rick Giles
    2004-07-23

    Logged In: YES
    user_id=539926

    Re. atsjewell's followup.

    Checkstyle 3.4 doesn't give a false warning with

    class Foo
    {

    private void privateUsedMethod(Foo m)
    {
    }
    
    private Foo methodReturningFoo()
    {
        return new Foo();
    }
    
    public void callingMethod()
    {
        privateUsedMethod(methodReturningFoo());
    }
    

    }

    Please provide a checkstyle version number and a concrete
    example of a false warning.

     
  • Aaron Jewell
    Aaron Jewell
    2004-07-23

    Logged In: YES
    user_id=1089486

    I'm not sure how to submit an example seeing as there no
    link to send an attachment on this form.

    However, I am using version 3.4 of the checkstyle with
    version 1.6.2 beta of ant. I can not reproduce the false
    positives when running checkstyle with an ANT task. :(

    Using the Eclipse-Checkstyle plugin version 3.4.1 with
    version 3.0 of eclipse then I can reproduce the problem. I
    don't know if that makes it a problem with checkstyle or a
    problem with the plugin. But unless Mr Schneider opened the
    source and changed it I don't see why it would stop working
    just because it's being called from a plugin.

    At any rate there doesn't seem to be a log file produced
    when using the eclipse plugin but I do have a screenshot on
    my harddrive that shows the method be declared unused when
    in fact it is being called four times in the same class. It
    one example of many and they all occur under the conditions
    demonstrated. I could send you the "one" file along with the
    screenshot (if I knew how) but I doubt it would be much use
    because by the time all the imports got resolved I'd have to
    send you several thousand lines of proprietary code which
    ain't gonna happen. Sorry. It's not my code to send.

     
  • Rick Giles
    Rick Giles
    2004-07-25

    Logged In: YES
    user_id=539926

    Regarding the original bug report, the error is resolved by
    including pkg.ArrayMap in the classpath.

    Aaron, since checkstyle works as expected with an ANT task
    (and, presumably, at the command line), I think your problem
    should be reported as a plugin bug.