Unshorten reference (import)

Hi,

I'd like to "unshorten" the reference to a class (the exact opposite to javaCodeStyleManager.shortenClassReferences(referenceClazz);). Is there any kind of utility method to achieve this kind of thing?

To illustrate this further, please consider the following example:

import blablub.A.ABuilder;

public class Test {
    public static void main(String[] args) {
        new ABuilder();
    }
}


I'd like to get the following result:

import blablub.A;


public class Test {
    public static void main(String[] args) {
        new A.ABuilder();
    }
}

Thank you very much in advance,
Matthias
2 comments
Comment actions Permalink

I'm not aware of any existing utility method to do this.

0
Comment actions Permalink

Well I found a solution for this issue. An example how you can do it is
com.intellij.psi.util.ImportsUtil#expand (which is used within the ExpandStaticImportAction). You can modify the method a little bit, resulting in a method like that one:

 
public static void expand(@NotNull PsiJavaCodeReferenceElement refExpr, PsiImportStaticStatement staticImport) {
  final PsiElementFactory elementFactory = JavaPsiFacade.getElementFactory(refExpr.getProject());
  final PsiReferenceExpression referenceExpression = elementFactory.createReferenceExpression(staticImport.resolveTargetClass());
  if (refExpr instanceof PsiReferenceExpression) {
    ((PsiReferenceExpression)refExpr).setQualifierExpression(referenceExpression);
  }
  else {
    refExpr.replace(elementFactory.createReferenceFromText(referenceExpression.getText() + "." + refExpr.getText(), refExpr));
  }
}


Matthias

0

Please sign in to leave a comment.