Search in sources :

Example 16 with VariableNameDeclaration

use of net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration in project pmd by pmd.

the class ConsecutiveLiteralAppendsRule method visit.

@Override
public Object visit(ASTVariableDeclaratorId node, Object data) {
    if (!isStringBuffer(node)) {
        return data;
    }
    threshold = getProperty(THRESHOLD_DESCRIPTOR);
    int concurrentCount = checkConstructor(node, data);
    if (hasInitializer(node)) {
        concurrentCount += checkInitializerExpressions(node);
    }
    Node lastBlock = getFirstParentBlock(node);
    Node currentBlock = lastBlock;
    Map<VariableNameDeclaration, List<NameOccurrence>> decls = node.getScope().getDeclarations(VariableNameDeclaration.class);
    Node rootNode = null;
    // only want the constructor flagged if it's really containing strings
    if (concurrentCount >= 1) {
        rootNode = node;
    }
    for (List<NameOccurrence> decl : decls.values()) {
        for (NameOccurrence no : decl) {
            JavaNameOccurrence jno = (JavaNameOccurrence) no;
            Node n = jno.getLocation();
            // variable
            if (!node.getImage().equals(jno.getImage())) {
                continue;
            }
            currentBlock = getFirstParentBlock(n);
            if (!InefficientStringBufferingRule.isInStringBufferOperation(n, 3, "append")) {
                if (!jno.isPartOfQualifiedName()) {
                    checkForViolation(rootNode, data, concurrentCount);
                    concurrentCount = 0;
                }
                continue;
            }
            ASTPrimaryExpression s = n.getFirstParentOfType(ASTPrimaryExpression.class);
            int numChildren = s.jjtGetNumChildren();
            for (int jx = 0; jx < numChildren; jx++) {
                Node sn = s.jjtGetChild(jx);
                if (!(sn instanceof ASTPrimarySuffix) || sn.getImage() != null) {
                    continue;
                }
                // see if it changed blocks
                if (currentBlock != null && lastBlock != null && !currentBlock.equals(lastBlock) || currentBlock == null ^ lastBlock == null) {
                    checkForViolation(rootNode, data, concurrentCount);
                    concurrentCount = 0;
                }
                // here
                if (concurrentCount == 0) {
                    rootNode = sn;
                }
                if (isAdditive(sn)) {
                    concurrentCount = processAdditive(data, concurrentCount, sn, rootNode);
                    if (concurrentCount != 0) {
                        rootNode = sn;
                    }
                } else if (!isAppendingStringLiteral(sn)) {
                    checkForViolation(rootNode, data, concurrentCount);
                    concurrentCount = 0;
                } else {
                    concurrentCount++;
                }
                lastBlock = currentBlock;
            }
        }
    }
    checkForViolation(rootNode, data, concurrentCount);
    return data;
}
Also used : VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) List(java.util.List) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix) JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 17 with VariableNameDeclaration

use of net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration in project pmd by pmd.

the class BeanMembersShouldSerializeRule method visit.

@Override
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
    if (node.isInterface()) {
        return data;
    }
    Map<MethodNameDeclaration, List<NameOccurrence>> methods = node.getScope().getEnclosingScope(ClassScope.class).getMethodDeclarations();
    List<ASTMethodDeclarator> getSetMethList = new ArrayList<>(methods.size());
    for (MethodNameDeclaration d : methods.keySet()) {
        ASTMethodDeclarator mnd = d.getMethodNameDeclaratorNode();
        if (isBeanAccessor(mnd)) {
            getSetMethList.add(mnd);
        }
    }
    String[] methNameArray = imagesOf(getSetMethList);
    Arrays.sort(methNameArray);
    Map<VariableNameDeclaration, List<NameOccurrence>> vars = node.getScope().getDeclarations(VariableNameDeclaration.class);
    for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : vars.entrySet()) {
        VariableNameDeclaration decl = entry.getKey();
        AccessNode accessNodeParent = decl.getAccessNodeParent();
        if (entry.getValue().isEmpty() || accessNodeParent.isTransient() || accessNodeParent.isStatic()) {
            continue;
        }
        String varName = trimIfPrefix(decl.getImage());
        varName = varName.substring(0, 1).toUpperCase(Locale.ROOT) + varName.substring(1, varName.length());
        boolean hasGetMethod = Arrays.binarySearch(methNameArray, "get" + varName) >= 0 || Arrays.binarySearch(methNameArray, "is" + varName) >= 0;
        boolean hasSetMethod = Arrays.binarySearch(methNameArray, "set" + varName) >= 0;
        // variable...
        if (!hasGetMethod || !accessNodeParent.isFinal() && !hasSetMethod) {
            addViolation(data, decl.getNode(), decl.getImage());
        }
    }
    return super.visit(node, data);
}
Also used : VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ArrayList(java.util.ArrayList) ClassScope(net.sourceforge.pmd.lang.java.symboltable.ClassScope) ASTMethodDeclarator(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator) MethodNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.MethodNameDeclaration) ArrayList(java.util.ArrayList) List(java.util.List) AccessNode(net.sourceforge.pmd.lang.java.ast.AccessNode) Map(java.util.Map)

Example 18 with VariableNameDeclaration

use of net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration 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 19 with VariableNameDeclaration

use of net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration 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 20 with VariableNameDeclaration

use of net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration in project pmd by pmd.

the class UnusedFormalParameterRule method check.

private void check(Node node, Object data) {
    Node parent = node.jjtGetParent().jjtGetParent().jjtGetParent();
    if (parent instanceof ASTClassOrInterfaceDeclaration && !((ASTClassOrInterfaceDeclaration) parent).isInterface()) {
        Map<VariableNameDeclaration, List<NameOccurrence>> vars = ((JavaNode) node).getScope().getDeclarations(VariableNameDeclaration.class);
        for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : vars.entrySet()) {
            VariableNameDeclaration nameDecl = entry.getKey();
            if (actuallyUsed(nameDecl, entry.getValue())) {
                continue;
            }
            addViolation(data, nameDecl.getNode(), new Object[] { node instanceof ASTMethodDeclaration ? "method" : "constructor", nameDecl.getImage() });
        }
    }
}
Also used : ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) Node(net.sourceforge.pmd.lang.ast.Node) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode) ASTNameList(net.sourceforge.pmd.lang.java.ast.ASTNameList) List(java.util.List) Map(java.util.Map)

Aggregations

VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)23 List (java.util.List)18 Map (java.util.Map)15 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)8 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)7 ArrayList (java.util.ArrayList)6 AccessNode (net.sourceforge.pmd.lang.java.ast.AccessNode)6 Node (net.sourceforge.pmd.lang.ast.Node)5 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)4 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)4 JavaNameOccurrence (net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence)4 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)3 ClassScope (net.sourceforge.pmd.lang.java.symboltable.ClassScope)3 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)2 ASTForInit (net.sourceforge.pmd.lang.java.ast.ASTForInit)2 ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)2 ASTLiteral (net.sourceforge.pmd.lang.java.ast.ASTLiteral)2 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)2 ASTReferenceType (net.sourceforge.pmd.lang.java.ast.ASTReferenceType)2 ASTVariableInitializer (net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer)2