Skip to content

Commit

Permalink
fix(noclasspath): Mark target as implicit if it's an anonymous class. (
Browse files Browse the repository at this point in the history
…Closes #707)
  • Loading branch information
GerardPaligot authored and monperrus committed Jul 4, 2016
1 parent b37859c commit cffd98e
Show file tree
Hide file tree
Showing 4 changed files with 2,667 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2094,6 +2094,9 @@ public void visitCtCircularTypeReference(CtCircularTypeReference reference) {

@Override
public <T> void visitCtTypeAccess(CtTypeAccess<T> typeAccess) {
if (typeAccess.isImplicit()) {
return;
}
enterCtExpression(typeAccess);
scan(typeAccess.getAccessedType());
exitCtExpression(typeAccess);
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/spoon/support/compiler/jdt/JDTTreeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2921,6 +2921,9 @@ public boolean visit(MessageSend messageSend, BlockScope scope) {
// We are in a static complex in noclasspath mode.
if (inv.getExecutable() != null && inv.getExecutable().getDeclaringType() != null) {
final CtTypeAccess ta = factory.Code().createTypeAccess(inv.getExecutable().getDeclaringType());
if (ta.getAccessedType().isAnonymous()) {
ta.setImplicit(true);
}
inv.setTarget(ta);
}
if (messageSend.expectedType() != null) {
Expand Down Expand Up @@ -2971,7 +2974,11 @@ public boolean visit(MessageSend messageSend, BlockScope scope) {
context.enter(inv, messageSend);
if (messageSend.receiver.isImplicitThis()) {
if (inv.getExecutable().getDeclaringType() != null && inv.getExecutable().isStatic()) {
inv.setTarget(factory.Code().createTypeAccess(inv.getExecutable().getDeclaringType()));
final CtTypeAccess<?> typeAccess = factory.Code().createTypeAccess(inv.getExecutable().getDeclaringType());
if (typeAccess.getAccessedType().isAnonymous()) {
typeAccess.setImplicit(true);
}
inv.setTarget(typeAccess);
} else if (inv.getExecutable().getDeclaringType() != null && !inv.getExecutable().isStatic()) {
messageSend.receiver.traverse(this, scope);
if (inv.getTarget() instanceof CtThisAccess) {
Expand Down
24 changes: 24 additions & 0 deletions src/test/java/spoon/test/visibility/VisibilityTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import spoon.compiler.SpoonCompiler;
import spoon.reflect.code.CtBinaryOperator;
import spoon.reflect.code.CtFieldAccess;
import spoon.reflect.code.CtInvocation;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtField;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtType;
import spoon.reflect.factory.Factory;
Expand All @@ -23,6 +25,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static spoon.testing.utils.ModelUtils.build;
import static spoon.testing.utils.ModelUtils.canBeBuilt;

Expand Down Expand Up @@ -134,4 +137,25 @@ public boolean matches(CtFieldReference<?> reference) {
assertEquals(tacos, field.getDeclaringType().getDeclaration());
assertEquals(tacos.getFields().get(0), field.getDeclaration());
}

@Test
public void testInvocationVisibilityInFieldDeclaration() throws Exception {
final Launcher launcher = new Launcher();
launcher.getEnvironment().setNoClasspath(true);
launcher.addInputResource("./src/test/resources/noclasspath/Solver.java");
launcher.setSourceOutputDirectory("./target/spooned");
launcher.buildModel();

final CtType<Object> aSolver = launcher.getFactory().Type().get("org.sat4j.minisat.core.Solver");
final CtField<?> lbdTimerField = aSolver.getField("lbdTimer");
final CtInvocation<?> ctInvocation = lbdTimerField.getElements(new TypeFilter<CtInvocation<?>>(CtInvocation.class) {
@Override
public boolean matches(CtInvocation<?> element) {
return "bound".equals(element.getExecutable().getSimpleName()) && super.matches(element);
}
}).get(0);
assertNotNull(ctInvocation.getTarget());
assertTrue(ctInvocation.getTarget().isImplicit());
assertEquals("bound()", ctInvocation.toString());
}
}
Loading

0 comments on commit cffd98e

Please sign in to comment.