Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
643 views
in Technique[技术] by (71.8m points)

eclipse - Extract all string from a java project

I have a rather big number of source files that I need parse and extract all string literals and put them in a file as play old java constant.
For exemple:

Label l = new Label("Cat");

Would become:

Label l = new Label(Constants.CAT);

And in Constants.java I would have:

public final static String CAT = "Cat";

I do not want the strings to be externalized in a property text file.
One reason is for consistency and code readability.
The other is that our client code uses GWT, which does not support Java property text file mechanism.

I could write some sort of parser (using ant replace task maybe)?
But I wondered if an IDE already does this sort of thing automatically.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Eclipse does do this automatically. Right-click the file, choose "Source", then "Externalize strings"

This doesn't do exactly what you requested (having the strings in a Constants.java file as Strings) but the method used is very powerful indeed. It moves them into a properties file which can be loaded dynamically depending on your locale. Having them in a separate Java source file as you suggest means you'll either have ALL languages in your application at once or you'll ship different applications depending on locale.

We use it for our applications where even the basic stuff has to ship in English and Japanese - our more complicated applications ship with 12 languages - we're not a small software development company by any means :-).

If you do want them in a Java file, despite the shortcomings already mentioned, it's a lot easier to write a program to morph the properties file into a Java source file than it is to try and extract the strings from free-form Java source.

All you then need to do is modify the Accessor class to use the in-built strings (in the separate class) rather than loading them at run time.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...