Search in sources :

Example 16 with ASTMethodCallExpression

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

the class ApexXSSFromURLParamRule method processVariableAssignments.

private void processVariableAssignments(AbstractApexNode<?> node, Object data, final boolean reverseOrder) {
    ASTMethodCallExpression methodCallAssignment = node.getFirstChildOfType(ASTMethodCallExpression.class);
    if (methodCallAssignment != null) {
        String varType = null;
        if (node instanceof ASTVariableDeclaration) {
            varType = ((ASTVariableDeclaration) node).getNode().getLocalInfo().getType().getApexName();
        }
        if (varType == null || !"id".equalsIgnoreCase(varType)) {
            processInlineMethodCalls(methodCallAssignment, data, false);
        }
    }
    List<ASTVariableExpression> nodes = node.findChildrenOfType(ASTVariableExpression.class);
    switch(nodes.size()) {
        case 1:
            {
                // Look for: foo + bar
                final List<ASTBinaryExpression> ops = node.findChildrenOfType(ASTBinaryExpression.class);
                if (!ops.isEmpty()) {
                    for (ASTBinaryExpression o : ops) {
                        processBinaryExpression(o, data);
                    }
                }
            }
            break;
        case 2:
            {
                // Look for: foo = bar;
                final ASTVariableExpression right = reverseOrder ? nodes.get(0) : nodes.get(1);
                if (urlParameterStrings.contains(Helper.getFQVariableName(right))) {
                    addViolation(data, right);
                }
            }
            break;
        default:
            break;
    }
}
Also used : ASTVariableExpression(net.sourceforge.pmd.lang.apex.ast.ASTVariableExpression) ASTBinaryExpression(net.sourceforge.pmd.lang.apex.ast.ASTBinaryExpression) ASTVariableDeclaration(net.sourceforge.pmd.lang.apex.ast.ASTVariableDeclaration) List(java.util.List) ASTMethodCallExpression(net.sourceforge.pmd.lang.apex.ast.ASTMethodCallExpression)

Example 17 with ASTMethodCallExpression

use of net.sourceforge.pmd.lang.apex.ast.ASTMethodCallExpression 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)

Example 18 with ASTMethodCallExpression

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

the class ApexBadCryptoRule method visit.

@Override
public Object visit(ASTUserClass node, Object data) {
    if (Helper.isTestMethodOrClass(node)) {
        return data;
    }
    List<ASTFieldDeclaration> fieldDecl = node.findDescendantsOfType(ASTFieldDeclaration.class);
    for (ASTFieldDeclaration var : fieldDecl) {
        findSafeVariables(var);
    }
    List<ASTVariableDeclaration> variableDecl = node.findDescendantsOfType(ASTVariableDeclaration.class);
    for (ASTVariableDeclaration var : variableDecl) {
        findSafeVariables(var);
    }
    List<ASTMethodCallExpression> methodCalls = node.findDescendantsOfType(ASTMethodCallExpression.class);
    for (ASTMethodCallExpression methodCall : methodCalls) {
        if (Helper.isMethodName(methodCall, CRYPTO, ENCRYPT) || Helper.isMethodName(methodCall, CRYPTO, DECRYPT) || Helper.isMethodName(methodCall, CRYPTO, ENCRYPT_WITH_MANAGED_IV) || Helper.isMethodName(methodCall, CRYPTO, DECRYPT_WITH_MANAGED_IV)) {
            validateStaticIVorKey(methodCall, data);
        }
    }
    potentiallyStaticBlob.clear();
    return data;
}
Also used : ASTVariableDeclaration(net.sourceforge.pmd.lang.apex.ast.ASTVariableDeclaration) ASTFieldDeclaration(net.sourceforge.pmd.lang.apex.ast.ASTFieldDeclaration) ASTMethodCallExpression(net.sourceforge.pmd.lang.apex.ast.ASTMethodCallExpression)

Aggregations

ASTMethodCallExpression (net.sourceforge.pmd.lang.apex.ast.ASTMethodCallExpression)18 ASTVariableExpression (net.sourceforge.pmd.lang.apex.ast.ASTVariableExpression)10 ASTVariableDeclaration (net.sourceforge.pmd.lang.apex.ast.ASTVariableDeclaration)7 ASTBinaryExpression (net.sourceforge.pmd.lang.apex.ast.ASTBinaryExpression)5 ASTMethod (net.sourceforge.pmd.lang.apex.ast.ASTMethod)4 ASTBlockStatement (net.sourceforge.pmd.lang.apex.ast.ASTBlockStatement)3 HashSet (java.util.HashSet)2 ASTAssignmentExpression (net.sourceforge.pmd.lang.apex.ast.ASTAssignmentExpression)2 ASTFieldDeclaration (net.sourceforge.pmd.lang.apex.ast.ASTFieldDeclaration)2 ASTLiteralExpression (net.sourceforge.pmd.lang.apex.ast.ASTLiteralExpression)2 ASTUserClass (net.sourceforge.pmd.lang.apex.ast.ASTUserClass)2 Identifier (apex.jorje.data.Identifier)1 VariableDeclaration (apex.jorje.semantic.ast.statement.VariableDeclaration)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ASTField (net.sourceforge.pmd.lang.apex.ast.ASTField)1 ASTIfElseBlockStatement (net.sourceforge.pmd.lang.apex.ast.ASTIfElseBlockStatement)1 ASTNewKeyValueObjectExpression (net.sourceforge.pmd.lang.apex.ast.ASTNewKeyValueObjectExpression)1 ASTReferenceExpression (net.sourceforge.pmd.lang.apex.ast.ASTReferenceExpression)1 ASTReturnStatement (net.sourceforge.pmd.lang.apex.ast.ASTReturnStatement)1