Search in sources :

Example 11 with ASTArgumentList

use of net.sourceforge.pmd.lang.java.ast.ASTArgumentList 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;
}
Also used : ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ArrayList(java.util.ArrayList) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList)

Example 12 with ASTArgumentList

use of net.sourceforge.pmd.lang.java.ast.ASTArgumentList 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 13 with ASTArgumentList

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

the class ClassTypeResolverTest method testMethodParameterization.

@Test
public void testMethodParameterization() throws JaxenException, NoSuchMethodException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(GenericMethodsImplicit.class);
    List<AbstractJavaNode> expressions = convertList(acu.findChildNodesWithXPath("//ArgumentList"), AbstractJavaNode.class);
    JavaTypeDefinition context = forClass(GenericMethodsImplicit.class, forClass(Thread.class));
    Method method = GenericMethodsImplicit.class.getMethod("bar", Object.class, Object.class, Integer.class, Object.class);
    ASTArgumentList argList = (ASTArgumentList) expressions.get(0);
    MethodType inferedMethod = MethodTypeResolution.parameterizeInvocation(context, method, argList);
    assertEquals(inferedMethod.getParameterTypes().get(0), forClass(SuperClassA2.class));
    assertEquals(inferedMethod.getParameterTypes().get(1), forClass(SuperClassA2.class));
    assertEquals(inferedMethod.getParameterTypes().get(2), forClass(Integer.class));
    assertEquals(inferedMethod.getParameterTypes().get(3), forClass(SuperClassAOther2.class));
}
Also used : MethodType(net.sourceforge.pmd.lang.java.typeresolution.MethodType) SuperClassA2(net.sourceforge.pmd.typeresolution.testdata.dummytypes.SuperClassA2) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) JavaTypeDefinition(net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition) AbstractJavaNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode) SuperClassAOther2(net.sourceforge.pmd.typeresolution.testdata.dummytypes.SuperClassAOther2) Method(java.lang.reflect.Method) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) Test(org.junit.Test)

Example 14 with ASTArgumentList

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

the class ClassTypeResolverTest method testMethodInitialConstraints.

@Test
public void testMethodInitialConstraints() throws NoSuchMethodException, JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(GenericMethodsImplicit.class);
    List<AbstractJavaNode> expressions = convertList(acu.findChildNodesWithXPath("//ArgumentList"), AbstractJavaNode.class);
    List<Variable> variables = new ArrayList<>();
    for (int i = 0; i < 2; ++i) {
        variables.add(new Variable());
    }
    Method method = GenericMethodsImplicit.class.getMethod("bar", Object.class, Object.class, Integer.class, Object.class);
    ASTArgumentList argList = (ASTArgumentList) expressions.get(0);
    List<Constraint> constraints = MethodTypeResolution.produceInitialConstraints(method, argList, variables);
    assertEquals(constraints.size(), 3);
    // A
    assertTrue(constraints.contains(new Constraint(forClass(SuperClassA.class), variables.get(0), LOOSE_INVOCATION)));
    assertTrue(constraints.contains(new Constraint(forClass(SuperClassAOther.class), variables.get(0), LOOSE_INVOCATION)));
    // B
    assertTrue(constraints.contains(new Constraint(forClass(SuperClassAOther2.class), variables.get(1), LOOSE_INVOCATION)));
}
Also used : Variable(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Variable) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) AbstractJavaNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) SuperClassAOther(net.sourceforge.pmd.typeresolution.testdata.dummytypes.SuperClassAOther) SuperClassAOther2(net.sourceforge.pmd.typeresolution.testdata.dummytypes.SuperClassAOther2) SuperClassA(net.sourceforge.pmd.typeresolution.testdata.dummytypes.SuperClassA) Test(org.junit.Test)

Aggregations

ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)14 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)9 Node (net.sourceforge.pmd.lang.ast.Node)7 ArrayList (java.util.ArrayList)6 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)4 List (java.util.List)3 ASTAllocationExpression (net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression)3 ASTArguments (net.sourceforge.pmd.lang.java.ast.ASTArguments)3 ASTLiteral (net.sourceforge.pmd.lang.java.ast.ASTLiteral)3 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)3 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)3 JavaTypeDefinition (net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition)3 Method (java.lang.reflect.Method)2 ASTBooleanLiteral (net.sourceforge.pmd.lang.java.ast.ASTBooleanLiteral)2 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)2 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)2 ASTExtendsList (net.sourceforge.pmd.lang.java.ast.ASTExtendsList)2 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)2 AbstractJavaNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaNode)2 AbstractJavaTypeNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode)2