Search in sources :

Example 16 with ASTClassOrInterfaceDeclaration

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

the class CloneMethodMustImplementCloneableRule method determineTopLevelCloneableClasses.

/**
 * Determines all the class/interface declarations inside this compilation
 * unit, which implement Cloneable
 *
 * @param currentClass
 *            the node of the class, that is currently analyzed (inside this
 *            compilation unit)
 * @return a Set of class/interface names
 */
private Set<String> determineTopLevelCloneableClasses(final ASTClassOrInterfaceDeclaration currentClass) {
    final List<ASTClassOrInterfaceDeclaration> classes = currentClass.getFirstParentOfType(ASTCompilationUnit.class).findDescendantsOfType(ASTClassOrInterfaceDeclaration.class);
    final Set<String> classesNames = new HashSet<String>();
    for (final ASTClassOrInterfaceDeclaration c : classes) {
        if (!Objects.equals(c, currentClass) && extendsOrImplementsCloneable(c)) {
            classesNames.add(c.getImage());
        }
    }
    return classesNames;
}
Also used : ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) HashSet(java.util.HashSet)

Example 17 with ASTClassOrInterfaceDeclaration

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

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

the class UnusedFormalParameterRule method check.

private void check(Node node, Object data) {
    Node parent = node.jjtGetParent().jjtGetParent().jjtGetParent();
    if (parent instanceof ASTClassOrInterfaceDeclaration && !((ASTClassOrInterfaceDeclaration) parent).isInterface()) {
        Map<VariableNameDeclaration, List<NameOccurrence>> vars = ((JavaNode) node).getScope().getDeclarations(VariableNameDeclaration.class);
        for (Map.Entry<VariableNameDeclaration, List<NameOccurrence>> entry : vars.entrySet()) {
            VariableNameDeclaration nameDecl = entry.getKey();
            if (actuallyUsed(nameDecl, entry.getValue())) {
                continue;
            }
            addViolation(data, nameDecl.getNode(), new Object[] { node instanceof ASTMethodDeclaration ? "method" : "constructor", nameDecl.getImage() });
        }
    }
}
Also used : ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) VariableNameDeclaration(net.sourceforge.pmd.lang.java.symboltable.VariableNameDeclaration) Node(net.sourceforge.pmd.lang.ast.Node) JavaNode(net.sourceforge.pmd.lang.java.ast.JavaNode) ASTNameList(net.sourceforge.pmd.lang.java.ast.ASTNameList) List(java.util.List) Map(java.util.Map)

Example 19 with ASTClassOrInterfaceDeclaration

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

the class UnnecessaryModifierRule method visit.

public Object visit(ASTAnnotationTypeDeclaration node, Object data) {
    if (node.isAbstract()) {
        // an abstract annotation
        addViolation(data, node, getMessage());
    }
    if (!node.isNested()) {
        return data;
    }
    Node parent = node.jjtGetParent().jjtGetParent().jjtGetParent();
    boolean isParentInterfaceOrAnnotation = parent instanceof ASTAnnotationTypeDeclaration || parent instanceof ASTClassOrInterfaceDeclaration && ((ASTClassOrInterfaceDeclaration) parent).isInterface();
    // a public annotation within an interface or annotation
    if (node.isPublic() && isParentInterfaceOrAnnotation) {
        addViolation(data, node, getMessage());
    }
    if (node.isStatic()) {
        // a static annotation
        addViolation(data, node, getMessage());
    }
    return data;
}
Also used : ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) Node(net.sourceforge.pmd.lang.ast.Node) ASTAnnotationTypeDeclaration(net.sourceforge.pmd.lang.java.ast.ASTAnnotationTypeDeclaration)

Example 20 with ASTClassOrInterfaceDeclaration

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

the class VariableNamingConventionsRule method visit.

public Object visit(ASTFieldDeclaration node, Object data) {
    if (!checkMembers) {
        return data;
    }
    boolean isStatic = node.isStatic();
    boolean isFinal = node.isFinal();
    Node type = node.jjtGetParent().jjtGetParent().jjtGetParent();
    // Anything inside an annotation type is also static and final
    if (type instanceof ASTClassOrInterfaceDeclaration && ((ASTClassOrInterfaceDeclaration) type).isInterface() || type instanceof ASTAnnotationTypeDeclaration) {
        isStatic = true;
        isFinal = true;
    }
    return checkVariableDeclarators(node.isStatic() ? staticPrefixes : memberPrefixes, isStatic ? staticSuffixes : memberSuffixes, node, isStatic, isFinal, data);
}
Also used : ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) Node(net.sourceforge.pmd.lang.ast.Node) ASTAnnotationTypeDeclaration(net.sourceforge.pmd.lang.java.ast.ASTAnnotationTypeDeclaration)

Aggregations

ASTClassOrInterfaceDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration)31 Test (org.junit.Test)18 EnumTest (net.sourceforge.pmd.lang.java.symboltable.testdata.InnerClass.TheInnerClass.EnumTest)15 List (java.util.List)14 NameDeclaration (net.sourceforge.pmd.lang.symboltable.NameDeclaration)12 Node (net.sourceforge.pmd.lang.ast.Node)7 Map (java.util.Map)4 ASTAnnotationTypeDeclaration (net.sourceforge.pmd.lang.java.ast.ASTAnnotationTypeDeclaration)3 ASTClassOrInterfaceType (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceType)3 ASTCompilationUnit (net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit)3 ASTExtendsList (net.sourceforge.pmd.lang.java.ast.ASTExtendsList)3 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)3 AbstractJavaTypeNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaTypeNode)3 JavaNode (net.sourceforge.pmd.lang.java.ast.JavaNode)3 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)3 ArrayList (java.util.ArrayList)2 ASTArguments (net.sourceforge.pmd.lang.java.ast.ASTArguments)2 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)2 ASTImplementsList (net.sourceforge.pmd.lang.java.ast.ASTImplementsList)2 ASTTypeDeclaration (net.sourceforge.pmd.lang.java.ast.ASTTypeDeclaration)2