void (PsiJavaFile javaFile, String importStatementOnDemand) { PsiElementFactory factory = JavaPsiFacade.getElementFactory(javaFile.getProject()); PsiImportStatement is = factory.createImportStatementOnDemand(fixImportStatement(importStatementOnDemand)); // add the import to the file, and optimize the imports PsiImportList importList = javaFile.getImportList(); if (importList != null) { importList.add(is); } JavaCodeStyleManager.getInstance(javaFile.getProject()).optimizeImports(javaFile); }