Search in sources :

Example 11 with ASTExpression

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

the class NpathBaseVisitor method visit.

@Override
public Object visit(ASTReturnStatement node, Object data) {
    // return statements are valued at 1, or the value of the boolean expression
    ASTExpression expr = node.getFirstChildOfType(ASTExpression.class);
    if (expr == null) {
        return 1;
    }
    int boolCompReturn = CycloMetric.booleanExpressionComplexity(expr);
    int conditionalExpressionComplexity = multiplyChildrenComplexities(expr, data);
    if (conditionalExpressionComplexity > 1) {
        boolCompReturn += conditionalExpressionComplexity;
    }
    return boolCompReturn > 0 ? boolCompReturn : 1;
}
Also used : ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Example 12 with ASTExpression

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

the class ForLoopCanBeForeachRule method visit.

@Override
public Object visit(ASTForStatement node, Object data) {
    final ASTForInit init = node.getFirstChildOfType(ASTForInit.class);
    final ASTForUpdate update = node.getFirstChildOfType(ASTForUpdate.class);
    final ASTExpression guardCondition = node.getFirstChildOfType(ASTExpression.class);
    if (init == null && update == null || guardCondition == null) {
        return data;
    }
    Entry<VariableNameDeclaration, List<NameOccurrence>> indexDecl = getIndexVarDeclaration(init, update);
    if (indexDecl == null) {
        return data;
    }
    List<NameOccurrence> occurrences = indexDecl.getValue();
    VariableNameDeclaration index = indexDecl.getKey();
    if (TypeHelper.isA(index, Iterator.class)) {
        Entry<VariableNameDeclaration, List<NameOccurrence>> iterableInfo = getIterableDeclOfIteratorLoop(index, node.getScope());
        if (iterableInfo != null && isReplaceableIteratorLoop(indexDecl, guardCondition, iterableInfo, node)) {
            addViolation(data, node);
        }
        return data;
    }
    if (occurrences == null || !"int".equals(index.getTypeImage()) || !indexStartsAtZero(index)) {
        return data;
    }
    String itName = index.getName();
    String iterableName = getIterableNameOrNullToAbort(guardCondition, itName);
    if (!isForUpdateSimpleEnough(update, itName) || iterableName == null) {
        return data;
    }
    Entry<VariableNameDeclaration, List<NameOccurrence>> iterableInfo = findDeclaration(iterableName, node.getScope());
    VariableNameDeclaration iterableDeclaration = iterableInfo == null ? null : iterableInfo.getKey();
    if (iterableDeclaration == null) {
        return data;
    }
    if (iterableDeclaration.isArray() && isReplaceableArrayLoop(node, occurrences, iterableDeclaration)) {
        addViolation(data, node);
    } else if (iterableDeclaration.getTypeImage() != null && iterableDeclaration.getTypeImage().matches("List|ArrayList|LinkedList") && isReplaceableListLoop(node, occurrences, iterableDeclaration)) {
        addViolation(data, node);
    }
    return data;
}
Also used : VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTForInit(net.sourceforge.pmd.lang.java.ast.ASTForInit) List(java.util.List) ASTForUpdate(net.sourceforge.pmd.lang.java.ast.ASTForUpdate) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 13 with ASTExpression

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

the class StatementAndBraceFinder method addForExpressionNode.

/*
     * The method handles the special "for" loop. It creates always an
     * expression node even if the loop looks like for(;;).
     */
private void addForExpressionNode(Node node, Structure dataFlow) {
    ASTForStatement parent = (ASTForStatement) node.jjtGetParent();
    boolean hasExpressionChild = false;
    boolean hasForInitNode = false;
    boolean hasForUpdateNode = false;
    for (int i = 0; i < parent.jjtGetNumChildren(); i++) {
        if (parent.jjtGetChild(i) instanceof ASTExpression) {
            hasExpressionChild = true;
        } else if (parent.jjtGetChild(i) instanceof ASTForUpdate) {
            hasForUpdateNode = true;
        } else if (parent.jjtGetChild(i) instanceof ASTForInit) {
            hasForInitNode = true;
        }
    }
    if (!hasExpressionChild) {
        if (node instanceof ASTForInit) {
            dataFlow.createNewNode(node);
            dataFlow.pushOnStack(NodeType.FOR_EXPR, dataFlow.getLast());
            tryToLog(NodeType.FOR_EXPR, node);
        } else if (node instanceof ASTForUpdate) {
            if (!hasForInitNode) {
                dataFlow.createNewNode(node);
                dataFlow.pushOnStack(NodeType.FOR_EXPR, dataFlow.getLast());
                tryToLog(NodeType.FOR_EXPR, node);
            }
        } else if (node instanceof ASTStatement) {
            if (!hasForInitNode && !hasForUpdateNode) {
                dataFlow.createNewNode(node);
                dataFlow.pushOnStack(NodeType.FOR_EXPR, dataFlow.getLast());
                tryToLog(NodeType.FOR_EXPR, node);
            }
        }
    }
}
Also used : ASTForInit(net.sourceforge.pmd.lang.java.ast.ASTForInit) ASTForStatement(net.sourceforge.pmd.lang.java.ast.ASTForStatement) ASTForUpdate(net.sourceforge.pmd.lang.java.ast.ASTForUpdate) ASTStatement(net.sourceforge.pmd.lang.java.ast.ASTStatement) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Example 14 with ASTExpression

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

the class NpathBaseVisitor method visit.

@Override
public Object visit(ASTConditionalExpression node, Object data) {
    if (node.isTernary()) {
        ASTExpression wrapper = new ASTExpression(Integer.MAX_VALUE);
        wrapper.jjtAddChild(node.jjtGetChild(0), 0);
        int boolCompTernary = CycloMetric.booleanExpressionComplexity(wrapper);
        return boolCompTernary + sumChildrenComplexities(node, data) - 1;
    }
    return 1;
}
Also used : ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Example 15 with ASTExpression

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

the class MethodTypeResolution method selectMethodsSecondPhase.

/**
 * Look for methods be method conversion.
 * https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2.3
 */
public static List<MethodType> selectMethodsSecondPhase(List<MethodType> methodsToSearch, ASTArgumentList argList) {
    // TODO: check if explicit type arguments are applicable to the type parameter bounds
    List<MethodType> selectedMethods = new ArrayList<>();
    final int argCount = argList == null ? 0 : argList.jjtGetNumChildren();
    for (int methodIndex = 0; methodIndex < methodsToSearch.size(); ++methodIndex) {
        MethodType methodType = methodsToSearch.get(methodIndex);
        if (!methodType.isParameterized()) {
            throw new ResolutionFailedException();
        }
        // vararg methods are considered fixed arity here, see 3rd phase
        if (getArity(methodType.getMethod()) == argCount) {
            // check method convertability of each argument to the corresponding parameter
            boolean methodIsApplicable = true;
            // try each arguments if it's method convertible
            for (int argIndex = 0; argIndex < argCount; ++argIndex) {
                if (!isMethodConvertible(methodType.getParameterTypes().get(argIndex), (ASTExpression) argList.jjtGetChild(argIndex))) {
                    methodIsApplicable = false;
                    break;
                }
            // TODO: add unchecked conversion in an else if branch
            }
            if (methodIsApplicable) {
                selectedMethods.add(methodType);
            }
        }
    }
    return selectedMethods;
}
Also used : ArrayList(java.util.ArrayList) ResolutionFailedException(net.sourceforge.pmd.lang.java.typeresolution.typeinference.TypeInferenceResolver.ResolutionFailedException) Constraint(net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Aggregations

ASTExpression (net.sourceforge.pmd.lang.java.ast.ASTExpression)26 Test (org.junit.Test)9 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)8 Constraint (net.sourceforge.pmd.lang.java.typeresolution.typeinference.Constraint)8 Node (net.sourceforge.pmd.lang.ast.Node)7 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)6 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)6 DataFlowNode (net.sourceforge.pmd.lang.dfa.DataFlowNode)3 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)3 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)3 ArrayList (java.util.ArrayList)2 ASTAssignmentOperator (net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator)2 ASTForInit (net.sourceforge.pmd.lang.java.ast.ASTForInit)2 ASTForUpdate (net.sourceforge.pmd.lang.java.ast.ASTForUpdate)2 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)2 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)2 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)2 ResolutionFailedException (net.sourceforge.pmd.lang.java.typeresolution.typeinference.TypeInferenceResolver.ResolutionFailedException)2 NameDeclaration (net.sourceforge.pmd.lang.symboltable.NameDeclaration)2 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)2