use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.
the class UselessOverridingMethodRule method isMethodThrowingType.
// TODO: this method should be externalize into an utility class, shouldn't it ?
private boolean isMethodThrowingType(ASTMethodDeclaration node, List<String> exceptedExceptions) {
boolean result = false;
ASTNameList thrownsExceptions = node.getFirstChildOfType(ASTNameList.class);
if (thrownsExceptions != null) {
List<ASTName> names = thrownsExceptions.findChildrenOfType(ASTName.class);
for (ASTName name : names) {
for (String exceptedException : exceptedExceptions) {
if (exceptedException.equals(name.getImage())) {
result = true;
}
}
}
}
return result;
}
use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.
the class CloseResourceRule method variableIsPassedToMethod.
private boolean variableIsPassedToMethod(ASTPrimaryExpression expr, String variable) {
List<ASTName> methodParams = new ArrayList<>();
expr.findDescendantsOfType(ASTName.class, methodParams, true);
for (ASTName pName : methodParams) {
String paramName = pName.getImage();
// also check if we've got the a parameter (i.e if it's an argument
// !)
ASTArgumentList parentParam = pName.getFirstParentOfType(ASTArgumentList.class);
if (paramName.equals(variable) && parentParam != null) {
return true;
}
}
return false;
}
use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.
the class PreserveStackTraceRule method visit.
@Override
public Object visit(ASTCatchStatement catchStmt, Object data) {
String target = catchStmt.jjtGetChild(0).findChildrenOfType(ASTVariableDeclaratorId.class).get(0).getImage();
// Inspect all the throw stmt inside the catch stmt
List<ASTThrowStatement> lstThrowStatements = catchStmt.findDescendantsOfType(ASTThrowStatement.class);
for (ASTThrowStatement throwStatement : lstThrowStatements) {
Node n = throwStatement.jjtGetChild(0).jjtGetChild(0);
if (n instanceof ASTCastExpression) {
ASTPrimaryExpression expr = (ASTPrimaryExpression) n.jjtGetChild(1);
if (expr.jjtGetNumChildren() > 1 && expr.jjtGetChild(1) instanceof ASTPrimaryPrefix) {
RuleContext ctx = (RuleContext) data;
addViolation(ctx, throwStatement);
}
continue;
}
// Retrieve all argument for the throw exception (to see if the
// original exception is preserved)
ASTArgumentList args = throwStatement.getFirstDescendantOfType(ASTArgumentList.class);
if (args != null) {
Node parent = args.jjtGetParent().jjtGetParent();
if (parent instanceof ASTAllocationExpression) {
// maybe it is used inside a anonymous class
ck(data, target, throwStatement, parent);
} else {
// Check all arguments used in the throw statement
ck(data, target, throwStatement, throwStatement);
}
} else {
Node child = throwStatement.jjtGetChild(0);
while (child != null && child.jjtGetNumChildren() > 0 && !(child instanceof ASTName)) {
child = child.jjtGetChild(0);
}
if (child != null) {
if (child instanceof ASTName && !target.equals(child.getImage()) && !child.hasImageEqualTo(target + FILL_IN_STACKTRACE)) {
Map<VariableNameDeclaration, List<NameOccurrence>> vars = ((ASTName) child).getScope().getDeclarations(VariableNameDeclaration.class);
for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : vars.entrySet()) {
VariableNameDeclaration decl = entry.getKey();
List<NameOccurrence> occurrences = entry.getValue();
if (decl.getImage().equals(child.getImage())) {
if (!isInitCauseCalled(target, occurrences)) {
// Check how the variable is initialized
ASTVariableInitializer initializer = decl.getNode().jjtGetParent().getFirstDescendantOfType(ASTVariableInitializer.class);
if (initializer != null) {
args = initializer.getFirstDescendantOfType(ASTArgumentList.class);
if (args != null) {
// constructor with args?
ck(data, target, throwStatement, args);
} else if (!isFillInStackTraceCalled(target, initializer)) {
addViolation(data, throwStatement);
}
}
}
}
}
} else if (child instanceof ASTClassOrInterfaceType) {
addViolation(data, throwStatement);
}
}
}
}
return super.visit(catchStmt, data);
}
use of net.sourceforge.pmd.lang.java.ast.ASTName 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;
}
use of net.sourceforge.pmd.lang.java.ast.ASTName in project pmd by pmd.
the class CommentDefaultAccessModifierRule method hasNoVisibleForTestingAnnotation.
private boolean hasNoVisibleForTestingAnnotation(AbstractJavaAccessNode decl) {
boolean result = true;
ASTClassOrInterfaceBodyDeclaration parent = decl.getFirstParentOfType(ASTClassOrInterfaceBodyDeclaration.class);
if (parent != null) {
List<ASTAnnotation> annotations = parent.findChildrenOfType(ASTAnnotation.class);
for (ASTAnnotation annotation : annotations) {
final ASTName name = annotation.getFirstDescendantOfType(ASTName.class);
if (name.hasImageEqualTo("VisibleForTesting")) {
result = false;
break;
}
}
}
return result;
}
Aggregations