Search in sources :

Example 1 with ASTConstructorDeclaration

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

the class StatementAndBraceFinder method buildDataFlowFor.

public void buildDataFlowFor(JavaNode node) {
    if (!(node instanceof ASTMethodDeclaration) && !(node instanceof ASTConstructorDeclaration)) {
        throw new RuntimeException("Can't build a data flow for anything other than a method or a constructor");
    }
    this.dataFlow = new Structure(dataFlowHandler);
    this.dataFlow.createStartNode(node.getBeginLine());
    this.dataFlow.createNewNode(node);
    node.jjtAccept(this, dataFlow);
    this.dataFlow.createEndNode(node.getEndLine());
    if (LOGGER.isLoggable(Level.FINE)) {
        // TODO SRT Remove after development
        LOGGER.fine("DataFlow is " + this.dataFlow.dump());
    }
    Linker linker = new Linker(dataFlowHandler, dataFlow.getBraceStack(), dataFlow.getContinueBreakReturnStack());
    try {
        linker.computePaths();
    } catch (SequenceException | LinkerException e) {
        e.printStackTrace();
    }
}
Also used : LinkerException(net.sourceforge.pmd.lang.dfa.LinkerException) ASTConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) SequenceException(net.sourceforge.pmd.lang.dfa.SequenceException) Structure(net.sourceforge.pmd.lang.dfa.Structure) Linker(net.sourceforge.pmd.lang.dfa.Linker)

Example 2 with ASTConstructorDeclaration

use of net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration 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());
    }
}
Also used : ASTConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration) ASTLocalVariableDeclaration(net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration) Node(net.sourceforge.pmd.lang.ast.Node) ArrayList(java.util.ArrayList) ASTStatementExpression(net.sourceforge.pmd.lang.java.ast.ASTStatementExpression) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement) ASTReferenceType(net.sourceforge.pmd.lang.java.ast.ASTReferenceType) ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) ASTTryStatement(net.sourceforge.pmd.lang.java.ast.ASTTryStatement) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix) ASTBlock(net.sourceforge.pmd.lang.java.ast.ASTBlock) ASTReturnStatement(net.sourceforge.pmd.lang.java.ast.ASTReturnStatement)

Example 3 with ASTConstructorDeclaration

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

the class ImmutableFieldRule method visit.

@Override
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
    Object result = super.visit(node, data);
    Map<VariableNameDeclaration, List<NameOccurrence>> vars = node.getScope().getDeclarations(VariableNameDeclaration.class);
    List<ASTConstructorDeclaration> constructors = findAllConstructors(node);
    for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : vars.entrySet()) {
        VariableNameDeclaration field = entry.getKey();
        AccessNode accessNodeParent = field.getAccessNodeParent();
        if (accessNodeParent.isStatic() || !accessNodeParent.isPrivate() || accessNodeParent.isFinal() || accessNodeParent.isVolatile() || hasClassLombokAnnotation()) {
            continue;
        }
        FieldImmutabilityType type = initializedInConstructor(entry.getValue(), new HashSet<>(constructors));
        if (type == FieldImmutabilityType.MUTABLE) {
            continue;
        }
        if (type == FieldImmutabilityType.IMMUTABLE || type == FieldImmutabilityType.CHECKDECL && initializedWhenDeclared(field)) {
            addViolation(data, field.getNode(), field.getImage());
        }
    }
    return result;
}
Also used : ASTConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) List(java.util.List) AccessNode(net.sourceforge.pmd.lang.java.ast.AccessNode) Map(java.util.Map)

Example 4 with ASTConstructorDeclaration

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

the class ImmutableFieldRule method initializedInConstructor.

private FieldImmutabilityType initializedInConstructor(List<NameOccurrence> usages, Set<ASTConstructorDeclaration> allConstructors) {
    FieldImmutabilityType result = FieldImmutabilityType.MUTABLE;
    int methodInitCount = 0;
    int lambdaUsage = 0;
    // set of constructors accessing the field
    Set<ASTConstructorDeclaration> consSet = new HashSet<>();
    for (NameOccurrence occ : usages) {
        JavaNameOccurrence jocc = (JavaNameOccurrence) occ;
        if (jocc.isOnLeftHandSide() || jocc.isSelfAssignment()) {
            Node node = jocc.getLocation();
            ASTConstructorDeclaration constructor = node.getFirstParentOfType(ASTConstructorDeclaration.class);
            if (constructor != null) {
                if (inLoopOrTry(node)) {
                    continue;
                }
                // in one constructor only
                if (node.getFirstParentOfType(ASTIfStatement.class) != null) {
                    methodInitCount++;
                }
                if (inAnonymousInnerClass(node)) {
                    methodInitCount++;
                } else if (node.getFirstParentOfType(ASTLambdaExpression.class) != null) {
                    lambdaUsage++;
                } else {
                    consSet.add(constructor);
                }
            } else {
                if (node.getFirstParentOfType(ASTMethodDeclaration.class) != null) {
                    methodInitCount++;
                } else if (node.getFirstParentOfType(ASTLambdaExpression.class) != null) {
                    lambdaUsage++;
                }
            }
        }
    }
    if (usages.isEmpty() || methodInitCount == 0 && lambdaUsage == 0 && consSet.isEmpty()) {
        result = FieldImmutabilityType.CHECKDECL;
    } else {
        allConstructors.removeAll(consSet);
        if (allConstructors.isEmpty() && methodInitCount == 0 && lambdaUsage == 0) {
            result = FieldImmutabilityType.IMMUTABLE;
        }
    }
    return result;
}
Also used : ASTConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) Node(net.sourceforge.pmd.lang.ast.Node) AccessNode(net.sourceforge.pmd.lang.java.ast.AccessNode) ASTIfStatement(net.sourceforge.pmd.lang.java.ast.ASTIfStatement) HashSet(java.util.HashSet) JavaNameOccurrence(net.sourceforge.pmd.lang.java.symboltable.JavaNameOccurrence) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 5 with ASTConstructorDeclaration

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

the class UnusedPrivateMethodRule method calledFromOutsideItself.

private boolean calledFromOutsideItself(List<NameOccurrence> occs, NameDeclaration mnd) {
    int callsFromOutsideMethod = 0;
    for (NameOccurrence occ : occs) {
        Node occNode = occ.getLocation();
        ASTConstructorDeclaration enclosingConstructor = occNode.getFirstParentOfType(ASTConstructorDeclaration.class);
        if (enclosingConstructor != null) {
            callsFromOutsideMethod++;
            // Do we miss unused private constructors here?
            break;
        }
        ASTInitializer enclosingInitializer = occNode.getFirstParentOfType(ASTInitializer.class);
        if (enclosingInitializer != null) {
            callsFromOutsideMethod++;
            break;
        }
        ASTMethodDeclaration enclosingMethod = occNode.getFirstParentOfType(ASTMethodDeclaration.class);
        if (enclosingMethod == null || !mnd.getNode().jjtGetParent().equals(enclosingMethod)) {
            callsFromOutsideMethod++;
        }
    }
    return callsFromOutsideMethod == 0;
}
Also used : ASTConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) Node(net.sourceforge.pmd.lang.ast.Node) AccessNode(net.sourceforge.pmd.lang.java.ast.AccessNode) ASTInitializer(net.sourceforge.pmd.lang.java.ast.ASTInitializer) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Aggregations

ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)9 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)6 Node (net.sourceforge.pmd.lang.ast.Node)4 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)3 AccessNode (net.sourceforge.pmd.lang.java.ast.AccessNode)3 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 Linker (net.sourceforge.pmd.lang.dfa.Linker)1 LinkerException (net.sourceforge.pmd.lang.dfa.LinkerException)1 SequenceException (net.sourceforge.pmd.lang.dfa.SequenceException)1 Structure (net.sourceforge.pmd.lang.dfa.Structure)1 ASTArguments (net.sourceforge.pmd.lang.java.ast.ASTArguments)1 ASTBlock (net.sourceforge.pmd.lang.java.ast.ASTBlock)1 ASTBlockStatement (net.sourceforge.pmd.lang.java.ast.ASTBlockStatement)1 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)1 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)1