How are static imports suggestions selected?
More than once I've been wondering how IntelliJ chooses which static imports to suggest. Most of the time it gets them right, probably because of "context", but sometimes (usually for new files) it will suggests or even import some exotic package instead of the most obvious ones.
For example, in this code (generated by the IDE with the "go to test class" shortcut on the EmailMessage class and edited adding the test case)
package acme.test;
import static org.junit.Assert.*;
import org.junit.Test;
public class EmailMessageTest {
@Test
public void itSplitsHeadersAndBody() {
EmailMessage message = EmailMessage.fromString(...);
assertThat(message.headers(), is(...));
assertThat(message.body(), is(...));
}
}
it will highlight the two is calls and pressing Alt-Enter for quick fixes it will suggest
- Create method is in EmailMessgeTest
- Import static method 'com.sun.apache.xerces.internal.util.PropertyState.is'
- Qualify static call 'com.sun.apache.xerces.internal.util.PropertyState.is'
instead of the much more obvious org.hamcrest.CoreMatchers.is
In this other case
package acme.email;
import java.util.List;
public class EmailMessage {
public static EmailMessage fromString(String message) {
return new EmailMessage();
}
public List<String> headers() {
return Collections.emptyList();
}
public String body() {
return "";
}
}
It will suggest to import Collections from edu.emory.matchs.backport.java.util instead of the obvious java.util.
How are suggestions chosen? Is there something I should change in my settings and/or project (which is a spring-boot project on java 1.8)?
Please sign in to leave a comment.
Hello,
In case several dependencies are defined in project, several imports should be suggested to be chosen. Can you please clarify, do I get it right that several imports are suggested, you are just unhappy with the higher priory given to the imports that are unlikely to be chosen, right?
Hi,
in the first example above the suggestions I've mentioned are all I get, IntelliJ seems to have a specific dislike for hamcrest since that happens across most of my projects when creating new tests. In the second example I get both java.util and edu...util but the latter is prioritized, so I guess I'm experiencing problems both in the selection and order of the recommendations.
Hi,
Can you please share a sample project illustrating the case when the import for hamcrest is not suggested though its dependency is presented in the classpath (if convenient you may attach the sample to the issue created here: https://youtrack.jetbrains.com/issues/IDEA
or upload the sample to the https://uploads.services.jetbrains.com and share the link).
Thanks
Hi,
I've created a simple project with spring startr and imported it into IntelliJ, adding a dependency on Junit4 and setting the project interpreter to Java 8. The upload id is Upload id: 2021_06_03_RgAswQcx8BxVA8tZ (file: intellij-test.zip)
If you open the EmailMessgeTest will
Hi,
Thank you for the sample provided. Can you please clarify what is expected behaviour in the following case:
do you expect the import of method to be not suggested in case its parameter is not yet resolved?
The behaviour in the second and the third cases look like expected as the IDE suggests the most appropriate imports taking into account the types of parameters (List and String).
I'm not really sure and I've no idea how suggestions are selected, hence the whole thread ;-)
To me it's strange that if I don't have the java.util.Collections import in place the suggestions I get for is are a list of imports that are (to me) mostly appropriate (given that assertThat expects some kind of matcher in the second argument). Once I add the java.util.Collections import the suggestions narrow down to a single package, which is (again, to me) wildly inappropriate: I would expect the IDE to never suggest anything inside com.sun.* or sun.*
Can you please share the screenshot of what imports are suggested for you after the `Collections` is resolved, I see the following behaviour:
if you have the different one, can you please check how it works for you with the latest available version:
https://www.jetbrains.com/idea/nextversion.
Thank you
Hi,
trying to capture a screenshot I think I've stumbled on something else. If I press Alt-Enter and chose Import-Class to fix the Collections import, after a short delay a tooltip appears offering to import ElementMatchers. If I press Alt-Enter again I get your behaviour.
If I dismiss the tooltip or move the caret and press Alt-Enter instead I get this
Hello, the issue seems to be the duplicate of this one: https://youtrack.jetbrains.com/issue/IDEA-223655, it should be fixed in 2021.2 EAP1. Can you please try it:
https://www.jetbrains.com/idea/nextversion
Thank you
Yep, the EAP fixes that, thanks!