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
Eclipse can do a global search and replace with regular expression over all files in the project.
To add the static import at the beginning of all files you could replace the package declaration by itself plus the static import.
Then do a project wide organize imports, and you're all set.
Be careful, though, this could also make a big mess if you do it wrong.
I don't know if it would fit your needs, but you can always have these constants defined in an interface and have all your classes just implement this interface. Then using some smart RegExp as others suggest you could get rid of all Foo.
occurences.
Example:
public interface Foo {
static final String constant1 = "Some";
static final String constant2 = "Value";
//...
}
And then
public class YourClass implements Foo {
public YourClass() {
System.out.println("Hello " + constant1);
}
}
Well something which pops in as a solution. I would recommend using a simple groovy script. Basically the semantics are related to parsing the import statements and the files they refer to. Some basic steps could be like
If i get some time i will try and post a small groovy script for doing the same.
Make sure no conflicts with other constant classes
replace all the "Foo." with empty (" ")
use this line at the top. worked for me
import static Foo.*
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.
Regular expression was invented to solve problems like these! =)
you will need to write a small script (or use the IDE regex search/replace). The only problem is that if there are lots of classes that needs to be statically imported, then its just as much work. Otherwise, if its just one class like FOO.BLAH, then you can use a regex like
(\bFOO\.(\w+)\b)
-> replace with group 2 ($2 or \2 or however your regex searcher does capture replaces).
you can try it here : http://www.gskinner.com/RegExr/ . Select the replace tab and type in $2
A problem might arise if you have expressions like this though:
FOO f = FOO.blah + "FOO.blah"
, so watch out for that.
As for the import statement at the top of the file, eclipse has an auto import feature where it does autoimport on format, and it works as long as the name of the import is unique enough. Or, if you cant use it because the name is too common, you can just use a perl script to do the regex instead, and prepend the static imports.