Skip to content

Commit

Permalink
Possible fix for INRIA#3329
Browse files Browse the repository at this point in the history
  • Loading branch information
Egor18 committed Apr 16, 2020
1 parent 7966595 commit 5176fb8
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,11 @@ CtExpression<?> createTargetFieldAccess(QualifiedNameReference qualifiedNameRefe
} else if (ref.isStatic()) {
target = createTypeAccess(qualifiedNameReference, ref);
} else if (!ref.isStatic() && !ref.getDeclaringType().isAnonymous()) {
target = jdtTreeBuilder.getFactory().Code().createThisAccess(jdtTreeBuilder.getReferencesBuilder().<Object>getTypeReference(qualifiedNameReference.actualReceiverType), true);
if (ref.getDeclaringType().getDeclaredField(ref.getSimpleName()) == null) {
target = createTypeAccessNoClasspath(qualifiedNameReference);
} else {
target = jdtTreeBuilder.getFactory().Code().createThisAccess(jdtTreeBuilder.getReferencesBuilder().<Object>getTypeReference(qualifiedNameReference.actualReceiverType), true);
}
}
return target;
}
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/spoon/test/targeted/TargetedExpressionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.junit.Test;

import spoon.Launcher;
import spoon.reflect.CtModel;
import spoon.reflect.code.CtExpression;
import spoon.reflect.code.CtFieldAccess;
import spoon.reflect.code.CtFieldRead;
Expand Down Expand Up @@ -282,6 +283,24 @@ public void testStaticTargetsOfFieldAccessNoClasspath() {
assertEqualsFieldAccess(new ExpectedTargetedExpression().declaringType(expectedFiiFuu).target(launcher.getFactory().Code().createTypeAccess(expectedFiiFuu)).result("Fii.Fuu.i"), elements.get(9));
}

@Test
public void testOnlyStaticTargetFieldReadNoClasspath() {
// bug case kindly provided by @slarse
// in https://github.com/INRIA/spoon/issues/3329
final Launcher launcher = new Launcher();
launcher.getEnvironment().setNoClasspath(true);
launcher.addInputResource("./src/test/resources/spoon/test/noclasspath/targeted/StaticFieldReadOnly.java");
CtModel model = launcher.buildModel();

List<CtInvocation<?>> invocations = model.getElements(e -> e.getExecutable().getSimpleName().equals("error"));
CtInvocation<?> inv = invocations.get(0);
CtFieldRead<?> fieldRead = (CtFieldRead<?>) inv.getTarget();
CtExpression<?> target = fieldRead.getTarget();

assertTrue(target instanceof CtTypeAccess);
assertEquals("Launcher", ((CtTypeAccess<?>) target).getAccessedType().getSimpleName());
}

@Test
public void testTargetsOfInv() throws Exception {
// contract: Specify declaring type of the executable of an invocation, the target of the invocation and its result.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import spoon.Launcher;

public class StaticFieldReadOnly {
public static void main(String[] args) {
Launcher.logger.error("Hello :)");
}
}

0 comments on commit 5176fb8

Please sign in to comment.