Shorten class references - adding import statements for a single field

I'm trying to add a import statement for newly altered field(my inspection replaces the deprecated method name with the  supported one).Example:
private DeprecatedClass s = new DeprecatedClass.getInstance();
after applying a fix
private DeprecatedClass s = new SupportedClass.getInstance();

I've found code to add a new field and automatically add a import for it.

 PsiField newField =
 factory.createFieldFromText("private static final java.util.List  myList = new java.util.LinkedList();", parentClass);
final PsiElement addedField = parentClass.add(newField);
 CodeStyleManager codeStyleManager =
 manager.getCodeStyleManager();
 codeStyleManager.shortenClassReferences(addedField);

 


But how could I create an import of the method I changed? (in the example I've provided before it would be import com.SupportedClass;)
What to pass to shortenClassReference method?
I've tried passing directly a field created from text like "private DeprecatedClass s = new SupportedClass.getInstance();" but it doesn't work.
I would happy provide the fully qualified name of the class i want to import but the shortenClassReference method takes only PsiElements as parameters.

Thanks in advance
1 comment
Comment actions Permalink

You need to create field with initializer containing fully qualified name of your class, e.g. 'private DeprecatedClass s = new com.SupportedClass.getInstance();'
and use remaining code of quoted example

0

Please sign in to leave a comment.