Search in sources :

Example 26 with ASTPrimaryExpression

use of net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression in project pmd by pmd.

the class InvalidSlf4jMessageFormatRule method visit.

@Override
public Object visit(final ASTName node, final Object data) {
    final NameDeclaration nameDeclaration = node.getNameDeclaration();
    // ignore imports or methods
    if (!(nameDeclaration instanceof VariableNameDeclaration)) {
        return super.visit(node, data);
    }
    // ignore non slf4j logger
    Class<?> type = ((VariableNameDeclaration) nameDeclaration).getType();
    if (type == null || !type.getName().equals(LOGGER_CLASS)) {
        return super.visit(node, data);
    }
    // get the node that contains the logger
    final ASTPrimaryExpression parentNode = node.getFirstParentOfType(ASTPrimaryExpression.class);
    // get the log level
    final String method = parentNode.getFirstChildOfType(ASTPrimaryPrefix.class).getFirstChildOfType(ASTName.class).getImage().replace(nameDeclaration.getImage() + ".", "");
    // ignore if not a log level
    if (!LOGGER_LEVELS.contains(method)) {
        return super.visit(node, data);
    }
    // find the arguments
    final List<ASTExpression> argumentList = parentNode.getFirstChildOfType(ASTPrimarySuffix.class).getFirstDescendantOfType(ASTArgumentList.class).findChildrenOfType(ASTExpression.class);
    // remove the message parameter
    final ASTPrimaryExpression messageParam = argumentList.remove(0).getFirstDescendantOfType(ASTPrimaryExpression.class);
    final int expectedArguments = expectedArguments(messageParam);
    if (expectedArguments == 0) {
        // or if we couldn't analyze the message parameter
        return super.visit(node, data);
    }
    // But only, if it is not used as a placeholder argument
    if (argumentList.size() > expectedArguments) {
        removeThrowableParam(argumentList);
    }
    if (argumentList.size() < expectedArguments) {
        addViolationWithMessage(data, node, "Missing arguments," + getExpectedMessage(argumentList, expectedArguments));
    } else if (argumentList.size() > expectedArguments) {
        addViolationWithMessage(data, node, "Too many arguments," + getExpectedMessage(argumentList, expectedArguments));
    }
    return super.visit(node, data);
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Example 27 with ASTPrimaryExpression

use of net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression in project pmd by pmd.

the class InvalidSlf4jMessageFormatRule method removeThrowableParam.

private void removeThrowableParam(final List<ASTExpression> params) {
    // Throwable parameters are the last one in the list, if any.
    if (params.isEmpty()) {
        return;
    }
    int lastIndex = params.size() - 1;
    ASTPrimaryExpression last = params.get(lastIndex).getFirstDescendantOfType(ASTPrimaryExpression.class);
    if (isNewThrowable(last) || hasTypeThrowable(last) || isReferencingThrowable(last)) {
        params.remove(lastIndex);
    }
}
Also used : ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)

Example 28 with ASTPrimaryExpression

use of net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression 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);
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTCastExpression(net.sourceforge.pmd.lang.java.ast.ASTCastExpression) RuleContext(net.sourceforge.pmd.RuleContext) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTVariableInitializer(net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTAllocationExpression(net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) ASTThrowStatement(net.sourceforge.pmd.lang.java.ast.ASTThrowStatement) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 29 with ASTPrimaryExpression

use of net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression in project pmd by pmd.

the class ConfusingTernaryRule method isParenthesisAroundMatch.

private static boolean isParenthesisAroundMatch(Node node) {
    // look for "(match)"
    if (!(node instanceof ASTPrimaryExpression) || node.jjtGetNumChildren() != 1) {
        return false;
    }
    Node inode = node.jjtGetChild(0);
    if (!(inode instanceof ASTPrimaryPrefix) || inode.jjtGetNumChildren() != 1) {
        return false;
    }
    Node jnode = inode.jjtGetChild(0);
    if (!(jnode instanceof ASTExpression) || jnode.jjtGetNumChildren() != 1) {
        return false;
    }
    Node knode = jnode.jjtGetChild(0);
    // recurse!
    return isMatch(knode);
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Example 30 with ASTPrimaryExpression

use of net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression in project pmd by pmd.

the class LawOfDemeterRule method visit.

/**
 * That's a new method. We are going to check each method call inside the
 * method.
 *
 * @return <code>null</code>.
 */
@Override
public Object visit(ASTMethodDeclaration node, Object data) {
    List<ASTPrimaryExpression> primaryExpressions = node.findDescendantsOfType(ASTPrimaryExpression.class);
    for (ASTPrimaryExpression expression : primaryExpressions) {
        List<MethodCall> calls = MethodCall.createMethodCalls(expression);
        addViolations(calls, (RuleContext) data);
    }
    return null;
}
Also used : ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)

Aggregations

ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)31 Node (net.sourceforge.pmd.lang.ast.Node)23 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)16 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)11 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)10 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)8 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)8 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)7 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)6 ASTAssignmentOperator (net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator)5 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)5 ASTLiteral (net.sourceforge.pmd.lang.java.ast.ASTLiteral)4 ASTReturnStatement (net.sourceforge.pmd.lang.java.ast.ASTReturnStatement)4 ASTType (net.sourceforge.pmd.lang.java.ast.ASTType)4 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)4 ASTAllocationExpression (net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression)3 ASTBlockStatement (net.sourceforge.pmd.lang.java.ast.ASTBlockStatement)3 ASTEqualityExpression (net.sourceforge.pmd.lang.java.ast.ASTEqualityExpression)3 ASTIfStatement (net.sourceforge.pmd.lang.java.ast.ASTIfStatement)3 ASTLocalVariableDeclaration (net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration)3