Skip to content

Commit

Permalink
#1228 UnusedPrivateMethod returns false positives
Browse files Browse the repository at this point in the history
  • Loading branch information
adangel committed Jul 28, 2014
1 parent c730c15 commit 4cef35c
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,18 @@
import net.sourceforge.pmd.lang.symboltable.NameDeclaration;
import net.sourceforge.pmd.lang.symboltable.NameOccurrence;

/**
* This rule detects private methods, that are not used and can therefore be deleted.
*/
public class UnusedPrivateMethodRule extends AbstractJavaRule {


/**
* Visit each method declaration.
* @param node the method declaration
* @param data data - rule context
* @return data
*/
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
if (node.isInterface()) {
return data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,24 +45,24 @@ public boolean isLambdaTypelessParameter() {
}

public boolean isPrimitiveType() {
return !isLambdaTypelessParameter() && getAccessNodeParent().jjtGetChild(0).jjtGetChild(0) instanceof ASTPrimitiveType;
return !isLambdaTypelessParameter() && getAccessNodeParent().getFirstChildOfType(ASTType.class).jjtGetChild(0) instanceof ASTPrimitiveType;
}

public String getTypeImage() {
if (isPrimitiveType()) {
return getAccessNodeParent().jjtGetChild(0).jjtGetChild(0).getImage();
}
if (!isLambdaTypelessParameter()) {
return getAccessNodeParent().jjtGetChild(0).jjtGetChild(0).jjtGetChild(0).getImage();
} else
return null;
if (isPrimitiveType()) {
return getAccessNodeParent().getFirstChildOfType(ASTType.class).jjtGetChild(0).getImage();
}
if (!isLambdaTypelessParameter()) {
return getAccessNodeParent().getFirstChildOfType(ASTType.class).jjtGetChild(0).jjtGetChild(0).getImage();
}
return null;
}

/**
* Note that an array of primitive types (int[]) is a reference type.
*/
public boolean isReferenceType() {
return !isLambdaTypelessParameter() && getAccessNodeParent().jjtGetChild(0).jjtGetChild(0) instanceof ASTReferenceType;
return !isLambdaTypelessParameter() && getAccessNodeParent().getFirstChildOfType(ASTType.class).jjtGetChild(0) instanceof ASTReferenceType;
}

public AccessNode getAccessNodeParent() {
Expand Down
2 changes: 2 additions & 0 deletions pmd/src/site/markdown/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

**Bugfixes:**

* [#1228](http://sourceforge.net/p/pmd/bugs/1228/): UnusedPrivateMethod returns false positives

**Feature Requests and Improvements:**

**Pull requests:**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,8 @@ public class Foo {
}
]]></code>
</test-code>
<!--test-code>
<description><![CDATA[
631681, call to instance of self, received from another class
]]></description>
<test-code regressionTest="false">
<description>#46 False +: Unused private field: call to instance of self, received from another class</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
Expand All @@ -313,7 +311,7 @@ public class Foo {
private void buz() {}
}
]]></code>
</test-code-->
</test-code>
<test-code>
<description><![CDATA[
Calling one arg varargs method
Expand Down Expand Up @@ -494,6 +492,28 @@ public class Foo {
private void setInnateFilterFields(MyObject filter, HttpServletRequest request) {
//add values to filter object
}
}
]]></code>
</test-code>
<test-code>
<description>#1228 UnusedPrivateMethod returns false positives (3)</description>
<expected-problems>0</expected-problems>
<code><![CDATA[
public class Foo {
String VIEW = "foo";
@RequestMapping(method = RequestMethod.GET, value = URL_MAPPINGS.BULK_ACTION_URL)
public ModelAndView viewEntry(Model model, HttpServletRequest request, @RequestParam(value = "fiscalYear", required = true) int fy) {
return view(model, VIEW, fy);
}
private ModelAndView view(Model model, String view, int fy) {
//add values to the model
model.addAttribute(FIELDS.FISCAL_YEAR, fy);
//return the correct view
return new ModelAndView(view, MVC_CONSTANTS.MODEL_KEY, model.asMap());
}
}
]]></code>
</test-code>
Expand Down

0 comments on commit 4cef35c

Please sign in to comment.