use of net.sourceforge.pmd.lang.java.ast.ASTReferenceType in project pmd by pmd.
the class CompareObjectsWithEqualsRule method visit.
public Object visit(ASTEqualityExpression node, Object data) {
Node c0 = node.jjtGetChild(0).jjtGetChild(0);
Node c1 = node.jjtGetChild(1).jjtGetChild(0);
// equals expression is correct
if (isAllocation(c0) || isAllocation(c1)) {
addViolation(data, node);
return data;
}
// skip if either child is not a simple name
if (!hasName(c0) || !hasName(c1)) {
return data;
}
// skip if either is a qualified name
if (isQualifiedName(c0.jjtGetChild(0)) || isQualifiedName(c1.jjtGetChild(0))) {
return data;
}
// skip if either is part of a qualified name
if (isPartOfQualifiedName(node.jjtGetChild(0)) || isPartOfQualifiedName(node.jjtGetChild(1))) {
return data;
}
// skip static initializers... missing some cases here
if (!node.getParentsOfType(ASTInitializer.class).isEmpty()) {
return data;
}
ASTName n0 = (ASTName) c0.jjtGetChild(0);
ASTName n1 = (ASTName) c1.jjtGetChild(0);
if (n0.getNameDeclaration() instanceof VariableNameDeclaration && n1.getNameDeclaration() instanceof VariableNameDeclaration) {
VariableNameDeclaration nd0 = (VariableNameDeclaration) n0.getNameDeclaration();
VariableNameDeclaration nd1 = (VariableNameDeclaration) n1.getNameDeclaration();
// FIXME catch comparisons btwn array elements of reference types
if (nd0.isArray() || nd1.isArray()) {
return data;
}
if (nd0.isReferenceType() && nd1.isReferenceType()) {
ASTReferenceType type0 = ((Node) nd0.getAccessNodeParent()).getFirstDescendantOfType(ASTReferenceType.class);
ASTReferenceType type1 = ((Node) nd1.getAccessNodeParent()).getFirstDescendantOfType(ASTReferenceType.class);
// skip, if it is an enum
if (type0.getType() != null && type0.getType().equals(type1.getType()) && // It may be a custom enum class or an explicit Enum class usage
(type0.getType().isEnum() || type0.getType() == java.lang.Enum.class)) {
return data;
}
addViolation(data, node);
}
}
return data;
}
use of net.sourceforge.pmd.lang.java.ast.ASTReferenceType in project pmd by pmd.
the class CloseResourceRule method checkForResources.
private void checkForResources(Node node, Object data) {
List<ASTLocalVariableDeclaration> vars = node.findDescendantsOfType(ASTLocalVariableDeclaration.class);
List<ASTVariableDeclaratorId> ids = new ArrayList<>();
// find all variable references to Connection objects
for (ASTLocalVariableDeclaration var : vars) {
ASTType type = var.getTypeNode();
if (type.jjtGetChild(0) instanceof ASTReferenceType) {
ASTReferenceType ref = (ASTReferenceType) type.jjtGetChild(0);
if (ref.jjtGetChild(0) instanceof ASTClassOrInterfaceType) {
ASTClassOrInterfaceType clazz = (ASTClassOrInterfaceType) ref.jjtGetChild(0);
if (clazz.getType() != null && types.contains(clazz.getType().getName()) || clazz.getType() == null && simpleTypes.contains(toSimpleType(clazz.getImage())) && !clazz.isReferenceToClassSameCompilationUnit() || types.contains(clazz.getImage()) && !clazz.isReferenceToClassSameCompilationUnit()) {
ASTVariableDeclaratorId id = var.getFirstDescendantOfType(ASTVariableDeclaratorId.class);
ids.add(id);
}
}
}
}
// if there are connections, ensure each is closed.
for (ASTVariableDeclaratorId x : ids) {
ensureClosed((ASTLocalVariableDeclaration) x.jjtGetParent().jjtGetParent(), x, data);
}
}
use of net.sourceforge.pmd.lang.java.ast.ASTReferenceType 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.ASTReferenceType in project pmd by pmd.
the class DoubleCheckedLockingRule method visit.
@Override
public Object visit(ASTMethodDeclaration node, Object data) {
if (node.getResultType().isVoid()) {
return super.visit(node, data);
}
ASTType typeNode = (ASTType) node.getResultType().jjtGetChild(0);
if (typeNode.jjtGetNumChildren() == 0 || !(typeNode.jjtGetChild(0) instanceof ASTReferenceType)) {
return super.visit(node, data);
}
List<ASTReturnStatement> rsl = node.findDescendantsOfType(ASTReturnStatement.class);
if (rsl.size() != 1) {
return super.visit(node, data);
}
ASTReturnStatement rs = rsl.get(0);
List<ASTPrimaryExpression> pel = rs.findDescendantsOfType(ASTPrimaryExpression.class);
ASTPrimaryExpression ape = pel.get(0);
Node lastChild = ape.jjtGetChild(ape.jjtGetNumChildren() - 1);
String returnVariableName = null;
if (lastChild instanceof ASTPrimaryPrefix) {
returnVariableName = getNameFromPrimaryPrefix((ASTPrimaryPrefix) lastChild);
}
// With Java5 and volatile keyword, DCL is no longer an issue
if (returnVariableName == null || this.volatileFields.contains(returnVariableName)) {
return super.visit(node, data);
}
// field, then it's ok, too
if (checkLocalVariableUsage(node, returnVariableName)) {
return super.visit(node, data);
}
List<ASTIfStatement> isl = node.findDescendantsOfType(ASTIfStatement.class);
if (isl.size() == 2) {
ASTIfStatement is = isl.get(0);
if (ifVerify(is, returnVariableName)) {
// find synchronized
List<ASTSynchronizedStatement> ssl = is.findDescendantsOfType(ASTSynchronizedStatement.class);
if (ssl.size() == 1) {
ASTSynchronizedStatement ss = ssl.get(0);
isl = ss.findDescendantsOfType(ASTIfStatement.class);
if (isl.size() == 1) {
ASTIfStatement is2 = isl.get(0);
if (ifVerify(is2, returnVariableName)) {
List<ASTStatementExpression> sel = is2.findDescendantsOfType(ASTStatementExpression.class);
if (sel.size() == 1) {
ASTStatementExpression se = sel.get(0);
if (se.jjtGetNumChildren() == 3) {
// primaryExpression, AssignmentOperator, Expression
if (se.jjtGetChild(0) instanceof ASTPrimaryExpression) {
ASTPrimaryExpression pe = (ASTPrimaryExpression) se.jjtGetChild(0);
if (matchName(pe, returnVariableName)) {
if (se.jjtGetChild(1) instanceof ASTAssignmentOperator) {
addViolation(data, node);
}
}
}
}
}
}
}
}
}
}
return super.visit(node, data);
}
use of net.sourceforge.pmd.lang.java.ast.ASTReferenceType in project pmd by pmd.
the class UselessStringValueOfRule method visit.
@Override
public Object visit(ASTPrimaryPrefix node, Object data) {
if (node.jjtGetNumChildren() == 0 || !(node.jjtGetChild(0) instanceof ASTName)) {
return super.visit(node, data);
}
String image = ((ASTName) node.jjtGetChild(0)).getImage();
if ("String.valueOf".equals(image)) {
Node parent = node.jjtGetParent();
if (parent.jjtGetNumChildren() != 2) {
return super.visit(node, data);
}
// skip String.valueOf(anyarraytype[])
ASTArgumentList args = parent.getFirstDescendantOfType(ASTArgumentList.class);
if (args != null) {
ASTName arg = args.getFirstDescendantOfType(ASTName.class);
if (arg != null) {
NameDeclaration declaration = arg.getNameDeclaration();
if (declaration != null) {
ASTType argType = declaration.getNode().jjtGetParent().jjtGetParent().getFirstDescendantOfType(ASTType.class);
if (argType != null && argType.jjtGetChild(0) instanceof ASTReferenceType && ((ASTReferenceType) argType.jjtGetChild(0)).isArray()) {
return super.visit(node, data);
}
}
}
}
Node gp = parent.jjtGetParent();
if (parent instanceof ASTPrimaryExpression && gp instanceof ASTAdditiveExpression && "+".equals(gp.getImage())) {
boolean ok = false;
if (gp.jjtGetChild(0) == parent) {
ok = !isPrimitive(gp.jjtGetChild(1));
} else {
for (int i = 0; !ok && gp.jjtGetChild(i) != parent; i++) {
ok = !isPrimitive(gp.jjtGetChild(i));
}
}
if (ok) {
super.addViolation(data, node);
return data;
}
}
}
return super.visit(node, data);
}
Aggregations