use of net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix in project pmd by pmd.
the class UnnecessaryCaseChangeRule method visit.
public Object visit(ASTPrimaryExpression exp, Object data) {
int n = exp.jjtGetNumChildren();
if (n < 4) {
return data;
}
int first = getBadPrefixOrNull(exp, n);
if (first == -1) {
return data;
}
String second = getBadSuffixOrNull(exp, first + 2);
if (second == null) {
return data;
}
if (!(exp.jjtGetChild(first + 1) instanceof ASTPrimarySuffix)) {
return data;
}
ASTPrimarySuffix methodCall = (ASTPrimarySuffix) exp.jjtGetChild(first + 1);
if (!methodCall.isArguments() || methodCall.getArgumentCount() > 0) {
return data;
}
addViolation(data, exp);
return data;
}
use of net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix in project pmd by pmd.
the class UnnecessaryCaseChangeRule method getBadPrefixOrNull.
private int getBadPrefixOrNull(ASTPrimaryExpression exp, int childrenCount) {
// verify PrimaryPrefix/Name[ends-with(@Image, 'toUpperCase']
for (int i = 0; i < childrenCount - 3; i++) {
Node child = exp.jjtGetChild(i);
String image;
if (child instanceof ASTPrimaryPrefix) {
if (child.jjtGetNumChildren() != 1 || !(child.jjtGetChild(0) instanceof ASTName)) {
continue;
}
ASTName name = (ASTName) child.jjtGetChild(0);
image = name.getImage();
} else if (child instanceof ASTPrimarySuffix) {
image = ((ASTPrimarySuffix) child).getImage();
} else {
continue;
}
if (image == null || !(image.endsWith("toUpperCase") || image.endsWith("toLowerCase"))) {
continue;
} else {
return i;
}
}
return -1;
}
use of net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix in project pmd by pmd.
the class UselessOverridingMethodRule method visit.
@Override
public Object visit(ASTMethodDeclaration node, Object data) {
// them.
if (node.isAbstract() || node.isFinal() || node.isNative() || node.isSynchronized()) {
return super.visit(node, data);
}
// implement them anyway ( see bug 1522517)
if (CLONE.equals(node.getMethodName()) && node.isPublic() && !this.hasArguments(node) && this.isMethodType(node, OBJECT) && this.isMethodThrowingType(node, exceptions)) {
return super.visit(node, data);
}
ASTBlock block = node.getBlock();
if (block == null) {
return super.visit(node, data);
}
// Only process functions with one BlockStatement
if (block.jjtGetNumChildren() != 1 || block.findDescendantsOfType(ASTStatement.class).size() != 1) {
return super.visit(node, data);
}
Node statement = block.jjtGetChild(0).jjtGetChild(0);
if (statement.jjtGetChild(0).jjtGetNumChildren() == 0) {
// skips empty return statements
return data;
}
Node statementGrandChild = statement.jjtGetChild(0).jjtGetChild(0);
ASTPrimaryExpression primaryExpression;
if (statementGrandChild instanceof ASTPrimaryExpression) {
primaryExpression = (ASTPrimaryExpression) statementGrandChild;
} else {
List<ASTPrimaryExpression> primaryExpressions = findFirstDegreeChildrenOfType(statementGrandChild, ASTPrimaryExpression.class);
if (primaryExpressions.size() != 1) {
return super.visit(node, data);
}
primaryExpression = primaryExpressions.get(0);
}
ASTPrimaryPrefix primaryPrefix = findFirstDegreeChildrenOfType(primaryExpression, ASTPrimaryPrefix.class).get(0);
if (!primaryPrefix.usesSuperModifier()) {
return super.visit(node, data);
}
List<ASTPrimarySuffix> primarySuffixList = findFirstDegreeChildrenOfType(primaryExpression, ASTPrimarySuffix.class);
if (primarySuffixList.size() != 2) {
// extra method call on result of super method
return super.visit(node, data);
}
ASTMethodDeclarator methodDeclarator = findFirstDegreeChildrenOfType(node, ASTMethodDeclarator.class).get(0);
ASTPrimarySuffix primarySuffix = primarySuffixList.get(0);
if (!primarySuffix.hasImageEqualTo(methodDeclarator.getImage())) {
return super.visit(node, data);
}
// Process arguments
primarySuffix = primarySuffixList.get(1);
ASTArguments arguments = (ASTArguments) primarySuffix.jjtGetChild(0);
ASTFormalParameters formalParameters = (ASTFormalParameters) methodDeclarator.jjtGetChild(0);
if (formalParameters.jjtGetNumChildren() != arguments.jjtGetNumChildren()) {
return super.visit(node, data);
}
if (!ignoreAnnotations) {
ASTClassOrInterfaceBodyDeclaration parent = (ASTClassOrInterfaceBodyDeclaration) node.jjtGetParent();
for (int i = 0; i < parent.jjtGetNumChildren(); i++) {
Node n = parent.jjtGetChild(i);
if (n instanceof ASTAnnotation) {
if (n.jjtGetChild(0) instanceof ASTMarkerAnnotation) {
// @Override is ignored
if ("Override".equals(((ASTName) n.jjtGetChild(0).jjtGetChild(0)).getImage())) {
continue;
}
}
return super.visit(node, data);
}
}
}
if (arguments.jjtGetNumChildren() == 0) {
addViolation(data, node, getMessage());
} else {
ASTArgumentList argumentList = (ASTArgumentList) arguments.jjtGetChild(0);
for (int i = 0; i < argumentList.jjtGetNumChildren(); i++) {
Node expressionChild = argumentList.jjtGetChild(i).jjtGetChild(0);
if (!(expressionChild instanceof ASTPrimaryExpression) || expressionChild.jjtGetNumChildren() != 1) {
// The arguments are not simply passed through
return super.visit(node, data);
}
ASTPrimaryExpression argumentPrimaryExpression = (ASTPrimaryExpression) expressionChild;
ASTPrimaryPrefix argumentPrimaryPrefix = (ASTPrimaryPrefix) argumentPrimaryExpression.jjtGetChild(0);
if (argumentPrimaryPrefix.jjtGetNumChildren() == 0) {
// The arguments are not simply passed through (using "this" for instance)
return super.visit(node, data);
}
Node argumentPrimaryPrefixChild = argumentPrimaryPrefix.jjtGetChild(0);
if (!(argumentPrimaryPrefixChild instanceof ASTName)) {
// The arguments are not simply passed through
return super.visit(node, data);
}
if (formalParameters.jjtGetNumChildren() < i + 1) {
// different number of args
return super.visit(node, data);
}
ASTName argumentName = (ASTName) argumentPrimaryPrefixChild;
ASTFormalParameter formalParameter = (ASTFormalParameter) formalParameters.jjtGetChild(i);
ASTVariableDeclaratorId variableId = findFirstDegreeChildrenOfType(formalParameter, ASTVariableDeclaratorId.class).get(0);
if (!argumentName.hasImageEqualTo(variableId.getImage())) {
// The arguments are not simply passed through
return super.visit(node, data);
}
}
// All arguments are passed through directly
addViolation(data, node, getMessage());
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix in project pmd by pmd.
the class UnusedPrivateFieldRule method usedInOuter.
private boolean usedInOuter(NameDeclaration decl, JavaNode body) {
List<ASTClassOrInterfaceBodyDeclaration> classOrInterfaceBodyDeclarations = body.findChildrenOfType(ASTClassOrInterfaceBodyDeclaration.class);
List<ASTEnumConstant> enumConstants = body.findChildrenOfType(ASTEnumConstant.class);
List<AbstractJavaNode> nodes = new ArrayList<>();
nodes.addAll(classOrInterfaceBodyDeclarations);
nodes.addAll(enumConstants);
for (AbstractJavaNode node : nodes) {
for (ASTPrimarySuffix primarySuffix : node.findDescendantsOfType(ASTPrimarySuffix.class, true)) {
if (decl.getImage().equals(primarySuffix.getImage())) {
// No violation
return true;
}
}
for (ASTPrimaryPrefix primaryPrefix : node.findDescendantsOfType(ASTPrimaryPrefix.class, true)) {
ASTName name = primaryPrefix.getFirstDescendantOfType(ASTName.class);
if (name != null) {
for (String id : name.getImage().split("\\.")) {
if (id.equals(decl.getImage())) {
// No violation
return true;
}
}
}
}
}
return false;
}
Aggregations