views:

1277

answers:

6

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 reason 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.

+12  A: 

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 :-).

paxdiablo
I already have checked Eclipse and it will move all string in a txt property file, which is then loaded by Messages.getString(). This is not what I want to do. I just want my string to be play old java constant.
Thierry-Dimitri Roy
This isn't a bad idea. Once you have those, you can look for those patterns and replace them with what you want.
ykaganovich
Additionally, this method does not recognize same string across the same project. I want all instance of "Cat" to point to Constants.CAT
Thierry-Dimitri Roy
A: 

Have you heard of grep? Maybe sed or awk? Maybe you could use grep combined with regexp search/replace in emacs?

Marcin
-1 condescending and doesn't answer the question
ykaganovich
Well, actually it does answer the question: using these tools the questioner could have already solved his problem.
Marcin
i agree, regex search and replace would work. However, it does take a bit of effort (since how would you name the constants?)
Chii
You could use the content of the string, capitalised, and with disallowed characters squeezed out/translated to allowed ones. So, you might need tr as well. Or you could do it all in perl if you roll that way.
Marcin
+1  A: 

There are some good reasons why you wouldn't want to do this. Aside from the fact that any such generated file (I didn't know about the eclipse function)is not going to distinguish between strings that you're setting, for example, as constructor args in test classes and things you actually want to have as constants, the bigger issue is that all of your public static finals are going to be compiled into your classes, and if you want to alter the classes behaviour you'll need to alter not only the class holding the constants but everything that references it.

Steve B.
A: 

I fully acknowledge what Pax Diablo said. We're using that function too.

When applied to a class file the function "Externalize strings" will create two files, a class Messages.class and a properties file messages.properties. Then it will redirect all direct usages of string literals to a call to Messages.get(String key) and using the key you entered for the string in the "Ext. String" wizard.

BTW: What's so bad about property files? As he said, you can just change the properties file and don't have to change the class if you need to change the text.

Another advantage is this one: The way of extracting the string literals into a property file leaves you free to translate the source language in any language you want without modifying any code. The properties file loader loads the target language file automatically by using the corresponding file with the language iso code. So you don't have to worry about the platform your code runs on, it will select the appropriate language (nearly) automatically. See documentation of class ResourceBundle for how this works in detail.

dhiller
A: 

You may want to check out the Jackpot source transformation engine in NetBeans which would allow you to script your source transformations.

Peter Kelley
+6  A: 

To complete Peter Kelley answer, you might consider for eclipse IDE the AST solution.

You might then write an AST program which parse your source code and does what you want.

A full example is available in this eclipse corner article, also more details in the eclipse help. And in the slide 59 of this presentation, you see how to apply a change to your source code.

VonC