Search in sources :

Example 16 with ASTFormalParameter

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

the class MethodNameDeclaration method isVarargs.

public boolean isVarargs() {
    ASTFormalParameters params = (ASTFormalParameters) node.jjtGetChild(0);
    if (params.getParameterCount() == 0) {
        return false;
    }
    // If it's a varargs, it HAS to be the last parameter
    ASTFormalParameter p = (ASTFormalParameter) params.jjtGetChild(params.getParameterCount() - 1);
    return p.isVarargs();
}
Also used : ASTFormalParameters(net.sourceforge.pmd.lang.java.ast.ASTFormalParameters) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)

Example 17 with ASTFormalParameter

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

the class VariableNameDeclaration method isVarargs.

public boolean isVarargs() {
    ASTVariableDeclaratorId astVariableDeclaratorId = (ASTVariableDeclaratorId) node;
    ASTFormalParameter parameter = astVariableDeclaratorId.getFirstParentOfType(ASTFormalParameter.class);
    return parameter != null && parameter.isVarargs();
}
Also used : ASTVariableDeclaratorId(net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)

Example 18 with ASTFormalParameter

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

the class OverrideBothEqualsAndHashcodeRule method visit.

@Override
public Object visit(ASTMethodDeclarator node, Object data) {
    if (implementsComparable) {
        return data;
    }
    int iFormalParams = 0;
    String paramName = null;
    for (int ix = 0; ix < node.jjtGetNumChildren(); ix++) {
        Node sn = node.jjtGetChild(ix);
        if (sn instanceof ASTFormalParameters) {
            List<ASTFormalParameter> allParams = ((ASTFormalParameters) sn).findChildrenOfType(ASTFormalParameter.class);
            for (ASTFormalParameter formalParam : allParams) {
                iFormalParams++;
                ASTClassOrInterfaceType param = formalParam.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
                if (param != null) {
                    paramName = param.getImage();
                }
            }
        }
    }
    if (iFormalParams == 0 && node.hasImageEqualTo("hashCode")) {
        containsHashCode = true;
        nodeFound = node;
    } else if (iFormalParams == 1 && node.hasImageEqualTo("equals") && ("Object".equals(paramName) || "java.lang.Object".equals(paramName))) {
        containsEquals = true;
        nodeFound = node;
    }
    return super.visit(node, data);
}
Also used : Node(net.sourceforge.pmd.lang.ast.Node) ASTFormalParameters(net.sourceforge.pmd.lang.java.ast.ASTFormalParameters) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)

Example 19 with ASTFormalParameter

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

Example 20 with ASTFormalParameter

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

the class ConstructorCallsOverridableMethodRule method getMethodDeclaratorParameterTypes.

private static List<String> getMethodDeclaratorParameterTypes(Node methodOrConstructorDeclarator) {
    List<ASTFormalParameter> parameters = methodOrConstructorDeclarator.findDescendantsOfType(ASTFormalParameter.class);
    List<String> parameterTypes = new ArrayList<>();
    if (parameters != null) {
        for (ASTFormalParameter p : parameters) {
            ASTType type = p.getFirstChildOfType(ASTType.class);
            if (type.jjtGetChild(0) instanceof ASTPrimitiveType) {
                parameterTypes.add(type.jjtGetChild(0).getImage());
            } else if (type.jjtGetChild(0) instanceof ASTReferenceType) {
                parameterTypes.add("ref");
            } else {
                parameterTypes.add("<unkown>");
            }
        }
    }
    return parameterTypes;
}
Also used : ASTPrimitiveType(net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType) ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) ArrayList(java.util.ArrayList) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter) ASTReferenceType(net.sourceforge.pmd.lang.java.ast.ASTReferenceType)

Aggregations

ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)21 ASTFormalParameters (net.sourceforge.pmd.lang.java.ast.ASTFormalParameters)8 Node (net.sourceforge.pmd.lang.ast.Node)7 ASTPrimitiveType (net.sourceforge.pmd.lang.java.ast.ASTPrimitiveType)5 ASTMethodDeclarator (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator)4 ASTType (net.sourceforge.pmd.lang.java.ast.ASTType)4 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)4 ArrayList (java.util.ArrayList)3 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)3 Test (org.junit.Test)3 List (java.util.List)2 Map (java.util.Map)2 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)2 ASTReferenceType (net.sourceforge.pmd.lang.java.ast.ASTReferenceType)2 ASTTryStatement (net.sourceforge.pmd.lang.java.ast.ASTTryStatement)2 VariableNameDeclaration (net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration)2 InvalidObjectException (java.io.InvalidObjectException)1 ObjectInputStream (java.io.ObjectInputStream)1 RuleContext (net.sourceforge.pmd.RuleContext)1 AbstractNode (net.sourceforge.pmd.lang.ast.AbstractNode)1