Search in sources :

Example 1 with ASTInitializer

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

the class UnusedPrivateMethodRule method calledFromOutsideItself.

private boolean calledFromOutsideItself(List<NameOccurrence> occs, NameDeclaration mnd) {
    int callsFromOutsideMethod = 0;
    for (NameOccurrence occ : occs) {
        Node occNode = occ.getLocation();
        ASTConstructorDeclaration enclosingConstructor = occNode.getFirstParentOfType(ASTConstructorDeclaration.class);
        if (enclosingConstructor != null) {
            callsFromOutsideMethod++;
            // Do we miss unused private constructors here?
            break;
        }
        ASTInitializer enclosingInitializer = occNode.getFirstParentOfType(ASTInitializer.class);
        if (enclosingInitializer != null) {
            callsFromOutsideMethod++;
            break;
        }
        ASTMethodDeclaration enclosingMethod = occNode.getFirstParentOfType(ASTMethodDeclaration.class);
        if (enclosingMethod == null || !mnd.getNode().jjtGetParent().equals(enclosingMethod)) {
            callsFromOutsideMethod++;
        }
    }
    return callsFromOutsideMethod == 0;
}
Also used : ASTConstructorDeclaration(net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) Node(net.sourceforge.pmd.lang.ast.Node) AccessNode(net.sourceforge.pmd.lang.java.ast.AccessNode) ASTInitializer(net.sourceforge.pmd.lang.java.ast.ASTInitializer) NameOccurrence(net.sourceforge.pmd.lang.symboltable.NameOccurrence)

Example 2 with ASTInitializer

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

the class AcceptanceTest method testInitializer.

@Test
public void testInitializer() {
    parseCode(TEST_INITIALIZERS);
    ASTInitializer a = acu.findDescendantsOfType(ASTInitializer.class).get(0);
    assertFalse(a.isStatic());
    a = acu.findDescendantsOfType(ASTInitializer.class).get(1);
    assertTrue(a.isStatic());
}
Also used : ASTInitializer(net.sourceforge.pmd.lang.java.ast.ASTInitializer) Test(org.junit.Test)

Aggregations

ASTInitializer (net.sourceforge.pmd.lang.java.ast.ASTInitializer)2 Node (net.sourceforge.pmd.lang.ast.Node)1 ASTConstructorDeclaration (net.sourceforge.pmd.lang.java.ast.ASTConstructorDeclaration)1 ASTMethodDeclaration (net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration)1 AccessNode (net.sourceforge.pmd.lang.java.ast.AccessNode)1 NameOccurrence (net.sourceforge.pmd.lang.symboltable.NameOccurrence)1 Test (org.junit.Test)1