Search in sources :

Example 1 with ASTBlockStatement

use of net.sourceforge.pmd.lang.apex.ast.ASTBlockStatement in project pmd by pmd.

the class ApexCRUDViolationRule method recursivelyEvaluateCRUDMethodCalls.

private void recursivelyEvaluateCRUDMethodCalls(final AbstractApexNode<?> self, final Set<ASTMethodCallExpression> innerMethodCalls, final ASTBlockStatement blockStatement) {
    if (blockStatement != null) {
        int numberOfStatements = blockStatement.jjtGetNumChildren();
        for (int i = 0; i < numberOfStatements; i++) {
            Node n = blockStatement.jjtGetChild(i);
            if (n instanceof ASTIfElseBlockStatement) {
                List<ASTBlockStatement> innerBlocks = n.findDescendantsOfType(ASTBlockStatement.class);
                for (ASTBlockStatement innerBlock : innerBlocks) {
                    recursivelyEvaluateCRUDMethodCalls(self, innerMethodCalls, innerBlock);
                }
            }
            AbstractApexNode<?> match = n.getFirstDescendantOfType(self.getClass());
            if (Objects.equal(match, self)) {
                break;
            }
            ASTMethodCallExpression methodCall = n.getFirstDescendantOfType(ASTMethodCallExpression.class);
            if (methodCall != null) {
                mapCallToMethodDecl(self, innerMethodCalls, Arrays.asList(methodCall));
            }
        }
    }
}
Also used : ASTIfElseBlockStatement(net.sourceforge.pmd.lang.apex.ast.ASTIfElseBlockStatement) AbstractApexNode(net.sourceforge.pmd.lang.apex.ast.AbstractApexNode) Node(net.sourceforge.pmd.lang.ast.Node) ASTBlockStatement(net.sourceforge.pmd.lang.apex.ast.ASTBlockStatement) ASTMethodCallExpression(net.sourceforge.pmd.lang.apex.ast.ASTMethodCallExpression)

Example 2 with ASTBlockStatement

use of net.sourceforge.pmd.lang.apex.ast.ASTBlockStatement in project pmd by pmd.

the class ApexCRUDViolationRule method getPreviousMethodCalls.

private Set<ASTMethodCallExpression> getPreviousMethodCalls(final AbstractApexNode<?> self) {
    final Set<ASTMethodCallExpression> innerMethodCalls = new HashSet<>();
    final ASTMethod outerMethod = self.getFirstParentOfType(ASTMethod.class);
    if (outerMethod != null) {
        final ASTBlockStatement blockStatement = outerMethod.getFirstChildOfType(ASTBlockStatement.class);
        recursivelyEvaluateCRUDMethodCalls(self, innerMethodCalls, blockStatement);
        final List<ASTMethod> constructorMethods = findConstructorlMethods();
        for (ASTMethod method : constructorMethods) {
            innerMethodCalls.addAll(method.findDescendantsOfType(ASTMethodCallExpression.class));
        }
        // some methods might be within this class
        mapCallToMethodDecl(self, innerMethodCalls, new ArrayList<ASTMethodCallExpression>(innerMethodCalls));
    }
    return innerMethodCalls;
}
Also used : ASTMethod(net.sourceforge.pmd.lang.apex.ast.ASTMethod) ASTBlockStatement(net.sourceforge.pmd.lang.apex.ast.ASTBlockStatement) ASTMethodCallExpression(net.sourceforge.pmd.lang.apex.ast.ASTMethodCallExpression) HashSet(java.util.HashSet)

Example 3 with ASTBlockStatement

use of net.sourceforge.pmd.lang.apex.ast.ASTBlockStatement in project pmd by pmd.

the class ApexUnitTestClassShouldHaveAssertsRule method checkForAssertStatements.

private Object checkForAssertStatements(ApexNode<?> node, Object data) {
    final List<ASTBlockStatement> blockStatements = node.findDescendantsOfType(ASTBlockStatement.class);
    final List<ASTStatement> statements = new ArrayList<>();
    final List<ASTMethodCallExpression> methodCalls = new ArrayList<>();
    for (ASTBlockStatement blockStatement : blockStatements) {
        statements.addAll(blockStatement.findDescendantsOfType(ASTStatement.class));
        methodCalls.addAll(blockStatement.findDescendantsOfType(ASTMethodCallExpression.class));
    }
    boolean isAssertFound = false;
    for (final ASTMethodCallExpression methodCallExpression : methodCalls) {
        if (ASSERT_METHODS.contains(methodCallExpression.getFullMethodName().toLowerCase(Locale.ROOT))) {
            isAssertFound = true;
            break;
        }
    }
    if (!isAssertFound) {
        addViolation(data, node);
    }
    return data;
}
Also used : ASTBlockStatement(net.sourceforge.pmd.lang.apex.ast.ASTBlockStatement) ArrayList(java.util.ArrayList) ASTStatement(net.sourceforge.pmd.lang.apex.ast.ASTStatement) ASTMethodCallExpression(net.sourceforge.pmd.lang.apex.ast.ASTMethodCallExpression)

Aggregations

ASTBlockStatement (net.sourceforge.pmd.lang.apex.ast.ASTBlockStatement)3 ASTMethodCallExpression (net.sourceforge.pmd.lang.apex.ast.ASTMethodCallExpression)3 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 ASTIfElseBlockStatement (net.sourceforge.pmd.lang.apex.ast.ASTIfElseBlockStatement)1 ASTMethod (net.sourceforge.pmd.lang.apex.ast.ASTMethod)1 ASTStatement (net.sourceforge.pmd.lang.apex.ast.ASTStatement)1 AbstractApexNode (net.sourceforge.pmd.lang.apex.ast.AbstractApexNode)1 Node (net.sourceforge.pmd.lang.ast.Node)1