Search in sources :

Example 21 with ASTClassOrInterfaceType

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

the class UseUtilityClassRule method visit.

@Override
public Object visit(ASTClassOrInterfaceBody decl, Object data) {
    if (decl.jjtGetParent() instanceof ASTClassOrInterfaceDeclaration) {
        ASTClassOrInterfaceDeclaration parent = (ASTClassOrInterfaceDeclaration) decl.jjtGetParent();
        if (parent.isAbstract() || parent.isInterface() || isExceptionType(parent)) {
            return super.visit(decl, data);
        }
        if (isOkUsingLombok(parent)) {
            return super.visit(decl, data);
        }
        int i = decl.jjtGetNumChildren();
        int methodCount = 0;
        boolean isOK = false;
        while (i > 0) {
            Node p = decl.jjtGetChild(--i);
            if (p.jjtGetNumChildren() == 0) {
                continue;
            }
            Node n = skipAnnotations(p);
            if (n instanceof ASTFieldDeclaration) {
                if (!((ASTFieldDeclaration) n).isStatic()) {
                    isOK = true;
                    break;
                }
            } else if (n instanceof ASTConstructorDeclaration) {
                if (((ASTConstructorDeclaration) n).isPrivate()) {
                    isOK = true;
                    break;
                }
            } else if (n instanceof ASTMethodDeclaration) {
                ASTMethodDeclaration m = (ASTMethodDeclaration) n;
                if (!m.isPrivate()) {
                    methodCount++;
                }
                if (!m.isStatic()) {
                    isOK = true;
                    break;
                }
                // TODO use symbol table
                if (m.getMethodName().equals("suite")) {
                    ASTResultType res = m.getResultType();
                    ASTClassOrInterfaceType c = res.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
                    if (c != null && c.hasImageEqualTo("Test")) {
                        isOK = true;
                        break;
                    }
                }
            }
        }
        if (!isOK && methodCount > 0) {
            addViolation(data, decl);
        }
    }
    return super.visit(decl, data);
}
Also used : ASTConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration) ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) Node(net.sourceforge.pmd.lang.ast.Node) ASTFieldDeclaration(net.sourceforge.pmd.lang.java.ast.ASTFieldDeclaration) ASTResultType(net.sourceforge.pmd.lang.java.ast.ASTResultType) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)

Example 22 with ASTClassOrInterfaceType

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

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

Example 24 with ASTClassOrInterfaceType

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

the class PreserveStackTraceRule method visit.

@Override
public Object visit(ASTCatchStatement catchStmt, Object data) {
    String target = catchStmt.jjtGetChild(0).findChildrenOfType(ASTVariableDeclaratorId.class).get(0).getImage();
    // Inspect all the throw stmt inside the catch stmt
    List<ASTThrowStatement> lstThrowStatements = catchStmt.findDescendantsOfType(ASTThrowStatement.class);
    for (ASTThrowStatement throwStatement : lstThrowStatements) {
        Node n = throwStatement.jjtGetChild(0).jjtGetChild(0);
        if (n instanceof ASTCastExpression) {
            ASTPrimaryExpression expr = (ASTPrimaryExpression) n.jjtGetChild(1);
            if (expr.jjtGetNumChildren() > 1 && expr.jjtGetChild(1) instanceof ASTPrimaryPrefix) {
                RuleContext ctx = (RuleContext) data;
                addViolation(ctx, throwStatement);
            }
            continue;
        }
        // Retrieve all argument for the throw exception (to see if the
        // original exception is preserved)
        ASTArgumentList args = throwStatement.getFirstDescendantOfType(ASTArgumentList.class);
        if (args != null) {
            Node parent = args.jjtGetParent().jjtGetParent();
            if (parent instanceof ASTAllocationExpression) {
                // maybe it is used inside a anonymous class
                ck(data, target, throwStatement, parent);
            } else {
                // Check all arguments used in the throw statement
                ck(data, target, throwStatement, throwStatement);
            }
        } else {
            Node child = throwStatement.jjtGetChild(0);
            while (child != null && child.jjtGetNumChildren() > 0 && !(child instanceof ASTName)) {
                child = child.jjtGetChild(0);
            }
            if (child != null) {
                if (child instanceof ASTName && !target.equals(child.getImage()) && !child.hasImageEqualTo(target + FILL_IN_STACKTRACE)) {
                    Map<VariableNameDeclaration, List<NameOccurrence>> vars = ((ASTName) child).getScope().getDeclarations(VariableNameDeclaration.class);
                    for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : vars.entrySet()) {
                        VariableNameDeclaration decl = entry.getKey();
                        List<NameOccurrence> occurrences = entry.getValue();
                        if (decl.getImage().equals(child.getImage())) {
                            if (!isInitCauseCalled(target, occurrences)) {
                                // Check how the variable is initialized
                                ASTVariableInitializer initializer = decl.getNode().jjtGetParent().getFirstDescendantOfType(ASTVariableInitializer.class);
                                if (initializer != null) {
                                    args = initializer.getFirstDescendantOfType(ASTArgumentList.class);
                                    if (args != null) {
                                        // constructor with args?
                                        ck(data, target, throwStatement, args);
                                    } else if (!isFillInStackTraceCalled(target, initializer)) {
                                        addViolation(data, throwStatement);
                                    }
                                }
                            }
                        }
                    }
                } else if (child instanceof ASTClassOrInterfaceType) {
                    addViolation(data, throwStatement);
                }
            }
        }
    }
    return super.visit(catchStmt, data);
}
Also used : ASTPrimaryPrefix(net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix) ASTCastExpression(net.sourceforge.pmd.lang.java.ast.ASTCastExpression) RuleContext(net.sourceforge.pmd.RuleContext) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) ASTVariableInitializer(net.sourceforge.pmd.lang.java.ast.ASTVariableInitializer) Node(net.sourceforge.pmd.lang.ast.Node) ASTPrimaryExpression(net.sourceforge.pmd.lang.java.ast.ASTPrimaryExpression) ASTAllocationExpression(net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) ASTThrowStatement(net.sourceforge.pmd.lang.java.ast.ASTThrowStatement) ASTName(net.sourceforge.pmd.lang.java.ast.ASTName) ASTArgumentList(net.sourceforge.pmd.lang.java.ast.ASTArgumentList) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 25 with ASTClassOrInterfaceType

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

the class AvoidCatchingThrowableRule method visit.

@Override
public Object visit(ASTCatchStatement node, Object data) {
    ASTType type = node.getFirstDescendantOfType(ASTType.class);
    ASTClassOrInterfaceType name = type.getFirstDescendantOfType(ASTClassOrInterfaceType.class);
    if (name.hasImageEqualTo("Throwable")) {
        addViolation(data, name);
    }
    return super.visit(node, data);
}
Also used : ASTType(net.sourceforge.pmd.lang.java.ast.ASTType) ASTClassOrInterfaceType(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)

Aggregations

ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)25 Node (net.sourceforge.pmd.lang.ast.Node)13 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)7 ASTType (net.sourceforge.pmd.lang.java.ast.ASTType)7 ASTExtendsList (net.sourceforge.pmd.lang.java.ast.ASTExtendsList)5 ASTImplementsList (net.sourceforge.pmd.lang.java.ast.ASTImplementsList)5 ASTVariableDeclaratorId (net.sourceforge.pmd.lang.java.ast.ASTVariableDeclaratorId)5 ArrayList (java.util.ArrayList)4 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)4 ASTReferenceType (net.sourceforge.pmd.lang.java.ast.ASTReferenceType)4 List (java.util.List)3 ASTAllocationExpression (net.sourceforge.pmd.lang.java.ast.ASTAllocationExpression)3 ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)3 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)3 ASTFormalParameter (net.sourceforge.pmd.lang.java.ast.ASTFormalParameter)3 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)3 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)3 Map (java.util.Map)2 RuleContext (net.sourceforge.pmd.RuleContext)2 ASTArgumentList (net.sourceforge.pmd.lang.java.ast.ASTArgumentList)2