Fix some false positives in UnusedPrivateField

This commit is contained in:
Roman
2015-02-15 11:41:05 -08:00
parent c44d94416d
commit 070a7c992f
2 changed files with 47 additions and 17 deletions

View File

@ -80,26 +80,22 @@ public class UnusedPrivateFieldRule extends AbstractJavaRule {
nodes.addAll(enumConstants);
for (JavaNode node : nodes) {
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
if (node.jjtGetChild(i) instanceof ASTClassOrInterfaceDeclaration) {
continue; // Skip other inner classes
List<ASTPrimarySuffix> primarySuffixes = node.findDescendantsOfType(ASTPrimarySuffix.class);
for (ASTPrimarySuffix primarySuffix : primarySuffixes) {
if (decl.getImage().equals(primarySuffix.getImage())) {
return true; // No violation
}
}
List<ASTPrimarySuffix> primarySuffixes = node
.findDescendantsOfType(ASTPrimarySuffix.class);
for (ASTPrimarySuffix primarySuffix : primarySuffixes) {
if (decl.getImage().equals(primarySuffix.getImage())) {
return true; // No violation
}
}
List<ASTPrimaryPrefix> primaryPrefixes = node.findDescendantsOfType(ASTPrimaryPrefix.class);
for (ASTPrimaryPrefix primaryPrefix : primaryPrefixes) {
ASTName name = primaryPrefix.getFirstDescendantOfType(ASTName.class);
List<ASTPrimaryPrefix> primaryPrefixes = node
.findDescendantsOfType(ASTPrimaryPrefix.class);
for (ASTPrimaryPrefix primaryPrefix : primaryPrefixes) {
ASTName name = primaryPrefix.getFirstDescendantOfType(ASTName.class);
if (name != null && name.getImage().endsWith(decl.getImage())) {
return true; // No violation
if (name != null) {
for (String id : name.getImage().split("\\.")) {
if (id.equals(decl.getImage())) {
return true; // No violation
}
}
}
}

View File

@ -391,4 +391,38 @@ public enum Operation
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
private field in inner class accessed as method call
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class InnerPrivateFieldCall {
int method() {
return Inner.FIELD.length();
}
static class Inner {
private static final String FIELD = "";
}
}
]]></code>
</test-code>
<test-code>
<description><![CDATA[
private field in inner class accessed by another inner class
]]></description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class InnerPrivateFieldInAnotherInner {
static class InnerUsing {
int method() {
return InnerDeclaring.INNER_FIELD;
}
}
static class InnerDeclaring {
private static int INNER_FIELD;
}
}
]]></code>
</test-code>
</test-data>