shortenClassReferences does not "shorten" java.util.Collections



I am writing an intention that is adding some statements, these statements uses types from external library and some from the JDK (such as `java.util.Collections`). The types from the external libraries are imported correctly, but the types coming from the JDK are not.


At some point the intention adds this statement

var stmt = JavaPsiFacade.getElementFactory(project).createStatementFromText("\"\"))", null)

Then the invoke method of the intention terminates with these lines on the `PsiCodeBlock` that received the statements :

var javaStyleManager = JavaCodeStyleManager.getInstance(project);
var formatter = CodeStyleManager.getInstance(project);


The test is written this way : 

public void testCatch() {
myFixture.configureByFile("before" + name + ".java");

assertTrue(action.isAvailable(getProject(), getEditor(), getFile()));
myFixture.checkResultByFile("after" + name + ".java");

protected @NotNull LightProjectDescriptor getProjectDescriptor() {
return new DefaultLightProjectDescriptor() {
public void configureModule(@NotNull Module module, @NotNull ModifiableRootModel model, @NotNull ContentEntry contentEntry) {
super.configureModule(module, model, contentEntry);
String jarPath = new File("src/test/testData/jars").getAbsolutePath();


The test extends `LightJavaCodeInsightFixtureTestCase`.


The test fails because `java.util.Collections` does not appear in the imports. I have searched on this forum similar problem. The only thing that seemed to work is a comment from Yann Cebron .


If the fixture in the test is configured with 

myFixture.addClass("package java.util; public class Collections {}");

The intention correctly add the imports of `java.util.Collections`.


While this work, I wonder if I didn't miss something in the test setup, maybe around the `LightProjectDescriptor`. Thanks in advance for your support.



This is expected behavior, as JDK classes are not automatically available in tests but mockJDK must be configured. See


Ah ok! That makes sense thank you !

Do you think the alternative I took with 

myFixture.addClass("package java.util; public class Collections {}");

is acceptable if the code only require a few select type ? I.e it doesn't require the other types that are declared in ?


OK, great ! Thank you again for the fast reply !



Please sign in to leave a comment.