Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

bug: Unqualified type access becomes fully qualified in some circumstances (noclasspath) #3370

Closed
slarse opened this issue May 16, 2020 · 0 comments · Fixed by #3371
Closed

Comments

@slarse
Copy link
Collaborator

slarse commented May 16, 2020

This bug specifically affects the method JDTTreeBuilderHelper#createTypeAccessNoclasspath

<T> CtTypeAccess<T> createTypeAccessNoClasspath(SingleNameReference singleNameReference) {
final CtTypeReference<T> typeReference = jdtTreeBuilder.getFactory().Core().createTypeReference();
if (singleNameReference.binding == null) {
typeReference.setSimpleName(CharOperation.charToString(singleNameReference.token));
} else {
typeReference.setSimpleName(CharOperation.charToString(singleNameReference.binding.readableName()));
}
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);
}

My previous fix (PR #3364 for issue #3363) was only partially successful. Apparently, it can happen that the packageOrDeclaringType variable is null (duh), and then it must be made implicit afterward This occurs when the JDT compiler can't resolve a package or declaring type. This occurs for example in the code below, where Spoon will guess that the type belongs to the CU's package.

package pkg;

public class Cls {
    public static void main(String[] args) {
        SomeClass.someMethod(args);
    }
}

The type access to SomeClass will become explicitly qualified as pkg.SomeClass.

The fix is simple, I'm working on a PR.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant