Skip to content

Commit

Permalink
Fix chained field access in noclasspath
Browse files Browse the repository at this point in the history
  • Loading branch information
tdurieux committed Nov 25, 2015
1 parent 196d90f commit 43a8cb9
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -718,9 +718,14 @@ 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.getDeclaringType() == null
|| 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
17 changes: 17 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 Down Expand Up @@ -166,4 +167,20 @@ 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());
}
}
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);
}
}

0 comments on commit 43a8cb9

Please sign in to comment.