A simple way to create static imports for constants in existing code?

前端 未结 9 865
心在旅途
心在旅途 2021-01-30 21:17

If I have a constant BAR in Foo, which I will use in a class C I\'ll have to write

Object o = Foo.BAR + \"...\";

which I can use Ctrl-Shift-M i

9条回答
  •  余生分开走
    2021-01-30 21:50

    I don't know any other automated refactorings to do what you're looking for, but there are two key aspects to doing this - one is adding the imports and the other is removing the class name from the constants.

    For adding the imports, I recommend pasting import static application.KeyHolder.*; after the package on every file which uses them.

    For removing the class name, in eclipse do a Java search for KeyHolder.* and search for a field. You can then do a find/replace for "KeyHolder." and replace with "". In order to prevent mismatches make sure the replaced item count is equal to the number of matches for that file. Also, make sure you search down and start after the imports.

提交回复
热议问题