Search in sources :

Example 1 with ASTClassOrInterfaceDeclaration

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

the class ClassTypeResolverTest method testInnerClass.

@Test
public void testInnerClass() throws ClassNotFoundException {
    ASTCompilationUnit acu = parseAndTypeResolveForClass15(InnerClass.class);
    Class<?> theInnerClass = Class.forName("net.sourceforge.pmd.typeresolution.testdata.InnerClass$TheInnerClass");
    // Outer class
    ASTTypeDeclaration typeDeclaration = acu.getFirstDescendantOfType(ASTTypeDeclaration.class);
    assertEquals(InnerClass.class, typeDeclaration.getType());
    ASTClassOrInterfaceDeclaration outerClassDeclaration = typeDeclaration.getFirstDescendantOfType(ASTClassOrInterfaceDeclaration.class);
    assertEquals(InnerClass.class, outerClassDeclaration.getType());
    // Inner class
    assertEquals(theInnerClass, outerClassDeclaration.getFirstDescendantOfType(ASTClassOrInterfaceDeclaration.class).getType());
    // Method parameter as inner class
    ASTFormalParameter formalParameter = typeDeclaration.getFirstDescendantOfType(ASTFormalParameter.class);
    assertEquals(theInnerClass, formalParameter.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) ASTFormalParameter(net.sourceforge.pmd.lang.java.ast.ASTFormalParameter) Test(org.junit.Test)

Example 2 with ASTClassOrInterfaceDeclaration

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

the class ClassScopeTest method testOneParamVararg.

@Test
public final void testOneParamVararg() {
    parseCode15(ONE_PARAM_VARARG);
    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 3 with ASTClassOrInterfaceDeclaration

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

the class ClassScopeTest method testClassName.

@Test
public void testClassName() {
    parseCode(CLASS_NAME);
    ASTClassOrInterfaceDeclaration n = acu.findDescendantsOfType(ASTClassOrInterfaceDeclaration.class).get(0);
    assertEquals("Foo", n.getScope().getEnclosingScope(ClassScope.class).getClassName());
}
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 4 with ASTClassOrInterfaceDeclaration

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

the class ClassScopeTest method testTwoMethodsSameNameDiffArgs.

@Test
public void testTwoMethodsSameNameDiffArgs() {
    // TODO this won't work with String and java.lang.String
    parseCode(METHODS_WITH_DIFF_ARG);
    ASTClassOrInterfaceDeclaration n = acu.findDescendantsOfType(ASTClassOrInterfaceDeclaration.class).get(0);
    Map<NameDeclaration, List<NameOccurrence>> m = ((ClassScope) n.getScope()).getDeclarations();
    assertEquals(2, m.size());
    Iterator<NameDeclaration> i = m.keySet().iterator();
    MethodNameDeclaration mnd = (MethodNameDeclaration) i.next();
    assertEquals("bar", mnd.getImage());
    assertEquals("bar", ((MethodNameDeclaration) i.next()).getImage());
}
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 5 with ASTClassOrInterfaceDeclaration

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

the class ClassScopeTest method testTwoParams.

@Test
public final void testTwoParams() {
    parseCode(TWO_PARAMS);
    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,int)", 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)

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