-
-
Notifications
You must be signed in to change notification settings - Fork 352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: prevent importing classes which are already imported via *
#4320
Changes from 3 commits
c07aafd
c4a86f9
b1c2ff4
a76ff26
ed2de44
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -184,6 +184,9 @@ void addImport(CtReference ref) { | |
//java.lang is always imported implicitly. Ignore it | ||
return; | ||
} | ||
if (isPackageImportedViaWildcardAndUnresolved(packageRef)) { | ||
return; | ||
} | ||
if (Objects.equals(packageQName, packageRef.getQualifiedName()) && !isStaticExecutableRef(ref)) { | ||
//it is reference to a type of the same package. Do not add it | ||
return; | ||
|
@@ -201,6 +204,12 @@ void addImport(CtReference ref) { | |
} | ||
} | ||
|
||
private boolean isPackageImportedViaWildcardAndUnresolved(CtPackageReference packageReference) { | ||
return compilationUnit.getImports().stream().anyMatch( | ||
ctImport -> ctImport.toString().contains(packageReference.toString()) | ||
&& ctImport.getImportKind() == CtImportKind.UNRESOLVED); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When Imagine that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I delved into You are right that this case will be a false-positive. The Anyway, of course, we need a better way to know if the wildcard import exists corresponding to the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
You're thinking of this from the perspective of this being the input, but Spoon is made for AST transformations. The purpose of auto-import functionality is to automatically add imports after one has transformed the AST. For example, in a Spoon model, I can insert a variable declaration with type java.util.List<Integer> myNewList = new java.util.ArrayList<>(); No one writes Java like that. Rather, I want this: List<Integer> myNewList = new ArrayList<>(); Since imports aren't really part of the AST (they belong to the compilation unit), it's kind of awkward to get that right. As a concrete example closer to what you work with, look at the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That makes sense.
Yes, I disabled the |
||
} | ||
|
||
void onCompilationUnitProcessed(CtCompilationUnit compilationUnit) { | ||
ModelList<CtImport> existingImports = compilationUnit.getImports(); | ||
Set<CtImport> computedImports = new HashSet<>(this.computedImports.values()); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import java.util.*; | ||
|
||
public class WildcardImport { | ||
private static List<Integer> x = new ArrayList<>(); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Name is a bit weird to me; you can't import a package. Maybe something like this?
Not crazy about that name either, but as a package itself can't be imported, I don't think we should... imply that it can.