Specialized condition
This commit is contained in:
@ -28,7 +28,7 @@ import net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence;
|
|||||||
* @author acaplan
|
* @author acaplan
|
||||||
*/
|
*/
|
||||||
public class InefficientEmptyStringCheckRule extends AbstractInefficientZeroCheck {
|
public class InefficientEmptyStringCheckRule extends AbstractInefficientZeroCheck {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isTargetMethod(JavaNameOccurrence occ) {
|
public boolean isTargetMethod(JavaNameOccurrence occ) {
|
||||||
if (occ.getNameForWhichThisIsAQualifier() != null
|
if (occ.getNameForWhichThisIsAQualifier() != null
|
||||||
@ -40,28 +40,28 @@ public class InefficientEmptyStringCheckRule extends AbstractInefficientZeroChec
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean appliesToClassName(String name) {
|
public boolean appliesToClassName(String name) {
|
||||||
return "String".equals(name);
|
return "String".equals(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object visit(ASTPrimaryExpression node, Object data) {
|
public Object visit(ASTPrimaryExpression node, Object data) {
|
||||||
|
|
||||||
if (node.jjtGetNumChildren() > 3) {
|
if (node.jjtGetNumChildren() > 3) {
|
||||||
// Check last suffix
|
// Check last suffix
|
||||||
if (!"isEmpty".equals(node.jjtGetChild(node.jjtGetNumChildren() - 2).getImage())) {
|
if (!"isEmpty".equals(node.jjtGetChild(node.jjtGetNumChildren() - 2).getImage())) {
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
Node prevCall = node.jjtGetChild(node.jjtGetNumChildren() - 4);
|
Node prevCall = node.jjtGetChild(node.jjtGetNumChildren() - 4);
|
||||||
String target = prevCall.jjtGetNumChildren() > 0 ? prevCall.jjtGetChild(0).getImage() : prevCall.getImage();
|
String target = prevCall.jjtGetNumChildren() > 0 ? prevCall.jjtGetChild(0).getImage() : prevCall.getImage();
|
||||||
if (target != null && target.indexOf("trim") != -1) {
|
if (target != null && "trim".equals(target) || target.endsWith(".trim")) {
|
||||||
addViolation(data, node);
|
addViolation(data, node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user