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

test for thisAccess like this.field and not SomeClass.this.field #991

Closed
wants to merge 3 commits into from
Closed
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
34 changes: 34 additions & 0 deletions src/test/java/spoon/test/fieldaccesses/FieldAccessTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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());
}
}
18 changes: 18 additions & 0 deletions src/test/java/spoon/test/fieldaccesses/testclasses/Pizza.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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<String> keyValues = new ArrayList<>();

public Fails() {
keyValues.add("");
}
}
}