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

Package of array type reference marked implicit if component type is available on classpath (noclasspath) #3336

Closed
slarse opened this issue Apr 19, 2020 · 2 comments · Fixed by #3357

Comments

@slarse
Copy link
Collaborator

slarse commented Apr 19, 2020

It seems like this issue persists when running in classpath mode as well, but I'm running everything in noclasspath so I'll address this from that perspective. Anyway, array type references for which the component type is in fact available on the classpath always get their package references marked implicit, regardless of what the source code looks like. Here's a trivial example.

import java.util.*;

public class QualifiedArrayType {
    java.lang.Object[] builtinTypeArray;
    java.util.Date[] importedTypeArray;
}

The packages here are clearly explicit, but they are marked implicit. I believe this is due to a few issues that "work together".

First, when building an array type reference, the component type seems to typically be set here:

arrayref.setComponentType(getTypeReference(binding.leafComponentType(), resolveGeneric));

That call to getTypeReference seems to always end up in this case, at which point the package is set to be implicit:

} else {
CtPackageReference packageReference = getPackageReference(binding.getPackage());
packageReference.setImplicit(true);
ref.setPackage(packageReference);
}

That's the root of the problem. I don't understand why the reference is set to implicit here, but removing that causes the test case SpoonifierTest.testGeneratedSpoonifyCode to fail. So I'm assuming that it should be set implicit here.

I found another potential solution by calling JDTTreeBuilderHelper.handleImplicit on the array type reference after it has been created, with a tiny modification in handleImplicit. Putting up a PR once I've tried it out as a solution for my own problems.

@MartinWitt
Copy link
Collaborator

After a first look, i would say the SpoonifierTest.testGeneratedSpoonifyCode is created from the AST with all "bugs" and the test could be changed. SpoonifyVisitor creates a String allowing recreation of the AST including it's old state.

@slarse
Copy link
Collaborator Author

slarse commented Apr 19, 2020

@MartinWitt That sounds reasonable. I just felt a bit uneasy simply removing the setImplicit there as I don't understand its purpose.

I did try removing it, and from initial testing it solves the most serious issues I encountered. But that could simply be that for my purposes, it's significantly better that a package that must be explicit is actually marked explicit, than one that should be implicit is accidentally marked explicit.

I'm running some more tests locally to see if other issues crop up from removing that statement. If anyone knows why the packageReference.setImplicit(true) statement sits where it does up there, I'd be happy to hear about it.

# 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.

2 participants