Search in sources :

Example 6 with ASTArgumentList

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

the class GuardLogStatementRule method getLogLevelName.

/**
 * Determines the log level, that is used. It is either the called method name
 * itself or - if the method has a first argument a primary prefix - the first argument.
 *
 * @param node the method call
 * @param methodCallName the called method name previously determined
 * @return the log level
 */
private String getLogLevelName(Node node, String methodCallName) {
    String logLevel = methodCallName;
    ASTPrimarySuffix suffix = node.getFirstDescendantOfType(ASTPrimarySuffix.class);
    if (suffix != null) {
        ASTArgumentList argumentList = suffix.getFirstDescendantOfType(ASTArgumentList.class);
        if (argumentList != null && argumentList.hasDescendantOfType(ASTName.class)) {
            ASTName name = argumentList.getFirstDescendantOfType(ASTName.class);
            String lastPart = getLastPartOfName(name.getImage());
            lastPart = lastPart.toLowerCase(Locale.ROOT);
            if (!lastPart.isEmpty()) {
                logLevel = lastPart;
            }
        }
    }
    return logLevel;
}
Also used : ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList)

Example 7 with ASTArgumentList

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

the class ClassTypeResolver method visit.

@Override
public Object visit(ASTName node, Object data) {
    Class<?> accessingClass = getEnclosingTypeDeclarationClass(node);
    String[] dotSplitImage = node.getImage().split("\\.");
    int startIndex = searchNodeNameForClass(node);
    ASTArguments astArguments = getSuffixMethodArgs(node);
    ASTArgumentList astArgumentList = getArgumentList(astArguments);
    int methodArgsArity = getArgumentListArity(astArgumentList);
    JavaTypeDefinition previousType;
    if (node.getType() != null) {
        // static field or method
        // node.getType() has been set by the call to searchNodeNameForClass above
        // node.getType() will have the value equal to the Class found by that method
        previousType = node.getTypeDefinition();
    } else {
        // non-static field or method
        if (dotSplitImage.length == 1 && astArguments != null) {
            // method
            List<MethodType> methods = getLocalApplicableMethods(node, dotSplitImage[0], Collections.<JavaTypeDefinition>emptyList(), methodArgsArity, accessingClass);
            TypeNode enclosingType = getEnclosingTypeDeclaration(node);
            if (enclosingType == null) {
                // we can't proceed, probably uncompiled sources
                return data;
            }
            previousType = getBestMethodReturnType(enclosingType.getTypeDefinition(), methods, astArgumentList);
        } else {
            // field
            previousType = getTypeDefinitionOfVariableFromScope(node.getScope(), dotSplitImage[0], accessingClass);
        }
        // first element's type in dotSplitImage has already been resolved
        startIndex = 1;
    }
    // as the code is not compiled there and symbol table works on uncompiled code
    if (node.getNameDeclaration() != null && // if it's not null, then let other code handle things
    previousType == null && node.getNameDeclaration().getNode() instanceof TypeNode) {
        // Carry over the type from the declaration
        Class<?> nodeType = ((TypeNode) node.getNameDeclaration().getNode()).getType();
        // FIXME : generic classes and class with generic super types could have the wrong type assigned here
        if (nodeType != null) {
            node.setType(nodeType);
            return super.visit(node, data);
        }
    }
    for (int i = startIndex; i < dotSplitImage.length; ++i) {
        if (previousType == null) {
            break;
        }
        if (i == dotSplitImage.length - 1 && astArguments != null) {
            // method
            List<MethodType> methods = getApplicableMethods(previousType, dotSplitImage[i], Collections.<JavaTypeDefinition>emptyList(), methodArgsArity, accessingClass);
            previousType = getBestMethodReturnType(previousType, methods, astArgumentList);
        } else {
            // field
            previousType = getFieldType(previousType, dotSplitImage[i], accessingClass);
        }
    }
    if (previousType != null) {
        node.setTypeDefinition(previousType);
    }
    return super.visit(node, data);
}
Also used : JavaTypeDefinition(net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition) TypeNode(net.sourceforge.pmd.lang.java.ast.TypeNode) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode) ASTArguments(net.sourceforge.pmd.lang.java.ast.ASTArguments) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList)

Example 8 with ASTArgumentList

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

the class ClassTypeResolver method visit.

@Override
public Object visit(ASTPrimaryExpression primaryNode, Object data) {
    // visit method arguments in reverse
    for (int i = primaryNode.jjtGetNumChildren() - 1; i >= 0; --i) {
        ((JavaNode) primaryNode.jjtGetChild(i)).jjtAccept(this, data);
    }
    JavaTypeDefinition primaryNodeType = null;
    AbstractJavaTypeNode previousChild = null;
    AbstractJavaTypeNode nextChild;
    Class<?> accessingClass = getEnclosingTypeDeclarationClass(primaryNode);
    for (int childIndex = 0; childIndex < primaryNode.jjtGetNumChildren(); ++childIndex) {
        AbstractJavaTypeNode currentChild = (AbstractJavaTypeNode) primaryNode.jjtGetChild(childIndex);
        nextChild = childIndex + 1 < primaryNode.jjtGetNumChildren() ? (AbstractJavaTypeNode) primaryNode.jjtGetChild(childIndex + 1) : null;
        // skip children which already have their type assigned
        if (currentChild.getType() == null) {
            // Last token, because if 'this' is a Suffix, it'll have tokens '.' and 'this'
            if (currentChild.jjtGetLastToken().toString().equals("this")) {
                if (previousChild != null) {
                    // Qualified 'this' expression
                    currentChild.setTypeDefinition(previousChild.getTypeDefinition());
                } else {
                    // simple 'this' expression
                    ASTClassOrInterfaceDeclaration typeDeclaration = currentChild.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
                    if (typeDeclaration != null) {
                        currentChild.setTypeDefinition(typeDeclaration.getTypeDefinition());
                    }
                }
            // Last token, because if 'super' is a Suffix, it'll have tokens '.' and 'super'
            } else if (currentChild.jjtGetLastToken().toString().equals("super")) {
                if (previousChild != null) {
                    // Qualified 'super' expression
                    // anonymous classes can't have qualified super expression, thus
                    // getSuperClassTypeDefinition's second argumet isn't null, but we are not
                    // looking for enclosing super types
                    currentChild.setTypeDefinition(getSuperClassTypeDefinition(currentChild, previousChild.getType()));
                } else {
                    // simple 'super' expression
                    currentChild.setTypeDefinition(getSuperClassTypeDefinition(currentChild, null));
                }
            } else if (currentChild.getFirstChildOfType(ASTArguments.class) != null) {
                currentChild.setTypeDefinition(previousChild.getTypeDefinition());
            } else if (previousChild != null && previousChild.getType() != null) {
                String currentChildImage = currentChild.getImage();
                if (currentChildImage == null) {
                    // this.<Something>foo(); <Something>foo would be in a Suffix and would have a null image
                    currentChildImage = currentChild.jjtGetLastToken().toString();
                }
                ASTArguments astArguments = nextChild != null ? nextChild.getFirstChildOfType(ASTArguments.class) : null;
                if (astArguments != null) {
                    // method
                    ASTArgumentList astArgumentList = getArgumentList(astArguments);
                    int methodArgsArity = getArgumentListArity(astArgumentList);
                    List<JavaTypeDefinition> typeArguments = getMethodExplicitTypeArugments(currentChild);
                    List<MethodType> methods = getApplicableMethods(previousChild.getTypeDefinition(), currentChildImage, typeArguments, methodArgsArity, accessingClass);
                    currentChild.setTypeDefinition(getBestMethodReturnType(previousChild.getTypeDefinition(), methods, astArgumentList));
                } else {
                    // field
                    currentChild.setTypeDefinition(getFieldType(previousChild.getTypeDefinition(), currentChildImage, accessingClass));
                }
            }
        }
        if (currentChild.getType() != null) {
            primaryNodeType = currentChild.getTypeDefinition();
        } else {
            // avoid falsely passing tests
            primaryNodeType = null;
            break;
        }
        previousChild = currentChild;
    }
    primaryNode.setTypeDefinition(primaryNodeType);
    return data;
}
Also used : ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) JavaTypeDefinition(net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition) ASTExtendsList(net.sourceforge.pmd.lang.java.ast.ASTExtendsList) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) List(java.util.List) ArrayList(java.util.ArrayList) ASTArguments(net.sourceforge.pmd.lang.java.ast.ASTArguments) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode) AbstractJavaTypeNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode)

Example 9 with ASTArgumentList

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

the class UseStringBufferForStringAppendsRule method visit.

@Override
public Object visit(ASTVariableDeclaratorId node, Object data) {
    if (!TypeHelper.isA(node, String.class) || node.isArray()) {
        return data;
    }
    Node parent = node.jjtGetParent().jjtGetParent();
    if (!(parent instanceof ASTLocalVariableDeclaration)) {
        return data;
    }
    for (NameOccurrence no : node.getUsages()) {
        Node name = no.getLocation();
        ASTStatementExpression statement = name.getFirstParentOfType(ASTStatementExpression.class);
        if (statement == null) {
            continue;
        }
        ASTArgumentList argList = name.getFirstParentOfType(ASTArgumentList.class);
        if (argList != null && argList.getFirstParentOfType(ASTStatementExpression.class) == statement) {
            // used in method call
            continue;
        }
        ASTEqualityExpression equality = name.getFirstParentOfType(ASTEqualityExpression.class);
        if (equality != null && equality.getFirstParentOfType(ASTStatementExpression.class) == statement) {
            // used in condition
            continue;
        }
        ASTConditionalExpression conditional = name.getFirstParentOfType(ASTConditionalExpression.class);
        if (conditional != null) {
            Node thirdParent = name.getNthParent(3);
            Node fourthParent = name.getNthParent(4);
            if ((Objects.equals(thirdParent, conditional) || Objects.equals(fourthParent, conditional)) && conditional.getFirstParentOfType(ASTStatementExpression.class) == statement) {
                // string)
                continue;
            }
        }
        if (statement.jjtGetNumChildren() > 0 && statement.jjtGetChild(0) instanceof ASTPrimaryExpression) {
            ASTName astName = statement.jjtGetChild(0).getFirstDescendantOfType(ASTName.class);
            if (astName != null) {
                if (astName.equals(name)) {
                    ASTAssignmentOperator assignmentOperator = statement.getFirstDescendantOfType(ASTAssignmentOperator.class);
                    if (assignmentOperator != null && assignmentOperator.isCompound()) {
                        addViolation(data, assignmentOperator);
                    }
                } else if (astName.getImage().equals(name.getImage())) {
                    ASTAssignmentOperator assignmentOperator = statement.getFirstDescendantOfType(ASTAssignmentOperator.class);
                    if (assignmentOperator != null && !assignmentOperator.isCompound()) {
                        addViolation(data, astName);
                    }
                }
            }
        }
    }
    return data;
}
Also used : ASTEqualityExpression(net.sourceforge.pmd.lang.java.ast.ASTEqualityExpression) ASTAssignmentOperator(net.sourceforge.pmd.lang.java.ast.ASTAssignmentOperator) ASTLocalVariableDeclaration(net.sourceforge.pmd.lang.java.ast.ASTLocalVariableDeclaration) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) Node(net.sourceforge.pmd.lang.ast.Node) ASTConditionalExpression(net.sourceforge.pmd.lang.java.ast.ASTConditionalExpression) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTStatementExpression(net.sourceforge.pmd.lang.java.ast.ASTStatementExpression) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 10 with ASTArgumentList

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

the class UselessOverridingMethodRule method visit.

@Override
public Object visit(ASTMethodDeclaration node, Object data) {
    // them.
    if (node.isAbstract() || node.isFinal() || node.isNative() || node.isSynchronized()) {
        return super.visit(node, data);
    }
    // implement them anyway ( see bug 1522517)
    if (CLONE.equals(node.getMethodName()) && node.isPublic() && !this.hasArguments(node) && this.isMethodType(node, OBJECT) && this.isMethodThrowingType(node, exceptions)) {
        return super.visit(node, data);
    }
    ASTBlock block = node.getBlock();
    if (block == null) {
        return super.visit(node, data);
    }
    // Only process functions with one BlockStatement
    if (block.jjtGetNumChildren() != 1 || block.findDescendantsOfType(ASTStatement.class).size() != 1) {
        return super.visit(node, data);
    }
    Node statement = block.jjtGetChild(0).jjtGetChild(0);
    if (statement.jjtGetChild(0).jjtGetNumChildren() == 0) {
        // skips empty return statements
        return data;
    }
    Node statementGrandChild = statement.jjtGetChild(0).jjtGetChild(0);
    ASTPrimaryExpression primaryExpression;
    if (statementGrandChild instanceof ASTPrimaryExpression) {
        primaryExpression = (ASTPrimaryExpression) statementGrandChild;
    } else {
        List<ASTPrimaryExpression> primaryExpressions = findFirstDegreeChildrenOfType(statementGrandChild, ASTPrimaryExpression.class);
        if (primaryExpressions.size() != 1) {
            return super.visit(node, data);
        }
        primaryExpression = primaryExpressions.get(0);
    }
    ASTPrimaryPrefix primaryPrefix = findFirstDegreeChildrenOfType(primaryExpression, ASTPrimaryPrefix.class).get(0);
    if (!primaryPrefix.usesSuperModifier()) {
        return super.visit(node, data);
    }
    List<ASTPrimarySuffix> primarySuffixList = findFirstDegreeChildrenOfType(primaryExpression, ASTPrimarySuffix.class);
    if (primarySuffixList.size() != 2) {
        // extra method call on result of super method
        return super.visit(node, data);
    }
    ASTMethodDeclarator methodDeclarator = findFirstDegreeChildrenOfType(node, ASTMethodDeclarator.class).get(0);
    ASTPrimarySuffix primarySuffix = primarySuffixList.get(0);
    if (!primarySuffix.hasImageEqualTo(methodDeclarator.getImage())) {
        return super.visit(node, data);
    }
    // Process arguments
    primarySuffix = primarySuffixList.get(1);
    ASTArguments arguments = (ASTArguments) primarySuffix.jjtGetChild(0);
    ASTFormalParameters formalParameters = (ASTFormalParameters) methodDeclarator.jjtGetChild(0);
    if (formalParameters.jjtGetNumChildren() != arguments.jjtGetNumChildren()) {
        return super.visit(node, data);
    }
    if (!ignoreAnnotations) {
        ASTClassOrInterfaceBodyDeclaration parent = (ASTClassOrInterfaceBodyDeclaration) node.jjtGetParent();
        for (int i = 0; i < parent.jjtGetNumChildren(); i++) {
            Node n = parent.jjtGetChild(i);
            if (n instanceof ASTAnnotation) {
                if (n.jjtGetChild(0) instanceof ASTMarkerAnnotation) {
                    // @Override is ignored
                    if ("Override".equals(((ASTName) n.jjtGetChild(0).jjtGetChild(0)).getImage())) {
                        continue;
                    }
                }
                return super.visit(node, data);
            }
        }
    }
    if (arguments.jjtGetNumChildren() == 0) {
        addViolation(data, node, getMessage());
    } else {
        ASTArgumentList argumentList = (ASTArgumentList) arguments.jjtGetChild(0);
        for (int i = 0; i < argumentList.jjtGetNumChildren(); i++) {
            Node expressionChild = argumentList.jjtGetChild(i).jjtGetChild(0);
            if (!(expressionChild instanceof ASTPrimaryExpression) || expressionChild.jjtGetNumChildren() != 1) {
                // The arguments are not simply passed through
                return super.visit(node, data);
            }
            ASTPrimaryExpression argumentPrimaryExpression = (ASTPrimaryExpression) expressionChild;
            ASTPrimaryPrefix argumentPrimaryPrefix = (ASTPrimaryPrefix) argumentPrimaryExpression.jjtGetChild(0);
            if (argumentPrimaryPrefix.jjtGetNumChildren() == 0) {
                // The arguments are not simply passed through (using "this" for instance)
                return super.visit(node, data);
            }
            Node argumentPrimaryPrefixChild = argumentPrimaryPrefix.jjtGetChild(0);
            if (!(argumentPrimaryPrefixChild instanceof ASTName)) {
                // The arguments are not simply passed through
                return super.visit(node, data);
            }
            if (formalParameters.jjtGetNumChildren() < i + 1) {
                // different number of args
                return super.visit(node, data);
            }
            ASTName argumentName = (ASTName) argumentPrimaryPrefixChild;
            ASTFormalParameter formalParameter = (ASTFormalParameter) formalParameters.jjtGetChild(i);
            ASTVariableDeclaratorId variableId = findFirstDegreeChildrenOfType(formalParameter, ASTVariableDeclaratorId.class).get(0);
            if (!argumentName.hasImageEqualTo(variableId.getImage())) {
                // The arguments are not simply passed through
                return super.visit(node, data);
            }
        }
        // All arguments are passed through directly
        addViolation(data, node, getMessage());
    }
    return super.visit(node, data);
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) Node(net.sourceforge.pmd.lang.ast.Node) ASTFormalParameters(net.sourceforge.pmd.lang.java.ast.ASTFormalParameters) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTArguments(net.sourceforge.pmd.lang.java.ast.ASTArguments) ASTClassOrInterfaceBodyDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix) ASTMarkerAnnotation(net.sourceforge.pmd.lang.java.ast.ASTMarkerAnnotation) ASTStatement(net.sourceforge.pmd.lang.java.ast.ASTStatement) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) ASTMethodDeclarator(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator) ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) ASTBlock(net.sourceforge.pmd.lang.java.ast.ASTBlock) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTAnnotation(net.sourceforge.pmd.lang.java.ast.ASTAnnotation)

Aggregations

ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)14 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)9 Node (net.sourceforge.pmd.lang.ast.Node)7 ArrayList (java.util.ArrayList)6 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)4 List (java.util.List)3 ASTAllocationExpression (net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression)3 ASTArguments (net.sourceforge.pmd.lang.java.ast.ASTArguments)3 ASTLiteral (net.sourceforge.pmd.lang.java.ast.ASTLiteral)3 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)3 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)3 JavaTypeDefinition (net.sourceforge.pmd.lang.java.typeresolution.typedefinition.JavaTypeDefinition)3 Method (java.lang.reflect.Method)2 ASTBooleanLiteral (net.sourceforge.pmd.lang.java.ast.ASTBooleanLiteral)2 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)2 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)2 ASTExtendsList (net.sourceforge.pmd.lang.java.ast.ASTExtendsList)2 ASTStatementExpression (net.sourceforge.pmd.lang.java.ast.ASTStatementExpression)2 AbstractJavaNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaNode)2 AbstractJavaTypeNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode)2