Search in sources :

Example 1 with ASTClassOrInterfaceBodyDeclaration

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

the class ClassScope method resolveGenericType.

/**
 * Tries to resolve a given typeImage as a generic Type. If the Generic Type
 * is found, any defined ClassOrInterfaceType below this type declaration is
 * used (this is typically a type bound, e.g. {@code <T extends List>}.
 *
 * @param argument
 *            the node, from where to start searching.
 * @param typeImage
 *            the type as string
 * @return the resolved class or <code>null</code> if nothing was found.
 */
private Class<?> resolveGenericType(Node argument, String typeImage) {
    List<ASTTypeParameter> types = new ArrayList<>();
    // first search only within the same method
    ASTClassOrInterfaceBodyDeclaration firstParentOfType = argument.getFirstParentOfType(ASTClassOrInterfaceBodyDeclaration.class);
    if (firstParentOfType != null) {
        types.addAll(firstParentOfType.findDescendantsOfType(ASTTypeParameter.class));
    }
    // then search class level types, from inner-most to outer-most
    List<ASTClassOrInterfaceDeclaration> enclosingClasses = argument.getParentsOfType(ASTClassOrInterfaceDeclaration.class);
    for (ASTClassOrInterfaceDeclaration enclosing : enclosingClasses) {
        ASTTypeParameters classLevelTypeParameters = enclosing.getFirstChildOfType(ASTTypeParameters.class);
        if (classLevelTypeParameters != null) {
            types.addAll(classLevelTypeParameters.findDescendantsOfType(ASTTypeParameter.class));
        }
    }
    return resolveGenericType(typeImage, types);
}
Also used : ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) ArrayList(java.util.ArrayList) ASTClassOrInterfaceBodyDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration) ASTTypeParameter(net.sourceforge.pmd.lang.java.ast.ASTTypeParameter) ASTTypeParameters(net.sourceforge.pmd.lang.java.ast.ASTTypeParameters)

Example 2 with ASTClassOrInterfaceBodyDeclaration

use of net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration 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 3 with ASTClassOrInterfaceBodyDeclaration

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

the class UnusedPrivateFieldRule method usedInOuter.

private boolean usedInOuter(NameDeclaration decl, JavaNode body) {
    List<ASTClassOrInterfaceBodyDeclaration> classOrInterfaceBodyDeclarations = body.findChildrenOfType(ASTClassOrInterfaceBodyDeclaration.class);
    List<ASTEnumConstant> enumConstants = body.findChildrenOfType(ASTEnumConstant.class);
    List<AbstractJavaNode> nodes = new ArrayList<>();
    nodes.addAll(classOrInterfaceBodyDeclarations);
    nodes.addAll(enumConstants);
    for (AbstractJavaNode node : nodes) {
        for (ASTPrimarySuffix primarySuffix : node.findDescendantsOfType(ASTPrimarySuffix.class, true)) {
            if (decl.getImage().equals(primarySuffix.getImage())) {
                // No violation
                return true;
            }
        }
        for (ASTPrimaryPrefix primaryPrefix : node.findDescendantsOfType(ASTPrimaryPrefix.class, true)) {
            ASTName name = primaryPrefix.getFirstDescendantOfType(ASTName.class);
            if (name != null) {
                for (String id : name.getImage().split("\\.")) {
                    if (id.equals(decl.getImage())) {
                        // No violation
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) AbstractJavaNode(net.sourceforge.pmd.lang.java.ast.AbstractJavaNode) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ArrayList(java.util.ArrayList) ASTClassOrInterfaceBodyDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration) ASTPrimarySuffix(net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix) ASTEnumConstant(net.sourceforge.pmd.lang.java.ast.ASTEnumConstant)

Example 4 with ASTClassOrInterfaceBodyDeclaration

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

the class CommentDefaultAccessModifierRule method hasNoVisibleForTestingAnnotation.

private boolean hasNoVisibleForTestingAnnotation(AbstractJavaAccessNode decl) {
    boolean result = true;
    ASTClassOrInterfaceBodyDeclaration parent = decl.getFirstParentOfType(ASTClassOrInterfaceBodyDeclaration.class);
    if (parent != null) {
        List<ASTAnnotation> annotations = parent.findChildrenOfType(ASTAnnotation.class);
        for (ASTAnnotation annotation : annotations) {
            final ASTName name = annotation.getFirstDescendantOfType(ASTName.class);
            if (name.hasImageEqualTo("VisibleForTesting")) {
                result = false;
                break;
            }
        }
    }
    return result;
}
Also used : ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTAnnotation(net.sourceforge.pmd.lang.java.ast.ASTAnnotation) ASTClassOrInterfaceBodyDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration)

Example 5 with ASTClassOrInterfaceBodyDeclaration

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

the class MethodNamingConventionsRule method isOverriddenMethod.

private boolean isOverriddenMethod(ASTMethodDeclarator node) {
    ASTClassOrInterfaceBodyDeclaration declaration = node.getFirstParentOfType(ASTClassOrInterfaceBodyDeclaration.class);
    List<ASTMarkerAnnotation> annotations = declaration.findDescendantsOfType(ASTMarkerAnnotation.class);
    for (ASTMarkerAnnotation ann : annotations) {
        ASTName name = ann.getFirstChildOfType(ASTName.class);
        if (name != null && name.hasImageEqualTo("Override")) {
            return true;
        }
    }
    return false;
}
Also used : ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTClassOrInterfaceBodyDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration) ASTMarkerAnnotation(net.sourceforge.pmd.lang.java.ast.ASTMarkerAnnotation)

Aggregations

ASTClassOrInterfaceBodyDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration)5 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)4 ArrayList (java.util.ArrayList)2 ASTAnnotation (net.sourceforge.pmd.lang.java.ast.ASTAnnotation)2 ASTMarkerAnnotation (net.sourceforge.pmd.lang.java.ast.ASTMarkerAnnotation)2 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)2 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)2 Node (net.sourceforge.pmd.lang.ast.Node)1 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)1 ASTArguments (net.sourceforge.pmd.lang.java.ast.ASTArguments)1 ASTBlock (net.sourceforge.pmd.lang.java.ast.ASTBlock)1 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)1 ASTEnumConstant (net.sourceforge.pmd.lang.java.ast.ASTEnumConstant)1 ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)1 ASTFormalParameters (net.sourceforge.pmd.lang.java.ast.ASTFormalParameters)1 ASTMethodDeclarator (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator)1 ASTPrimaryExpression (net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression)1 ASTStatement (net.sourceforge.pmd.lang.java.ast.ASTStatement)1 ASTTypeParameter (net.sourceforge.pmd.lang.java.ast.ASTTypeParameter)1 ASTTypeParameters (net.sourceforge.pmd.lang.java.ast.ASTTypeParameters)1