Skip to content

Commit

Permalink
ROASTER-43: MethodFinderVisitor should handle single-level methods only
Browse files Browse the repository at this point in the history
  • Loading branch information
gastaldi committed Dec 19, 2014
1 parent fa5f3a3 commit 37cff46
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,29 @@ public class MethodFinderVisitor extends ASTVisitor
{
private final List<MethodDeclaration> methods = new ArrayList<MethodDeclaration>();
private ASTNode parent;
private boolean outerTypeVisited;

@Override
public boolean visit(final TypeDeclaration node)
{
if (outerTypeVisited)
{
return false;
}
this.outerTypeVisited = true;
parent = node;
addMethods(node);
return super.visit(node);
}

@Override
public boolean visit(EnumDeclaration node)
{
parent = node;
addMethods(node);
return super.visit(node);
}

@Override
public boolean visit(AnnotationTypeDeclaration node)
{
Expand Down Expand Up @@ -83,7 +89,8 @@ private void addMethods(final List<BodyDeclaration> bodyDeclarations)
{
for (BodyDeclaration bodyDeclaration : bodyDeclarations)
{
if (bodyDeclaration instanceof MethodDeclaration) {
if (bodyDeclaration instanceof MethodDeclaration)
{
methods.add((MethodDeclaration) bodyDeclaration);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,38 @@ public void testRemoveNestedClassInClass()
Assert.assertEquals(1, javaClass.getNestedTypes().size());
}

@Test
public void testRenamedOuterClass() throws Exception
{
JavaClassSource source = Roaster
.parse(JavaClassSource.class,
"public class Outer {public class Inner { String innerField; public Inner() {} public Inner(String innerField){this.innerField=innerField;}} public Outer() {}}");

JavaClassSource nestedType = (JavaClassSource) source.getNestedType("Inner");
Assert.assertNotNull(nestedType);
Assert.assertEquals("Inner", nestedType.getName());
Assert.assertEquals(1, source.getMethods().size());
Assert.assertEquals(2, nestedType.getMethods().size());

Assert.assertTrue(source.getMethods().get(0).isConstructor());
Assert.assertEquals("Outer", source.getMethods().get(0).getName());

Assert.assertTrue(nestedType.getMethods().get(0).isConstructor());
Assert.assertTrue(nestedType.getMethods().get(1).isConstructor());
Assert.assertEquals("Inner", nestedType.getMethods().get(0).getName());
Assert.assertEquals("Inner", nestedType.getMethods().get(1).getName());

source.setName("RenamedOuter");
nestedType = (JavaClassSource) source.getNestedType("Inner");
Assert.assertTrue(source.getMethods().get(0).isConstructor());
Assert.assertEquals("RenamedOuter", source.getMethods().get(0).getName());

Assert.assertTrue(nestedType.getMethods().get(0).isConstructor());
Assert.assertTrue(nestedType.getMethods().get(1).isConstructor());
Assert.assertEquals("Inner", nestedType.getMethods().get(0).getName());
Assert.assertEquals("Inner", nestedType.getMethods().get(1).getName());
}

public class NestedClass
{
}
Expand Down

0 comments on commit 37cff46

Please sign in to comment.