Search in sources :

Example 21 with ASTClassOrInterfaceDeclaration

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

the class ClassTypeResolverTest method testAnonymousInnerClass.

@Test
public void testAnonymousInnerClass() throws ClassNotFoundException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(AnonymousInnerClass.class);
    Class<?> theAnonymousInnerClass = Class.forName("net.sourceforge.pmd.typeresolution.testdata.AnonymousInnerClass$1");
    // Outer class
    ASTTypeDeclaration typeDeclaration = acu.getFirstDescendantOfType(ASTTypeDeclaration.class);
    assertEquals(AnonymousInnerClass.class, typeDeclaration.getType());
    ASTClassOrInterfaceDeclaration outerClassDeclaration = typeDeclaration.getFirstDescendantOfType(ASTClassOrInterfaceDeclaration.class);
    assertEquals(AnonymousInnerClass.class, outerClassDeclaration.getType());
    // Anonymous Inner class
    assertEquals(theAnonymousInnerClass, outerClassDeclaration.getFirstDescendantOfType(ASTAllocationExpression.class).getType());
}
Also used : ASTCompilationUnit(net.sourceforge.pmd.lang.java.ast.ASTCompilationUnit) ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) ASTTypeDeclaration(net.sourceforge.pmd.lang.java.ast.ASTTypeDeclaration) Test(org.junit.Test)

Example 22 with ASTClassOrInterfaceDeclaration

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

the class ClassScopeTest method testMethodDeclarationRecorded.

@Test
public void testMethodDeclarationRecorded() {
    parseCode(METHOD_DECLARATIONS_RECORDED);
    ASTClassOrInterfaceDeclaration n = acu.findDescendantsOfType(ASTClassOrInterfaceDeclaration.class).get(0);
    ClassScope s = (ClassScope) n.getScope();
    Map<NameDeclaration, List<NameOccurrence>> m = s.getDeclarations();
    assertEquals(1, m.size());
    MethodNameDeclaration mnd = (MethodNameDeclaration) m.keySet().iterator().next();
    assertEquals("bar", mnd.getImage());
    ASTMethodDeclaration node = (ASTMethodDeclaration) mnd.getNode().jjtGetParent();
    assertTrue(node.isPrivate());
}
Also used : ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) ASTMethodDeclaration(net.sourceforge.pmd.lang.java.ast.ASTMethodDeclaration) List(java.util.List) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) Test(org.junit.Test) EnumTest(net.sourceforge.pmd.lang.java.symboltable.testdata.InnerClass.TheInnerClass.EnumTest)

Example 23 with ASTClassOrInterfaceDeclaration

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

the class ClassScopeTest method testNestedClassesOfImportResolution.

@Test
public void testNestedClassesOfImportResolution() {
    parseCode(NESTED_CLASSES_OF_IMPORT);
    final ASTClassOrInterfaceDeclaration n = acu.findDescendantsOfType(ASTClassOrInterfaceDeclaration.class).get(0);
    final ClassScope c = (ClassScope) n.getScope();
    assertEquals(EnumTest.class, c.resolveType("TheInnerClass.EnumTest"));
}
Also used : ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) Test(org.junit.Test) EnumTest(net.sourceforge.pmd.lang.java.symboltable.testdata.InnerClass.TheInnerClass.EnumTest)

Example 24 with ASTClassOrInterfaceDeclaration

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

the class ClassScopeTest method testOneParam.

@Test
public final void testOneParam() {
    parseCode(ONE_PARAM);
    ASTClassOrInterfaceDeclaration n = acu.findDescendantsOfType(ASTClassOrInterfaceDeclaration.class).get(0);
    Map<NameDeclaration, List<NameOccurrence>> m = ((ClassScope) n.getScope()).getDeclarations();
    MethodNameDeclaration mnd = (MethodNameDeclaration) m.keySet().iterator().next();
    assertEquals("(String)", mnd.getParameterDisplaySignature());
}
Also used : ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) List(java.util.List) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) Test(org.junit.Test) EnumTest(net.sourceforge.pmd.lang.java.symboltable.testdata.InnerClass.TheInnerClass.EnumTest)

Example 25 with ASTClassOrInterfaceDeclaration

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

the class ClassScopeTest method testMethodUsageSeen2.

@Test
public void testMethodUsageSeen2() {
    parseCode(METHOD_USAGE_SEEN2);
    ASTClassOrInterfaceDeclaration n = acu.findDescendantsOfType(ASTClassOrInterfaceDeclaration.class).get(0);
    Map<NameDeclaration, List<NameOccurrence>> m = ((ClassScope) n.getScope()).getDeclarations();
    assertEquals(2, m.size());
    for (Map.Entry<NameDeclaration, List<NameOccurrence>> entry : m.entrySet()) {
        assertEquals("baz", entry.getKey().getImage());
        if (entry.getKey().getNode().getBeginLine() == 2) {
            // this is the public method declaration - it is not used
            // anywhere
            assertEquals(0, entry.getValue().size());
        } else if (entry.getKey().getNode().getBeginLine() == 5) {
            // this is the private (overloaded) method
            assertEquals(1, entry.getValue().size());
            // it's used once in line 3
            assertEquals(3, entry.getValue().get(0).getLocation().getBeginLine());
        } else {
            fail("unexpected name declaration");
        }
    }
}
Also used : ASTClassOrInterfaceDeclaration(net.sourceforge.pmd.lang.java.ast.ASTClassOrInterfaceDeclaration) List(java.util.List) NameDeclaration(net.sourceforge.pmd.lang.symboltable.NameDeclaration) Map(java.util.Map) Test(org.junit.Test) EnumTest(net.sourceforge.pmd.lang.java.symboltable.testdata.InnerClass.TheInnerClass.EnumTest)

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