use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.
the class ConstructorCallsOverridableMethodRule method getArgumentTypes.
private static List<String> getArgumentTypes(ASTArguments args) {
List<String> argumentTypes = new ArrayList<>();
ASTArgumentList argumentList = args.getFirstChildOfType(ASTArgumentList.class);
if (argumentList != null) {
for (int a = 0; a < argumentList.jjtGetNumChildren(); a++) {
Node expression = argumentList.jjtGetChild(a);
ASTPrimaryPrefix arg = expression.getFirstDescendantOfType(ASTPrimaryPrefix.class);
String type = "<unknown>";
if (arg != null && arg.jjtGetNumChildren() > 0) {
if (arg.jjtGetChild(0) instanceof ASTLiteral) {
ASTLiteral lit = (ASTLiteral) arg.jjtGetChild(0);
if (lit.isCharLiteral()) {
type = "char";
} else if (lit.isFloatLiteral()) {
type = "float";
} else if (lit.isIntLiteral()) {
type = "int";
} else if (lit.isStringLiteral()) {
type = "String";
} else if (lit.jjtGetNumChildren() > 0 && lit.jjtGetChild(0) instanceof ASTBooleanLiteral) {
type = "boolean";
} else if (lit.isDoubleLiteral()) {
type = "double";
} else if (lit.isLongLiteral()) {
type = "long";
}
} else if (arg.jjtGetChild(0) instanceof ASTName) {
// ASTName n = (ASTName)arg.jjtGetChild(0);
type = "ref";
}
}
argumentTypes.add(type);
}
}
return argumentTypes;
}
use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.
the class IdempotentOperationsRule method visit.
@Override
public Object visit(ASTStatementExpression node, Object data) {
if (node.jjtGetNumChildren() != 3 || !(node.jjtGetChild(0) instanceof ASTPrimaryExpression) || !(node.jjtGetChild(1) instanceof ASTAssignmentOperator) || ((ASTAssignmentOperator) node.jjtGetChild(1)).isCompound() || !(node.jjtGetChild(2) instanceof ASTExpression) || node.jjtGetChild(0).jjtGetChild(0).jjtGetNumChildren() == 0 || node.jjtGetChild(2).jjtGetChild(0).jjtGetChild(0).jjtGetNumChildren() == 0) {
return super.visit(node, data);
}
Node lhs = node.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0);
if (!(lhs instanceof ASTName)) {
return super.visit(node, data);
}
Node rhs = node.jjtGetChild(2).jjtGetChild(0).jjtGetChild(0).jjtGetChild(0);
if (!(rhs instanceof ASTName)) {
return super.visit(node, data);
}
if (!lhs.hasImageEqualTo(rhs.getImage())) {
return super.visit(node, data);
}
if (lhs.jjtGetParent().jjtGetParent().jjtGetNumChildren() > 1) {
Node n = lhs.jjtGetParent().jjtGetParent().jjtGetChild(1);
if (n instanceof ASTPrimarySuffix && ((ASTPrimarySuffix) n).isArrayDereference()) {
return super.visit(node, data);
}
}
if (rhs.jjtGetParent().jjtGetParent().jjtGetNumChildren() > 1) {
Node n = rhs.jjtGetParent().jjtGetParent().jjtGetChild(1);
if (n instanceof ASTPrimarySuffix && ((ASTPrimarySuffix) n).isArguments() || ((ASTPrimarySuffix) n).isArrayDereference()) {
return super.visit(node, data);
}
}
if (lhs.findDescendantsOfType(ASTPrimarySuffix.class).size() != rhs.findDescendantsOfType(ASTPrimarySuffix.class).size()) {
return super.visit(node, data);
}
List<ASTPrimarySuffix> lhsSuffixes = lhs.jjtGetParent().jjtGetParent().findDescendantsOfType(ASTPrimarySuffix.class);
List<ASTPrimarySuffix> rhsSuffixes = rhs.jjtGetParent().jjtGetParent().findDescendantsOfType(ASTPrimarySuffix.class);
if (lhsSuffixes.size() != rhsSuffixes.size()) {
return super.visit(node, data);
}
for (int i = 0; i < lhsSuffixes.size(); i++) {
ASTPrimarySuffix l = lhsSuffixes.get(i);
ASTPrimarySuffix r = rhsSuffixes.get(i);
if (!l.hasImageEqualTo(r.getImage())) {
return super.visit(node, data);
}
}
addViolation(data, node);
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.
the class CloseResourceRule method ensureClosed.
private void ensureClosed(ASTLocalVariableDeclaration var, ASTVariableDeclaratorId id, Object data) {
// What are the chances of a Connection being instantiated in a
// for-loop init block? Anyway, I'm lazy!
String variableToClose = id.getImage();
Node n = var;
while (!(n instanceof ASTBlock) && !(n instanceof ASTConstructorDeclaration)) {
n = n.jjtGetParent();
}
Node top = n;
List<ASTTryStatement> tryblocks = top.findDescendantsOfType(ASTTryStatement.class);
boolean closed = false;
ASTBlockStatement parentBlock = id.getFirstParentOfType(ASTBlockStatement.class);
// block.
for (ASTTryStatement t : tryblocks) {
// verifies that there are no critical statements between the
// variable declaration and
// the beginning of the try block.
ASTBlockStatement tryBlock = t.getFirstParentOfType(ASTBlockStatement.class);
// the variable has been initialized with null
if (!hasNullInitializer(var) && parentBlock.jjtGetParent() == tryBlock.jjtGetParent()) {
List<ASTBlockStatement> blocks = parentBlock.jjtGetParent().findChildrenOfType(ASTBlockStatement.class);
int parentBlockIndex = blocks.indexOf(parentBlock);
int tryBlockIndex = blocks.indexOf(tryBlock);
boolean criticalStatements = false;
for (int i = parentBlockIndex + 1; i < tryBlockIndex; i++) {
// assume variable declarations are not critical
ASTLocalVariableDeclaration varDecl = blocks.get(i).getFirstDescendantOfType(ASTLocalVariableDeclaration.class);
if (varDecl == null) {
criticalStatements = true;
break;
}
}
if (criticalStatements) {
break;
}
}
if (t.getBeginLine() > id.getBeginLine() && t.hasFinally()) {
ASTBlock f = (ASTBlock) t.getFinally().jjtGetChild(0);
List<ASTName> names = f.findDescendantsOfType(ASTName.class);
for (ASTName oName : names) {
String name = oName.getImage();
if (name != null && name.contains(".")) {
String[] parts = name.split("\\.");
if (parts.length == 2) {
String methodName = parts[1];
String varName = parts[0];
if (varName.equals(variableToClose) && closeTargets.contains(methodName) && nullCheckIfCondition(f, oName, varName)) {
closed = true;
break;
}
}
}
}
if (closed) {
break;
}
List<ASTStatementExpression> exprs = new ArrayList<>();
f.findDescendantsOfType(ASTStatementExpression.class, exprs, true);
for (ASTStatementExpression stmt : exprs) {
ASTPrimaryExpression expr = stmt.getFirstChildOfType(ASTPrimaryExpression.class);
if (expr != null) {
ASTPrimaryPrefix prefix = expr.getFirstChildOfType(ASTPrimaryPrefix.class);
ASTPrimarySuffix suffix = expr.getFirstChildOfType(ASTPrimarySuffix.class);
if (prefix != null && suffix != null) {
if (prefix.getImage() == null) {
ASTName prefixName = prefix.getFirstChildOfType(ASTName.class);
if (prefixName != null && closeTargets.contains(prefixName.getImage())) {
// Found a call to a "close target" that is
// a direct
// method call without a "ClassName."
// prefix.
closed = variableIsPassedToMethod(expr, variableToClose);
if (closed) {
break;
}
}
} else if (suffix.getImage() != null) {
String prefixPlusSuffix = prefix.getImage() + "." + suffix.getImage();
if (closeTargets.contains(prefixPlusSuffix)) {
// Found a call to a "close target" that is
// a method call
// in the form "ClassName.methodName".
closed = variableIsPassedToMethod(expr, variableToClose);
if (closed) {
break;
}
}
}
// really check it.
if (!closed) {
List<ASTPrimarySuffix> suffixes = new ArrayList<>();
expr.findDescendantsOfType(ASTPrimarySuffix.class, suffixes, true);
for (ASTPrimarySuffix oSuffix : suffixes) {
String suff = oSuffix.getImage();
if (closeTargets.contains(suff)) {
closed = variableIsPassedToMethod(expr, variableToClose);
if (closed) {
break;
}
}
}
}
}
}
}
if (closed) {
break;
}
}
}
if (!closed) {
// See if the variable is returned by the method, which means the
// method is a utility for creating the db resource, which means of
// course it can't be closed by the method, so it isn't an error.
List<ASTReturnStatement> returns = new ArrayList<>();
top.findDescendantsOfType(ASTReturnStatement.class, returns, true);
for (ASTReturnStatement returnStatement : returns) {
ASTName name = returnStatement.getFirstDescendantOfType(ASTName.class);
if (name != null && name.getImage().equals(variableToClose)) {
closed = true;
break;
}
}
}
// if all is not well, complain
if (!closed) {
ASTType type = var.getFirstChildOfType(ASTType.class);
ASTReferenceType ref = (ASTReferenceType) type.jjtGetChild(0);
ASTClassOrInterfaceType clazz = (ASTClassOrInterfaceType) ref.jjtGetChild(0);
addViolation(data, id, clazz.getImage());
}
}
use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.
the class DoubleCheckedLockingRule method checkLocalVariableUsage.
private boolean checkLocalVariableUsage(ASTMethodDeclaration node, String returnVariableName) {
List<ASTLocalVariableDeclaration> locals = node.findDescendantsOfType(ASTLocalVariableDeclaration.class);
ASTVariableInitializer initializer = null;
for (ASTLocalVariableDeclaration l : locals) {
ASTVariableDeclaratorId id = l.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
if (id != null && id.hasImageEqualTo(returnVariableName)) {
initializer = l.getFirstDescendantOfType(ASTVariableInitializer.class);
break;
}
}
// the return variable name doesn't seem to be a local variable
if (initializer == null) {
return false;
}
// verify the value with which the local variable is initialized
if (initializer.jjtGetNumChildren() > 0 && initializer.jjtGetChild(0) instanceof ASTExpression && initializer.jjtGetChild(0).jjtGetNumChildren() > 0 && initializer.jjtGetChild(0).jjtGetChild(0) instanceof ASTPrimaryExpression && initializer.jjtGetChild(0).jjtGetChild(0).jjtGetNumChildren() > 0 && initializer.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0) instanceof ASTPrimaryPrefix && initializer.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0).jjtGetNumChildren() > 0 && initializer.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0).jjtGetChild(0) instanceof ASTName) {
ASTName name = (ASTName) initializer.jjtGetChild(0).jjtGetChild(0).jjtGetChild(0).jjtGetChild(0);
if (name == null || !volatileFields.contains(name.getImage())) {
return false;
}
} else {
// not a simple assignment
return false;
}
// now check every usage/assignment of the variable
List<ASTName> names = node.findDescendantsOfType(ASTName.class);
for (ASTName n : names) {
if (!n.hasImageEqualTo(returnVariableName)) {
continue;
}
Node expression = n.getNthParent(3);
if (expression instanceof ASTEqualityExpression) {
continue;
}
if (expression instanceof ASTStatementExpression) {
if (expression.jjtGetNumChildren() > 2 && expression.jjtGetChild(1) instanceof ASTAssignmentOperator) {
ASTName value = expression.jjtGetChild(2).getFirstDescendantOfType(ASTName.class);
if (value == null || !volatileFields.contains(value.getImage())) {
return false;
}
}
}
}
return true;
}
use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.
the class SignatureDeclareThrowsExceptionRule method visit.
@Override
public Object visit(ASTMethodDeclaration methodDeclaration, Object o) {
if (junitImported && isAllowedMethod(methodDeclaration)) {
return super.visit(methodDeclaration, o);
}
if (methodDeclaration.getMethodName().startsWith("test")) {
return super.visit(methodDeclaration, o);
}
// Ignore overridden methods, the issue should be marked on the method definition
final List<ASTAnnotation> methodAnnotations = methodDeclaration.jjtGetParent().findChildrenOfType(ASTAnnotation.class);
for (final ASTAnnotation annotation : methodAnnotations) {
final ASTName annotationName = annotation.getFirstDescendantOfType(ASTName.class);
if (annotationName.hasImageEqualTo("Override") || annotationName.hasImageEqualTo("java.lang.Override")) {
return super.visit(methodDeclaration, o);
}
}
checkExceptions(methodDeclaration, o);
return super.visit(methodDeclaration, o);
}
Aggregations