Using codeStyleManager.shortenClassReferences for single field only


I'm adding a field to a class, and calling "codeStyleManager.shortenClassReferences(psiClass)"
afterwards to move the FQN classname (of the field just added) to the imports
list.

I'd like to perform this operation for the field only, but it seems to have
no effect. (Only psiClass and psiMethod seem to be valid args to shortenClassReferences()).

I assume that the workaround is simply to add the necessary imports myself,
correct?

And another question: what does "importList.findOnDemandImportStatement()"
actually mean? Does IDEA remember how an import statement was created (manually
or "on demand"?)? What's the distinction, and what's the use for this?


0

In Inspection Gadgets shortenClassReferences is used all the time for PsiStatements, PsiExpressions and other PsiElements, so it should work for PsiFields too.

importList.findOnDemandInportStatement() finds "" imports (e.g. import java.awt.;)

Bas

0

Hello Bas,

In Inspection Gadgets shortenClassReferences is used all the time for
PsiStatements, PsiExpressions and other PsiElements, so it should work
for PsiFields too.


The code I'm looking at is:
-


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



The result I observe is certainly not shorteded.

This is in IDEA 5.1.2, and the sandbox instance has "Optimize imports on
the fly" disabled.


importList.findOnDemandInportStatement() finds "*" imports (e.g.
import java.awt.*;)


Ok. Seems a bit of strange naming? (I'd expect findWholePackageImportStatements()
or something similar).

Is there some code that computes whether an importlist does include include
(explicitly or implicitly) a specific class?

P.S. Did you manage to solve your SVN problem?


0

The result I observe is certainly not shorteded.


Found out my mistake: you have to operate on the physical element returned
from add(), not on the nonphysical elements passed to add().


0

Hi Taras,

Try this:


importList.findOnDemandInportStatement() finds "*"

imports (e.g.

import java.awt.*;)


Ok. Seems a bit of strange naming? (I'd expect
findWholePackageImportStatements()
or something similar).


I guess it is, it never bothered me though.

Is there some code that computes whether an
importlist does include include
(explicitly or implicitly) a specific class?


No that I know of, but it should not be hard to make.

P.S. Did you manage to solve your SVN problem?


Not yet, but I did not invest any time in it either. I have another machine I can still work on.

Bas

0

Whoops, you discovered the problem yourself already.

0
Avatar
Permanently deleted user

Thank you sir, you saved me much grief :-). I was curious though -- there is a PsiElement returned from shortenClassReferences, but that does not seem to be needed. For example:

        field = JavaPsiFacade.getElementFactory(project).createFieldFromText(code, propertyField);         field = (PsiField) propertyField.getContext().addAfter(field, propertyField);         JavaCodeStyleManager.getInstance(project).shortenClassReferences(field);



So is the PsiElement returned from shortenClassReferences the same as the element passed to it? It seems to work so I'd guess so, but just curious.
0

请先登录再写评论。