Skip to content

Commit

Permalink
Merge branch 'unused-private-field-inner' of https://github.com/rsalv…
Browse files Browse the repository at this point in the history
…ador/pmd into rsalvador-unused-private-field-inner
  • Loading branch information
adangel committed Feb 19, 2015
2 parents 299d167 + 070a7c9 commit 6b7846a
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
Expand Up @@ -80,26 +80,22 @@ private boolean usedInOuter(NameDeclaration decl, JavaNode body) {
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
}
}
}
}
Expand Down
Expand Up @@ -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>

0 comments on commit 6b7846a

Please sign in to comment.