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 chained field access in no classpath #424

Merged
merged 1 commit into from
Dec 2, 2015
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 @@ -718,9 +718,13 @@ public <E> void visitCtCase(CtCase<E> caseStatement) {
if (caseStatement.getCaseExpression() != null) {
write("case ");
// writing enum case expression
if ((caseStatement.getCaseExpression() instanceof CtFieldAccess) && ((CtFieldAccess) caseStatement.getCaseExpression()).getVariable().getType().getQualifiedName()
.equals(((CtFieldAccess) caseStatement.getCaseExpression()).getVariable().getDeclaringType().getQualifiedName())) {
write(((CtFieldAccess) caseStatement.getCaseExpression()).getVariable().getSimpleName());
if ((caseStatement.getCaseExpression() instanceof CtFieldAccess)) {
CtFieldReference variable = ((CtFieldAccess) caseStatement.getCaseExpression()).getVariable();
if (variable.getType().getQualifiedName().equals(variable.getDeclaringType().getQualifiedName())) {
write(variable.getSimpleName());
} else {
scan(caseStatement.getCaseExpression());
}
} else {
scan(caseStatement.getCaseExpression());
}
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2649,7 +2649,8 @@ public boolean visit(QualifiedNameReference qualifiedNameReference, BlockScope s
} else {
// case with no complete classpath
CtTypeReference<Object> ref2 = factory.Core().createTypeReference();
ref2.setSimpleName(new String(qualifiedNameReference.tokens[qualifiedNameReference.tokens.length - 1]));
ref2.setSimpleName(new String(qualifiedNameReference.tokens[i + 1]));
other.getVariable().setSimpleName(ref2.getSimpleName());
other.setType(ref2);
}

Expand Down
33 changes: 33 additions & 0 deletions src/test/java/spoon/test/fieldaccesses/FieldAccessTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package spoon.test.fieldaccesses;

import org.junit.Test;
import spoon.Launcher;
import spoon.reflect.code.CtFieldAccess;
import spoon.reflect.code.CtFieldRead;
import spoon.reflect.code.CtLambda;
Expand All @@ -10,6 +11,7 @@
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtType;
import spoon.reflect.factory.Factory;
import spoon.reflect.reference.CtFieldReference;
import spoon.reflect.visitor.Query;
import spoon.reflect.visitor.filter.NameFilter;
import spoon.reflect.visitor.filter.TypeFilter;
Expand Down Expand Up @@ -166,4 +168,35 @@ public void testFieldAccessInAnonymousClass() throws Exception {
assertEquals("next", fieldInAnonymous.getVariable().getSimpleName());
assertEquals("ingredient.next", fieldInAnonymous.toString());
}


@Test
public void testFieldAccessNoClasspath() throws Exception {
Launcher launcher = new Launcher();
launcher.addInputResource("src/test/resources/import-resources/fr/inria/");
launcher.getEnvironment().setNoClasspath(true);

launcher.run();

CtType<?> ctType = launcher.getFactory().Class().get("FooNoClassPath");

CtFieldAccess ctFieldAccess = ctType
.getElements(new TypeFilter<>(CtFieldAccess.class)).get(0);
assertEquals("(game.board.width)", ctFieldAccess.toString());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adds assertions on the AST.


CtFieldReference ctFieldReferenceWith = ctFieldAccess.getVariable();
assertEquals("width", ctFieldReferenceWith.getSimpleName());

CtFieldAccess ctFieldAccessBoard = (CtFieldAccess) ctFieldAccess.getTarget();
assertEquals("game.board", ctFieldAccessBoard.toString());

CtFieldReference ctFieldReferenceBoard = ctFieldAccessBoard.getVariable();
assertEquals("board", ctFieldReferenceBoard.getSimpleName());

CtFieldAccess ctFieldAccessGame = (CtFieldAccess) ctFieldAccessBoard.getTarget();
assertEquals("game.board", ctFieldAccessBoard.toString());

CtFieldReference ctFieldReferenceGame = ctFieldAccessGame.getVariable();
assertEquals("game", ctFieldReferenceGame.getSimpleName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
public class FooNoClassPath {

private Game game = new Game();


public void m() {
Coords coords = new Coords(game.board.width - 2, game.board.height - 2);
}
}