Search in sources :

Example 16 with ASTExpression

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

the class MethodTypeResolution method selectMethodsThirdPhase.

/**
 * Look for methods considering varargs as well.
 * https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.12.2.4
 */
public static List<MethodType> selectMethodsThirdPhase(List<MethodType> methodsToSearch, ASTArgumentList argList) {
    // TODO: check if explicit type arguments are applicable to the type parameter bounds
    List<MethodType> selectedMethods = new ArrayList<>();
    for (int methodIndex = 0; methodIndex < methodsToSearch.size(); ++methodIndex) {
        MethodType methodType = methodsToSearch.get(methodIndex);
        if (!methodType.isParameterized()) {
            throw new ResolutionFailedException();
        }
        // if we reach here and the method is not a vararg, then we didn't find a resolution in earlier phases
        if (methodType.isVararg()) {
            // check subtypeability of each argument to the corresponding parameter
            boolean methodIsApplicable = true;
            List<JavaTypeDefinition> methodParameters = methodType.getParameterTypes();
            JavaTypeDefinition varargComponentType = methodType.getVarargComponentType();
            if (argList == null) {
                // There are no arguments, make sure the method has only a vararg
                methodIsApplicable = getArity(methodType.getMethod()) == 1;
            } else {
                // try each arguments if it's method convertible
                for (int argIndex = 0; argIndex < argList.jjtGetNumChildren(); ++argIndex) {
                    JavaTypeDefinition parameterType = argIndex < methodParameters.size() - 1 ? methodParameters.get(argIndex) : varargComponentType;
                    if (!isMethodConvertible(parameterType, (ASTExpression) argList.jjtGetChild(argIndex))) {
                        methodIsApplicable = false;
                        break;
                    }
                // TODO: If k != n, or if k = n and An cannot be converted by method invocation conversion to
                // Sn[], then the type which is the erasure (ยง4.6) of Sn is accessible at the point of invocation.
                // TODO: add unchecked conversion in an else if branch
                }
            }
            if (methodIsApplicable) {
                selectedMethods.add(methodType);
            }
        } else {
            // TODO: Remove check for vararg here, once we can detect and use return types of method calls
            LOG.log(Level.FINE, "Method {0} couldn't be resolved", String.valueOf(methodType));
        }
    }
    return selectedMethods;
}
Also used : JavaTypeDefinition(net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition) 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)

Example 17 with ASTExpression

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

the class JavaNameOccurrence method isSelfAssignment.

/**
 * Assert it the occurrence is a self assignment such as:
 * <code>i += 3;</code>
 *
 * @return true, if the occurrence is self-assignment, false, otherwise.
 */
@SuppressWarnings("PMD.AvoidBranchingStatementAsLastInLoop")
public boolean isSelfAssignment() {
    Node l = location;
    while (true) {
        Node p = l.jjtGetParent();
        Node gp = p.jjtGetParent();
        Node node = gp.jjtGetParent();
        if (node instanceof ASTPreDecrementExpression || node instanceof ASTPreIncrementExpression || node instanceof ASTPostfixExpression) {
            return true;
        }
        if (hasAssignmentOperator(gp)) {
            return isCompoundAssignment(gp);
        }
        if (hasAssignmentOperator(node)) {
            return isCompoundAssignment(node);
        }
        // deal with extra parenthesis: "(i)++"
        if (p instanceof ASTPrimaryPrefix && p.jjtGetNumChildren() == 1 && gp instanceof ASTPrimaryExpression && gp.jjtGetNumChildren() == 1 && node instanceof ASTExpression && node.jjtGetNumChildren() == 1 && node.jjtGetParent() instanceof ASTPrimaryPrefix && node.jjtGetParent().jjtGetNumChildren() == 1) {
            l = node;
            continue;
        }
        // catch this.i++ or ++this.i
        return gp instanceof ASTPreDecrementExpression || gp instanceof ASTPreIncrementExpression || gp instanceof ASTPostfixExpression;
    }
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTPostfixExpression(net.sourceforge.pmd.lang.java.ast.ASTPostfixExpression) Node(net.sourceforge.pmd.lang.ast.Node) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTPreDecrementExpression(net.sourceforge.pmd.lang.java.ast.ASTPreDecrementExpression) ASTPreIncrementExpression(net.sourceforge.pmd.lang.java.ast.ASTPreIncrementExpression) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Example 18 with ASTExpression

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

the class BrokenNullCheckRule method visit.

@Override
public Object visit(ASTIfStatement node, Object data) {
    ASTExpression expression = (ASTExpression) node.jjtGetChild(0);
    ASTConditionalAndExpression conditionalAndExpression = expression.getFirstDescendantOfType(ASTConditionalAndExpression.class);
    if (conditionalAndExpression != null) {
        checkForViolations(node, data, conditionalAndExpression);
    }
    ASTConditionalOrExpression conditionalOrExpression = expression.getFirstDescendantOfType(ASTConditionalOrExpression.class);
    if (conditionalOrExpression != null) {
        checkForViolations(node, data, conditionalOrExpression);
    }
    return super.visit(node, data);
}
Also used : ASTConditionalOrExpression(net.sourceforge.pmd.lang.java.ast.ASTConditionalOrExpression) ASTConditionalAndExpression(net.sourceforge.pmd.lang.java.ast.ASTConditionalAndExpression) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Example 19 with ASTExpression

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

the class InvalidSlf4jMessageFormatRule method visit.

@Override
public Object visit(final ASTName node, final Object data) {
    final NameDeclaration nameDeclaration = node.getNameDeclaration();
    // ignore imports or methods
    if (!(nameDeclaration instanceof VariableNameDeclaration)) {
        return super.visit(node, data);
    }
    // ignore non slf4j logger
    Class<?> type = ((VariableNameDeclaration) nameDeclaration).getType();
    if (type == null || !type.getName().equals(LOGGER_CLASS)) {
        return super.visit(node, data);
    }
    // get the node that contains the logger
    final ASTPrimaryExpression parentNode = node.getFirstParentOfType(ASTPrimaryExpression.class);
    // get the log level
    final String method = parentNode.getFirstChildOfType(ASTPrimaryPrefix.class).getFirstChildOfType(ASTName.class).getImage().replace(nameDeclaration.getImage() + ".", "");
    // ignore if not a log level
    if (!LOGGER_LEVELS.contains(method)) {
        return super.visit(node, data);
    }
    // find the arguments
    final List<ASTExpression> argumentList = parentNode.getFirstChildOfType(ASTPrimarySuffix.class).getFirstDescendantOfType(ASTArgumentList.class).findChildrenOfType(ASTExpression.class);
    // remove the message parameter
    final ASTPrimaryExpression messageParam = argumentList.remove(0).getFirstDescendantOfType(ASTPrimaryExpression.class);
    final int expectedArguments = expectedArguments(messageParam);
    if (expectedArguments == 0) {
        // or if we couldn't analyze the message parameter
        return super.visit(node, data);
    }
    // But only, if it is not used as a placeholder argument
    if (argumentList.size() > expectedArguments) {
        removeThrowableParam(argumentList);
    }
    if (argumentList.size() < expectedArguments) {
        addViolationWithMessage(data, node, "Missing arguments," + getExpectedMessage(argumentList, expectedArguments));
    } else if (argumentList.size() > expectedArguments) {
        addViolationWithMessage(data, node, "Too many arguments," + getExpectedMessage(argumentList, expectedArguments));
    }
    return super.visit(node, data);
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) ASTExpression(net.sourceforge.pmd.lang.java.ast.ASTExpression)

Example 20 with ASTExpression

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

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