Search in sources :

Example 1 with ASTEnumConstant

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

Aggregations

ArrayList (java.util.ArrayList)1 ASTClassOrInterfaceBodyDeclaration (net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceBodyDeclaration)1 ASTEnumConstant (net.sourceforge.pmd.lang.java.ast.ASTEnumConstant)1 ASTName (net.sourceforge.pmd.lang.java.ast.ASTName)1 ASTPrimaryPrefix (net.sourceforge.pmd.lang.java.ast.ASTPrimaryPrefix)1 ASTPrimarySuffix (net.sourceforge.pmd.lang.java.ast.ASTPrimarySuffix)1 AbstractJavaNode (net.sourceforge.pmd.lang.java.ast.AbstractJavaNode)1