Search in sources :

Example 1 with ASTImplementsList

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

the class ClassScope method determineSuper.

private SimpleTypedNameDeclaration determineSuper(Node declaringNode) {
    SimpleTypedNameDeclaration result = null;
    if (declaringNode instanceof ASTClassOrInterfaceDeclaration) {
        ASTClassOrInterfaceDeclaration classDeclaration = (ASTClassOrInterfaceDeclaration) declaringNode;
        ASTImplementsList implementsList = classDeclaration.getFirstChildOfType(ASTImplementsList.class);
        if (implementsList != null) {
            List<ASTClassOrInterfaceType> types = implementsList.findChildrenOfType(ASTClassOrInterfaceType.class);
            SimpleTypedNameDeclaration type = convertToSimpleType(types);
            result = type;
        }
        ASTExtendsList extendsList = classDeclaration.getFirstChildOfType(ASTExtendsList.class);
        if (extendsList != null) {
            List<ASTClassOrInterfaceType> types = extendsList.findChildrenOfType(ASTClassOrInterfaceType.class);
            SimpleTypedNameDeclaration type = convertToSimpleType(types);
            if (result == null) {
                result = type;
            } else {
                result.addNext(type);
            }
        }
    }
    return result;
}
Also used : ASTImplementsList(net.sourceforge.pmd.lang.java.ast.ASTImplementsList) ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType) ASTExtendsList(net.sourceforge.pmd.lang.java.ast.ASTExtendsList)

Example 2 with ASTImplementsList

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

the class SignatureDeclareThrowsExceptionRule method visit.

@Override
public Object visit(ASTClassOrInterfaceDeclaration node, Object data) {
    if (junitImported) {
        return super.visit(node, data);
    }
    ASTImplementsList impl = node.getFirstChildOfType(ASTImplementsList.class);
    if (impl != null && impl.jjtGetParent().equals(node)) {
        for (int ix = 0; ix < impl.jjtGetNumChildren(); ix++) {
            Node child = impl.jjtGetChild(ix);
            if (child.getClass() != ASTClassOrInterfaceType.class) {
                continue;
            }
            ASTClassOrInterfaceType type = (ASTClassOrInterfaceType) child;
            if (isJUnitTest(type)) {
                junitImported = true;
                return super.visit(node, data);
            }
        }
    }
    if (node.jjtGetNumChildren() != 0 && node.jjtGetChild(0) instanceof ASTExtendsList) {
        ASTClassOrInterfaceType type = (ASTClassOrInterfaceType) node.jjtGetChild(0).jjtGetChild(0);
        if (isJUnitTest(type)) {
            junitImported = true;
            return super.visit(node, data);
        }
    }
    return super.visit(node, data);
}
Also used : ASTImplementsList(net.sourceforge.pmd.lang.java.ast.ASTImplementsList) Node(net.sourceforge.pmd.lang.ast.Node) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType) ASTExtendsList(net.sourceforge.pmd.lang.java.ast.ASTExtendsList)

Example 3 with ASTImplementsList

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

the class CloneMethodMustImplementCloneableRule method extendsOrImplementsCloneable.

private boolean extendsOrImplementsCloneable(final ASTClassOrInterfaceDeclaration node) {
    if (node.getType() != null) {
        return Cloneable.class.isAssignableFrom(node.getType());
    }
    // From this point on, this is a best effort, the auxclasspath is incomplete.
    // TODO : Should we really care about this?
    // Shouldn't the type resolver / symbol table report missing classes and the user
    // know results are dependent on running under proper arguments?
    final ASTImplementsList impl = node.getFirstChildOfType(ASTImplementsList.class);
    if (impl != null) {
        for (int ix = 0; ix < impl.jjtGetNumChildren(); ix++) {
            final Node child = impl.jjtGetChild(ix);
            if (child.getClass() != ASTClassOrInterfaceType.class) {
                continue;
            }
            final ASTClassOrInterfaceType type = (ASTClassOrInterfaceType) child;
            if (type.getType() == null) {
                if ("Cloneable".equals(type.getImage())) {
                    return true;
                }
            } else if (Cloneable.class.isAssignableFrom(type.getType())) {
                return true;
            }
        }
    }
    if (node.jjtGetNumChildren() != 0 && node.jjtGetChild(0) instanceof ASTExtendsList) {
        final ASTClassOrInterfaceType type = (ASTClassOrInterfaceType) node.jjtGetChild(0).jjtGetChild(0);
        final Class<?> clazz = type.getType();
        if (clazz != null) {
            return Cloneable.class.isAssignableFrom(clazz);
        }
    }
    return false;
}
Also used : ASTImplementsList(net.sourceforge.pmd.lang.java.ast.ASTImplementsList) Node(net.sourceforge.pmd.lang.ast.Node) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType) ASTExtendsList(net.sourceforge.pmd.lang.java.ast.ASTExtendsList)

Example 4 with ASTImplementsList

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

the class CloneMethodMustImplementCloneableRule method visit.

@Override
public Object visit(final ASTMethodDeclaration node, final Object data) {
    // Is this a clone method?
    final ASTMethodDeclarator methodDeclarator = node.getFirstChildOfType(ASTMethodDeclarator.class);
    if (!isCloneMethod(methodDeclarator)) {
        return data;
    }
    // Is the clone method just throwing CloneNotSupportedException?
    final ASTClassOrInterfaceDeclaration classOrInterface = node.getFirstParentOfType(ASTClassOrInterfaceDeclaration.class);
    if (// Don't analyze enums, which cannot subclass clone()
    classOrInterface != null && (node.isFinal() || classOrInterface.isFinal())) {
        if (node.findDescendantsOfType(ASTBlock.class).size() == 1) {
            final List<ASTBlockStatement> blocks = node.findDescendantsOfType(ASTBlockStatement.class);
            if (blocks.size() == 1) {
                final ASTBlockStatement block = blocks.get(0);
                final ASTClassOrInterfaceType type = block.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
                if (type != null && type.getType() != null && type.getNthParent(9).equals(node) && type.getType().equals(CloneNotSupportedException.class)) {
                    return data;
                } else if (type != null && type.getType() == null && "CloneNotSupportedException".equals(type.getImage())) {
                    return data;
                }
            }
        }
    }
    // TODO : Should we really care about this? It can only happen with an incomplete auxclasspath
    if (classOrInterface != null && classOrInterface.getType() == null) {
        // Now check other whether implemented or extended classes are defined inside the same file
        final Set<String> classesNames = determineTopLevelCloneableClasses(classOrInterface);
        final ASTImplementsList implementsList = classOrInterface.getFirstChildOfType(ASTImplementsList.class);
        if (implementsList != null) {
            final List<ASTClassOrInterfaceType> types = implementsList.findChildrenOfType(ASTClassOrInterfaceType.class);
            for (final ASTClassOrInterfaceType t : types) {
                if (classesNames.contains(t.getImage())) {
                    return data;
                }
            }
        }
        final ASTExtendsList extendsList = classOrInterface.getFirstChildOfType(ASTExtendsList.class);
        if (extendsList != null) {
            final ASTClassOrInterfaceType type = extendsList.getFirstChildOfType(ASTClassOrInterfaceType.class);
            if (classesNames.contains(type.getImage())) {
                return data;
            }
        }
    }
    // Nothing can save us now
    addViolation(data, node);
    return data;
}
Also used : ASTMethodDeclarator(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator) ASTImplementsList(net.sourceforge.pmd.lang.java.ast.ASTImplementsList) ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) ASTBlockStatement(net.sourceforge.pmd.lang.java.ast.ASTBlockStatement) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType) ASTExtendsList(net.sourceforge.pmd.lang.java.ast.ASTExtendsList)

Aggregations

ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)4 ASTExtendsList (net.sourceforge.pmd.lang.java.ast.ASTExtendsList)4 ASTImplementsList (net.sourceforge.pmd.lang.java.ast.ASTImplementsList)4 Node (net.sourceforge.pmd.lang.ast.Node)2 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)2 ASTBlockStatement (net.sourceforge.pmd.lang.java.ast.ASTBlockStatement)1 ASTMethodDeclarator (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclarator)1