Fix some false positives in UnusedPrivateField
This commit is contained in:
@ -80,30 +80,26 @@ public class UnusedPrivateFieldRule extends AbstractJavaRule {
|
|||||||
nodes.addAll(enumConstants);
|
nodes.addAll(enumConstants);
|
||||||
|
|
||||||
for (JavaNode node : nodes) {
|
for (JavaNode node : nodes) {
|
||||||
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
|
List<ASTPrimarySuffix> primarySuffixes = node.findDescendantsOfType(ASTPrimarySuffix.class);
|
||||||
if (node.jjtGetChild(i) instanceof ASTClassOrInterfaceDeclaration) {
|
|
||||||
continue; // Skip other inner classes
|
|
||||||
}
|
|
||||||
|
|
||||||
List<ASTPrimarySuffix> primarySuffixes = node
|
|
||||||
.findDescendantsOfType(ASTPrimarySuffix.class);
|
|
||||||
for (ASTPrimarySuffix primarySuffix : primarySuffixes) {
|
for (ASTPrimarySuffix primarySuffix : primarySuffixes) {
|
||||||
if (decl.getImage().equals(primarySuffix.getImage())) {
|
if (decl.getImage().equals(primarySuffix.getImage())) {
|
||||||
return true; // No violation
|
return true; // No violation
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
List<ASTPrimaryPrefix> primaryPrefixes = node
|
List<ASTPrimaryPrefix> primaryPrefixes = node.findDescendantsOfType(ASTPrimaryPrefix.class);
|
||||||
.findDescendantsOfType(ASTPrimaryPrefix.class);
|
|
||||||
for (ASTPrimaryPrefix primaryPrefix : primaryPrefixes) {
|
for (ASTPrimaryPrefix primaryPrefix : primaryPrefixes) {
|
||||||
ASTName name = primaryPrefix.getFirstDescendantOfType(ASTName.class);
|
ASTName name = primaryPrefix.getFirstDescendantOfType(ASTName.class);
|
||||||
|
|
||||||
if (name != null && name.getImage().endsWith(decl.getImage())) {
|
if (name != null) {
|
||||||
|
for (String id : name.getImage().split("\\.")) {
|
||||||
|
if (id.equals(decl.getImage())) {
|
||||||
return true; // No violation
|
return true; // No violation
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -388,6 +388,40 @@ public enum Operation
|
|||||||
private static final int PRIVATE_STAY = 0;
|
private static final int PRIVATE_STAY = 0;
|
||||||
private static final int PRIVATE_MOVE = 1;
|
private static final int PRIVATE_MOVE = 1;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
]]></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>
|
]]></code>
|
||||||
</test-code>
|
</test-code>
|
||||||
|
Reference in New Issue
Block a user