Static Imports and IntelliJ


I am currently trying to generate a static import statement to a newly generated Java class. What I got are references to PsiElements. How can I convert those to static import statements?

Whenever I try to call

AddSingleMemberStaticImportAction.invoke(erzeugerClazz.getContainingFile(), psiElement)

I get a ClassCastException:
java.lang.ClassCastException: com.intellij.psi.impl.source.PsiJavaFileImpl cannot be cast to com.intellij.psi.PsiJavaCodeReferenceElement

When I try to call

PsiClass clazz = AddOnDemandStaticImportAction.getClassToPerformStaticImport(psiElement);
if (clazz != null) {
  AddOnDemandStaticImportAction.invoke(project, erzeugerClazz.getContainingFile(), fileEditorManager.getSelectedTextEditor(), clazz);
clazz is null.
Obviously I am doing something wrong. But how can I add static imports to a PsiClass? Where can I find some kind of example on how to add those imports?

Thanks, Matthias

1 comment
Comment actions Permalink

What exactly is the psiElement that you're passing to AddSingleMemberStaticImportAction.invoke()? It has to be the specific qualified reference element that you need to shorten. Looks like you're passing something else to it.


Please sign in to leave a comment.