Search in sources :

Example 26 with ASTPrimaryPrefix

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

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

Example 28 with ASTPrimaryPrefix

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

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

the class ClassTypeResolverTest method testThisExpression.

@Test
public void testThisExpression() throws JaxenException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(ThisExpression.class);
    List<ASTPrimaryExpression> expressions = convertList(acu.findChildNodesWithXPath("//PrimaryExpression"), ASTPrimaryExpression.class);
    List<ASTPrimaryPrefix> prefixes = convertList(acu.findChildNodesWithXPath("//PrimaryPrefix"), ASTPrimaryPrefix.class);
    int index = 0;
    assertEquals(ThisExpression.class, expressions.get(index).getType());
    assertEquals(ThisExpression.class, prefixes.get(index++).getType());
    assertEquals(ThisExpression.class, expressions.get(index).getType());
    assertEquals(ThisExpression.class, prefixes.get(index++).getType());
    assertEquals(ThisExpression.class, expressions.get(index).getType());
    assertEquals(ThisExpression.class, prefixes.get(index++).getType());
    assertEquals(ThisExpression.class, expressions.get(index).getType());
    assertEquals(ThisExpression.class, prefixes.get(index++).getType());
    assertEquals(ThisExpression.ThisExprNested.class, expressions.get(index).getType());
    assertEquals(ThisExpression.ThisExprNested.class, prefixes.get(index++).getType());
    // Qualified this
    assertEquals(ThisExpression.class, expressions.get(index).getType());
    assertEquals(ThisExpression.class, prefixes.get(index).getType());
    assertEquals(ThisExpression.class, ((TypeNode) expressions.get(index++).jjtGetChild(1)).getType());
    assertEquals(ThisExpression.ThisExprStaticNested.class, expressions.get(index).getType());
    assertEquals(ThisExpression.ThisExprStaticNested.class, prefixes.get(index++).getType());
    // Make sure we got them all
    assertEquals("All expressions not tested", index, expressions.size());
    assertEquals("All expressions not tested", index, prefixes.size());
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ThisExpression(net.sourceforge.pmd.typeresolution.testdata.ThisExpression) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) Test(org.junit.Test)

Example 30 with ASTPrimaryPrefix

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

the class LocalScopeTest method testNameWithSuperIsNotFlaggedAsUnused.

@Test
public void testNameWithSuperIsNotFlaggedAsUnused() {
    LocalScope scope = new LocalScope();
    ASTName name = new ASTName(1);
    name.setImage("foo");
    ASTPrimaryPrefix prefix = new ASTPrimaryPrefix(2);
    prefix.setUsesSuperModifier();
    name.jjtAddChild(prefix, 1);
    JavaNameOccurrence occ = new JavaNameOccurrence(name, "foo");
    scope.addNameOccurrence(occ);
    assertFalse(scope.getDeclarations().keySet().iterator().hasNext());
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Test(org.junit.Test)

Aggregations

ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)30 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)16 Node (net.sourceforge.pmd.lang.ast.Node)15 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)15 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)11 ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)5 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)5 ArrayList (java.util.ArrayList)4 ASTLiteral (net.sourceforge.pmd.lang.java.ast.ASTLiteral)4 ASTReturnStatement (net.sourceforge.pmd.lang.java.ast.ASTReturnStatement)4 Test (org.junit.Test)4 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)3 ASTAssignmentOperator (net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator)3 ASTIfStatement (net.sourceforge.pmd.lang.java.ast.ASTIfStatement)3 ASTType (net.sourceforge.pmd.lang.java.ast.ASTType)3 ASTBlock (net.sourceforge.pmd.lang.java.ast.ASTBlock)2 ASTCastExpression (net.sourceforge.pmd.lang.java.ast.ASTCastExpression)2 ASTClassOrInterfaceBodyDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration)2 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)2 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)2