public class Foo {
private Foo fooField;
public void bar() {
Foo f = new Foo();
f.fooField.buz();
}
private void buz() {}
}
The problem is a more general one of resolving expressions
and types. This particular case could be solved by resolving
types within one compilation unit, but here's one that can't:
public class Foo {
private Foo fooField;
public void bar() {
Biz.getFoo().buz();
}
private void buz() {}
}
Unless we go outside the compilation unit and figure out that
Biz.getFoo() returns a Foo, we'll miss this usage of the
private method. Same goes for private fields, too.
So to catch these we need to do complete symbol resolution.
Tom
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
this is beyond the current capabilities of PMD, so I
would not mark it as a bug but an RFE.
I would err on the side of LESS hits, so I would mark
buz() as used. This way as PMD improves it catches more
cases.
Or perhaps their can be an overall PMD setting of vigorous
or cautious in which all Rules can either err on the side of
less misfires, or err on the side of more safety so to speak.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
=======================
Try running PMD's unused code ruleset on the current
jaxen (http://jaxen.codehaus.org/) code base. It pops
up several issues that appear to all be false positives
with respect to inner classes. For instance consider
this code:
public class XPathFunctionContext extends
SimpleFunctionContext
{
/ Singleton implementation.
*/
private static class Singleton
{
/ Singleton instance.
*/
private static XPathFunctionContext instance =
new XPathFunctionContext();
}
/* Retrieve the singleton instance.
*
* @return the singleton instance /
public static FunctionContext getInstance()
{
return Singleton.instance;
}
PMD complains that the instance field in the Singleton
class is unused, but that isn't true. There appear to
be simialr issues with method invocations between outer
and inner classes.
=======================
Thanks!
Tom
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
To fix that case, you need to know the return value of getFoo. This requires some advaned type resolution that is currently now available. So I'm going to leave this one alone for now.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
If it is known that PMD produces false positives on the "unused private field" check, and it cannot be fixed easily, why not remove the check from the suite then?
I think most modern compilers reports these problems (correctly) now anyways.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Logged In: YES
user_id=5159
This one is really going to require some compilerish symbol
tracking.
Logged In: YES
user_id=5159
A simpler example:
public class Foo {
private Foo fooField;
public void bar() {
Foo f = new Foo();
f.fooField.buz();
}
private void buz() {}
}
The problem is a more general one of resolving expressions
and types. This particular case could be solved by resolving
types within one compilation unit, but here's one that can't:
public class Foo {
private Foo fooField;
public void bar() {
Biz.getFoo().buz();
}
private void buz() {}
}
Unless we go outside the compilation unit and figure out that
Biz.getFoo() returns a Foo, we'll miss this usage of the
private method. Same goes for private fields, too.
So to catch these we need to do complete symbol resolution.
Tom
Logged In: YES
user_id=5159
Stefan Bodewig provided another example:
public class Foo {
private static class Inner {
private int bar;
}
public void buz() {
Inner inner = new Inner();
return inner.bar;
}
}
This should be resolvable, but will take some work...
Tom
Logged In: YES
user_id=280374
perhaps the code I have been working on will assist this.
it helps discern how methods are called.
Logged In: YES
user_id=280374
2 things.
this is beyond the current capabilities of PMD, so I
would not mark it as a bug but an RFE.
I would err on the side of LESS hits, so I would mark
buz() as used. This way as PMD improves it catches more
cases.
Or perhaps their can be an overall PMD setting of vigorous
or cautious in which all Rules can either err on the side of
less misfires, or err on the side of more safety so to speak.
Logged In: YES
user_id=273524
Same problem occurs for fields as for methods.
Scott
Logged In: YES
user_id=5159
Another variation on this theme:
"Avoid unused private methods is wrong in the following
context:
public class Converter {
private final static Converter converter = new
Converter();
public void doSomething(String s) {
Converter.converter.doSomethingImpl(s);
}
private void doSomethingImpl(String s) {
// do something
}
}
Logged In: YES
user_id=5159
Another case from elharo:
=======================
Try running PMD's unused code ruleset on the current
jaxen (http://jaxen.codehaus.org/) code base. It pops
up several issues that appear to all be false positives
with respect to inner classes. For instance consider
this code:
public class XPathFunctionContext extends
SimpleFunctionContext
{
/ Singleton implementation.
*/
private static class Singleton
{
/ Singleton instance.
*/
private static XPathFunctionContext instance =
new XPathFunctionContext();
}
/* Retrieve the singleton instance.
*
* @return the singleton instance
/
public static FunctionContext getInstance()
{
return Singleton.instance;
}
PMD complains that the instance field in the Singleton
class is unused, but that isn't true. There appear to
be simialr issues with method invocations between outer
and inner classes.
=======================
Thanks!
Tom
Logged In: YES
user_id=5159
Another case:
public class FilterConfigBean {
private Filter m_filter;
public void foo(FilterConfigBean configs) {
for (FilterConfigBean config : configs.values()) {
config.m_filter.init(config);
}
}
}
Logged In: YES
user_id=555114
Originator: NO
I fixed the inner class examples for UnusedPrivateField. I will look at UnusedPrivateMethod too.
Logged In: YES
user_id=555114
Originator: NO
Only this one is a real problem, the other cases have already been fixed:
public class Foo {
private Foo fooField;
public void bar() {
Biz.getFoo().buz();
}
private void buz() {}
}
Logged In: YES
user_id=555114
Originator: NO
To fix that case, you need to know the return value of getFoo. This requires some advaned type resolution that is currently now available. So I'm going to leave this one alone for now.
If it is known that PMD produces false positives on the "unused private field" check, and it cannot be fixed easily, why not remove the check from the suite then?
I think most modern compilers reports these problems (correctly) now anyways.