diff --git a/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java b/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java index 85f10862db7..477086c4835 100644 --- a/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java +++ b/src/main/java/spoon/reflect/visitor/DefaultJavaPrettyPrinter.java @@ -718,9 +718,13 @@ public void visitCtCase(CtCase 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()); } diff --git a/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java b/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java index ef66bc8a872..c3a1037bf69 100644 --- a/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java +++ b/src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java @@ -2649,7 +2649,8 @@ public boolean visit(QualifiedNameReference qualifiedNameReference, BlockScope s } else { // case with no complete classpath CtTypeReference 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); } diff --git a/src/test/java/spoon/test/fieldaccesses/FieldAccessTest.java b/src/test/java/spoon/test/fieldaccesses/FieldAccessTest.java index 0075b911ff8..c4f411b6807 100644 --- a/src/test/java/spoon/test/fieldaccesses/FieldAccessTest.java +++ b/src/test/java/spoon/test/fieldaccesses/FieldAccessTest.java @@ -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; @@ -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; @@ -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()); + + 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()); + } } diff --git a/src/test/resources/import-resources/fr/inria/FooNoClassPath.java b/src/test/resources/import-resources/fr/inria/FooNoClassPath.java new file mode 100644 index 00000000000..ac2915b81e1 --- /dev/null +++ b/src/test/resources/import-resources/fr/inria/FooNoClassPath.java @@ -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); + } +}