I was cleaning up some code and happened upon com.example.StringHelper
that contained 6 or 7 public static
methods (for example concatStrings(String...)
, but no member fields. There were a number of classes subclassing this class just so they could call concatStrings(str1, str2)
without prefixing it with the class like so: StringHelper.concatStrings(str1, str2)
.
I didn't want them subclassing a class just for that reason, so I broke a bunch off. I pasted the following static import into the top of any file subclassing it after removing the extends StringHelper
:
import static com.example.StringHelper.*;
Eclipse simplified this into specific imports for only the methods being used.
Question: Is there a simple way 开发者_运维技巧to have Eclipse "inline" these static imports? Can I get it to remove the actual static import and prefix every call with StringHelper.
instead?
Note This is a simplified contrived example, so please don't complain about why we need a StringHelper in the first place.
This will do it:
- Highlight one invocation of concatStrings(). Make sure to include the parentheses in the highlighting. Select Refactor/Extract Method, call it foo. Make sure to check the "Replace 47 additional occurrences of statements with method"
- In your new foo method, add the "StringHelper." prefix to the invocation of concatStrings
- remove the import static
- Do Refactor/Inline to get rid of foo and put your static calls back where they belong, along with their shiny new Prefixes.
Don't know if there's an automatic way, but I think it can help in a manual way. Delete the import, then click on each line with an error on it. Press ctrl-1 for 'quick fix' and choose the quick fix that prefixes the package name rather than add an import.
Preferences -> Java -> Code Style -> Organize Imports
There you can configure how many imports will be required to group them.
Preferences > Java > Code Style > Organize Imports:
"Number of static imports needed for .*" set this to 1.
精彩评论