How to automatically add an import statement as needed?

I'm making a refactoring to support moving to EJBs. As part of that, I will be adding @EJB and @Stateless annotations. I'm trying to automatically add an import statement for these as well, if they're not already imported, but am stuck on this:

  public static void ensureImport(PsiClass aClass, Class toImport) {
    final Project project = aClass.getProject();
    PsiFile file = aClass.getContainingFile();
    if (file instanceof PsiJavaFile) {
      final PsiElementFactory elementFactory = JavaPsiFacade.getInstance(project).getElementFactory();
      ((PsiJavaFile)file).importClass(elementFactory.createClass(toImport.getCanonicalName()));
    }
  }

Calling with EJB.class.getCanonicalName() gives:

Failed with com.intellij.util.IncorrectOperationException: 'javax.ejb.Stateless' is not an identifier.
com.intellij.util.IncorrectOperationException: 'javax.ejb.Stateless' is not an identifier.
    at com.intellij.psi.util.PsiUtil.checkIsIdentifier(PsiUtil.java:701)
    at com.intellij.psi.impl.PsiElementFactoryImpl.createClass(PsiElementFactoryImpl.java:85)
    at dk.amplex.intellij.refactorings.Utils.ensureImport(Utils.java:97)
...

To which I go: Is too!

Or is there some better way to go about this?  Does importClass() even check if it's already imported?

Thanks in advance,
-Lars Clausen

2
1 comment

Hello Lars,

The correct approach to this is to put fully qualified names in the generated
code and then call JavaCodeStyleManager.shortenClassReferences() to add the
necessary imports automatically.

I'm making a refactoring to support moving to EJBs. As part of that, I
will be adding @EJB and @Stateless annotations. I'm trying to
automatically add an import statement for these as well, if they're
not already imported, but am stuck on this:

public static void ensureImport(PsiClass aClass, Class toImport) {
final Project project = aClass.getProject();
PsiFile file = aClass.getContainingFile();
if (file instanceof PsiJavaFile) {
final PsiElementFactory elementFactory =
JavaPsiFacade.getInstance(project).getElementFactory();

((PsiJavaFile)file).importClass(elementFactory.createClass(toImport.ge
tCanonicalName()));
}
}
Calling with EJB.class.getCanonicalName() gives:

Failed with com.intellij.util.IncorrectOperationException:
'javax.ejb.Stateless' is not an identifier.
com.intellij.util.IncorrectOperationException: 'javax.ejb.Stateless'
is not an identifier.
at
com.intellij.psi.util.PsiUtil.checkIsIdentifier(PsiUtil.java:701)
at
com.intellij.psi.impl.PsiElementFactoryImpl.createClass(PsiElementFact
oryImpl.java:85)
at
dk.amplex.intellij.refactorings.Utils.ensureImport(Utils.java:97)
..
To which I go: Is too!

Or is there some better way to go about this? Does importClass() even
check if it's already imported?

Thanks in advance,
-Lars Clausen
---
Original message URL:
http://www.jetbrains.net/devnet/message/5234873#5234873

--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"


3

Please sign in to leave a comment.