diff --git a/src/test/java/spoon/test/fieldaccesses/FieldAccessTest.java b/src/test/java/spoon/test/fieldaccesses/FieldAccessTest.java index 9dba64286f1..3c921ea9f73 100644 --- a/src/test/java/spoon/test/fieldaccesses/FieldAccessTest.java +++ b/src/test/java/spoon/test/fieldaccesses/FieldAccessTest.java @@ -3,6 +3,7 @@ import org.junit.Test; import spoon.Launcher; import spoon.reflect.code.CtArrayWrite; +import spoon.reflect.code.CtAssignment; import spoon.reflect.code.CtExpression; import spoon.reflect.code.CtFieldAccess; import spoon.reflect.code.CtFieldRead; @@ -22,6 +23,7 @@ import spoon.reflect.declaration.CtType; import spoon.reflect.factory.Factory; import spoon.reflect.reference.CtFieldReference; +import spoon.reflect.reference.CtParameterReference; import spoon.reflect.visitor.CtScanner; import spoon.reflect.visitor.DefaultJavaPrettyPrinter; import spoon.reflect.visitor.Query; @@ -30,6 +32,7 @@ import spoon.test.fieldaccesses.testclasses.B; import spoon.test.fieldaccesses.testclasses.Kuu; import spoon.test.fieldaccesses.testclasses.Panini; +import spoon.test.fieldaccesses.testclasses.Pizza; import spoon.test.fieldaccesses.testclasses.Pozole; import spoon.test.fieldaccesses.testclasses.Tacos; import spoon.testing.utils.ModelUtils; @@ -395,4 +398,35 @@ public void testGetReference() throws Exception { assertEquals("A.myField", aClass.getElements(new TypeFilter<>(CtFieldWrite.class)).get(0).toString()); assertEquals("finalField", aClass.getElements(new TypeFilter<>(CtFieldWrite.class)).get(1).toString()); } + + @Test + public void testThisDotFieldAccess() throws Exception { + Factory factory = build(Pizza.class); + CtClass pizza = factory.Class().get(Pizza.class); + + assertEquals("this.size = size", pizza.getElements(new TypeFilter<>(CtAssignment.class)).get(0).toString()); + } + + @Test + public void testThisDotFieldAccessWithAutoImport() throws Exception { + Factory factory = build(Pizza.class); + CtClass pizza = factory.Class().get(Pizza.class); + factory.getEnvironment().setAutoImports(true); + + assertEquals("this.size = size", pizza.getElements(new TypeFilter<>(CtAssignment.class)).get(0).toString()); + } + + @Test + public void testThisDotFieldAccessAutoNotImplicit() throws Exception { + Factory factory = build(Pizza.class); + CtClass pizza = factory.Class().get(Pizza.class); + factory.getEnvironment().setAutoImports(true); + + CtMethod m = pizza.getMethod("addSize", factory.Type().INTEGER_PRIMITIVE); + CtParameter param = m.getParameters().get(0); + param.setSimpleName("size"); + m.getElements((CtParameterReference e)-> "plus".equals(e.getSimpleName())).forEach((CtParameterReference e)->e.setSimpleName("size")); + //the printer detects the name conflict between field name and parameter name and should use explicit thisAccess automatically + assertEquals("this.size = (this.size) + size", m.getElements(new TypeFilter<>(CtAssignment.class)).get(0).toString()); + } } diff --git a/src/test/java/spoon/test/fieldaccesses/testclasses/Pizza.java b/src/test/java/spoon/test/fieldaccesses/testclasses/Pizza.java new file mode 100644 index 00000000000..44ccd319404 --- /dev/null +++ b/src/test/java/spoon/test/fieldaccesses/testclasses/Pizza.java @@ -0,0 +1,18 @@ +package spoon.test.fieldaccesses.testclasses; + +public class Pizza +{ + int size; + + void setSize(int size) { + this.size = size; + } + + int getSize(int size) { + return size; + } + + void addSize(int plus) { + size = size + plus; + } +} diff --git a/src/test/java/spoon/test/fieldaccesses/testclasses/internal/Foo.java b/src/test/java/spoon/test/fieldaccesses/testclasses/internal/Foo.java index 69a3f42fb79..ce400e4b299 100644 --- a/src/test/java/spoon/test/fieldaccesses/testclasses/internal/Foo.java +++ b/src/test/java/spoon/test/fieldaccesses/testclasses/internal/Foo.java @@ -1,9 +1,18 @@ package spoon.test.fieldaccesses.testclasses.internal; -public abstract class Foo extends Bar.Inner { +import java.util.ArrayList; +import java.util.List; + +public class Foo extends Bar.Inner { class Test { class Test2 { - } } + public static class Fails { + public final List keyValues = new ArrayList<>(); + + public Fails() { + keyValues.add(""); + } + } }