Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(field): Qualified name of a type declared in an anonymous class. #541

Merged
merged 1 commit into from
Feb 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1908,7 +1908,7 @@ public <T> void visitCtTypeReference(CtTypeReference<T> ref) {
write(ref.getSimpleName());
} else {
if (ref.getDeclaringType() != null) {
if (!context.ignoreEnclosingClass && !ref.isLocalType()) {
if (!context.ignoreEnclosingClass && !ref.isLocalType() && !ref.getDeclaringType().isAnonymous()) {
scan(ref.getDeclaringType());
write(".");
}
Expand Down
18 changes: 16 additions & 2 deletions src/test/java/spoon/test/fieldaccesses/FieldAccessTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
import org.junit.Test;
import spoon.Launcher;
import spoon.reflect.code.CtArrayWrite;

import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtFieldAccess;
import spoon.reflect.code.CtFieldRead;
import spoon.reflect.code.CtFieldWrite;
import spoon.reflect.code.CtLambda;
import spoon.reflect.code.CtLocalVariable;
import spoon.reflect.code.CtOperatorAssignment;
import spoon.reflect.code.CtUnaryOperator;
import spoon.reflect.code.CtVariableWrite;
import spoon.reflect.code.UnaryOperatorKind;
import spoon.reflect.code.CtOperatorAssignment;
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtType;
Expand All @@ -23,6 +22,8 @@
import spoon.reflect.visitor.filter.NameFilter;
import spoon.reflect.visitor.filter.TypeFilter;
import spoon.test.fieldaccesses.testclasses.Panini;
import spoon.test.fieldaccesses.testclasses.Pozole;
import spoon.testing.Assert;

import java.util.List;
import java.util.logging.Logger;
Expand All @@ -31,6 +32,7 @@
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static spoon.testing.Assert.*;
import static spoon.testing.utils.ModelUtils.build;
import static spoon.testing.utils.ModelUtils.buildClass;

Expand Down Expand Up @@ -264,4 +266,16 @@ public void testFieldWriteWithPlusEqualsOperation() throws Exception {
assertEquals("array[0] += 0", arrays.get(0).getParent().toString());
assertEquals("array[0]", arrays.get(0).toString());
}

@Test
public void testTypeDeclaredInAnonymousClass() throws Exception {
// contract: Type declared in an anonymous class shouldn't include the anonymous qualified name
// in its own fully qualified name.
final CtType<Pozole> aPozole = buildClass(Pozole.class);
final List<CtField> elements = aPozole.getElements(new TypeFilter<>(CtField.class));

assertEquals(1, elements.size());
assertTrue(elements.get(0).getType().getDeclaringType().isAnonymous());
assertThat(elements.get(0)).isEqualTo("private final Test test = new Test();");
}
}
24 changes: 24 additions & 0 deletions src/test/java/spoon/test/fieldaccesses/testclasses/Pozole.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package spoon.test.fieldaccesses.testclasses;

public class Pozole {
interface Interface1 {
}
class Cook {
public Interface1 m() {
return null;
}
}
public Cook cook() {
return new Cook() {
@Override
public Interface1 m() {
return new Interface1() {
private final Test test = new Test();
};
}

class Test implements Interface1 {
}
};
}
}