Skip to content

Commit

Permalink
fix: Ensure unqualified member access remains unqualified (INRIA#3364)
Browse files Browse the repository at this point in the history
  • Loading branch information
slarse authored May 11, 2020
1 parent 37b100a commit 3a8d04d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -639,7 +639,12 @@ <T> CtTypeAccess<T> createTypeAccessNoClasspath(SingleNameReference singleNameRe
} else {
typeReference.setSimpleName(CharOperation.charToString(singleNameReference.binding.readableName()));
}
jdtTreeBuilder.getReferencesBuilder().setPackageOrDeclaringType(typeReference, jdtTreeBuilder.getReferencesBuilder().getDeclaringReferenceFromImports(singleNameReference.token));
CtReference packageOrDeclaringType = jdtTreeBuilder.getReferencesBuilder().getDeclaringReferenceFromImports(singleNameReference.token);
if (packageOrDeclaringType != null) {
// must be implicit as a SingleNameReference is not qualified, see #3363
packageOrDeclaringType.setImplicit(true);
}
jdtTreeBuilder.getReferencesBuilder().setPackageOrDeclaringType(typeReference, packageOrDeclaringType);
return jdtTreeBuilder.getFactory().Code().createTypeAccess(typeReference);
}

Expand Down
20 changes: 20 additions & 0 deletions src/test/java/spoon/test/reference/TypeReferenceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -731,4 +731,24 @@ private static CtTypeReference<?> getDeepestComponentType(CtArrayTypeReference<?
}
return (CtTypeReference<?>) ref;
}

@Test
public void testUnqualifiedExternalTypeMemberAccess() {
// contract: if a type member is accessed without qualification, but it is not part of the current
// compilation unit, any qualification attached to it through import resolution must remain implicit
// See #3363 for details
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/resources/noclasspath/UnqualifiedExternalTypeMemberAccess.java");
launcher.getEnvironment().setNoClasspath(true);
CtModel model = launcher.buildModel();
List<CtTypeReference<?>> typeReferences = model.getElements(e -> e.getSimpleName().equals("SOMETHING"));

assertEquals("There should only be one reference to SOMETHING, check the resource!", 1, typeReferences.size());

CtTypeReference<?> typeRef = typeReferences.get(0);
CtTypeReference<?> declType = typeRef.getDeclaringType();

assertEquals("Constants", declType.getSimpleName());
assertTrue(declType.isImplicit());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package pkg;

import static pkg.constants.Constants.SOMETHING;

public class UnqualifiedExternalTypeMemberAccess {
public static void main(String[] args) {
System.out.println(SOMETHING);
}
}

0 comments on commit 3a8d04d

Please sign in to comment.