Skip to content

Organize imports generates duplicate static import statement #1814

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

Merged
merged 1 commit into from
Jul 13, 2021

Conversation

snjeza
Copy link
Contributor

@snjeza snjeza commented Jul 9, 2021

Fixes redhat-developer/vscode-java#2012

Signed-off-by: Snjezana Peco [email protected]

@rgrunber
Copy link
Contributor

Thanks. I'll try to have a look at this today.

Copy link
Contributor

@rgrunber rgrunber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the case where an existing import is on-demand (eg. import static org.hamcrest.MatcherAssert.*; ) . The favourite will still get inserted. You may have to check if any of the favourite imports match an existing on-demand one.

UPDATE : My guess is this might involve using SimpleNode#resolveBinding() ? (See OrganizeImportsOperation#addStaticImports(..)) If the method reference can be resolved (which actually happens higher up in the code) then there's no need to add another import for it.

IImportDeclaration[] iImportDeclarations = unit.getImports();
for (IImportDeclaration importDeclaration : iImportDeclarations) {
String elementName = importDeclaration.getElementName();
int lastDot = elementName.lastIndexOf('.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd probably use Signature.getSimpleName(elementName) assuming it does exactly what you need.

@snjeza
Copy link
Contributor Author

snjeza commented Jul 13, 2021

UPDATE : My guess is this might involve using SimpleNode#resolveBinding() ? (See OrganizeImportsOperation#addStaticImports(..)) If the method reference can be resolved (which actually happens higher up in the code) then there's no need to add another import for it.

Fixed.

@rgrunber rgrunber merged commit 256e705 into eclipse-jdtls:master Jul 13, 2021
@rgrunber rgrunber added this to the End July 2021 milestone Aug 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Organize imports generates static import statement, even if already imported
2 participants