forked from phoedos/pmd
Checked for method primitive return type
This commit is contained in:
@ -14,6 +14,7 @@ import net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix;
|
|||||||
import net.sourceforge.pmd.lang.java.ast.ASTReferenceType;
|
import net.sourceforge.pmd.lang.java.ast.ASTReferenceType;
|
||||||
import net.sourceforge.pmd.lang.java.ast.ASTType;
|
import net.sourceforge.pmd.lang.java.ast.ASTType;
|
||||||
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
|
import net.sourceforge.pmd.lang.java.rule.AbstractJavaRule;
|
||||||
|
import net.sourceforge.pmd.lang.java.symboltable.MethodNameDeclaration;
|
||||||
import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration;
|
import net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration;
|
||||||
import net.sourceforge.pmd.lang.symboltable.NameDeclaration;
|
import net.sourceforge.pmd.lang.symboltable.NameDeclaration;
|
||||||
|
|
||||||
@ -54,7 +55,7 @@ public class UselessStringValueOfRule extends AbstractJavaRule {
|
|||||||
&& "+".equals(gp.getImage())) {
|
&& "+".equals(gp.getImage())) {
|
||||||
boolean ok = false;
|
boolean ok = false;
|
||||||
if (gp.getChild(0) == parent) {
|
if (gp.getChild(0) == parent) {
|
||||||
return super.visit(node, data);
|
ok = !isPrimitive(gp.getChild(1));
|
||||||
} else {
|
} else {
|
||||||
for (int i = 0; !ok && gp.getChild(i) != parent; i++) {
|
for (int i = 0; !ok && gp.getChild(i) != parent; i++) {
|
||||||
ok = !isPrimitive(gp.getChild(i));
|
ok = !isPrimitive(gp.getChild(i));
|
||||||
@ -71,7 +72,7 @@ public class UselessStringValueOfRule extends AbstractJavaRule {
|
|||||||
|
|
||||||
private static boolean isPrimitive(Node parent) {
|
private static boolean isPrimitive(Node parent) {
|
||||||
boolean result = false;
|
boolean result = false;
|
||||||
if (parent instanceof ASTPrimaryExpression && parent.getNumChildren() == 1) {
|
if (parent instanceof ASTPrimaryExpression && parent.getNumChildren() > 0) {
|
||||||
Node child = parent.getChild(0);
|
Node child = parent.getChild(0);
|
||||||
if (child instanceof ASTPrimaryPrefix && child.getNumChildren() == 1) {
|
if (child instanceof ASTPrimaryPrefix && child.getNumChildren() == 1) {
|
||||||
Node gc = child.getChild(0);
|
Node gc = child.getChild(0);
|
||||||
@ -80,6 +81,9 @@ public class UselessStringValueOfRule extends AbstractJavaRule {
|
|||||||
NameDeclaration nd = name.getNameDeclaration();
|
NameDeclaration nd = name.getNameDeclaration();
|
||||||
if (nd instanceof VariableNameDeclaration && ((VariableNameDeclaration) nd).isPrimitiveType()) {
|
if (nd instanceof VariableNameDeclaration && ((VariableNameDeclaration) nd).isPrimitiveType()) {
|
||||||
result = true;
|
result = true;
|
||||||
|
} else if (nd instanceof MethodNameDeclaration
|
||||||
|
&& ((MethodNameDeclaration) nd).isPrimitiveReturnType()) {
|
||||||
|
result = true;
|
||||||
}
|
}
|
||||||
} else if (gc instanceof ASTLiteral) {
|
} else if (gc instanceof ASTLiteral) {
|
||||||
result = !((ASTLiteral) gc).isStringLiteral();
|
result = !((ASTLiteral) gc).isStringLiteral();
|
||||||
|
@ -32,6 +32,11 @@ public class MethodNameDeclaration extends AbstractNameDeclaration {
|
|||||||
return p.isVarargs();
|
return p.isVarargs();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isPrimitiveReturnType() {
|
||||||
|
return getMethodNameDeclaratorNode().getParent().getResultType().getChild(0)
|
||||||
|
.getChild(0) instanceof ASTPrimitiveType;
|
||||||
|
}
|
||||||
|
|
||||||
public ASTMethodDeclarator getMethodNameDeclaratorNode() {
|
public ASTMethodDeclarator getMethodNameDeclaratorNode() {
|
||||||
return (ASTMethodDeclarator) node;
|
return (ASTMethodDeclarator) node;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user