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:
我有相当多的源文件需要解析和提取所有字符串文本,并将它们作为旧的java常量放在一个文件中。为例:
Label l = new Label("Cat");
Would become:
将成为:
Label l = new Label(Constants.CAT);
And in Constants.java
I would have:
和常量。java我会:
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.
我不希望字符串在属性文本文件中被具体化。原因之一是一致性和代码可读性。另一个是我们的客户端代码使用GWT,它不支持Java属性文本文件机制。
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.
我可以编写某种解析器(可能使用ant replace任务)?但是我想知道IDE是否已经自动地做了这类事情。
5 个解决方案
#1
9
To complete Peter Kelley answer, you might consider for eclipse IDE the AST solution.
要完成Peter Kelley的回答,您可以考虑使用eclipse IDE作为AST解决方案。
You might then write an AST program which parse your source code and does what you want.
然后,您可以编写一个AST程序来解析源代码并执行所需的操作。
A full example is available in this eclipse corner article, also more details in the eclipse help.
And you can find some examples in Listing 5 of the section "Implementation of in-place translation" of Automating the embedding of Domain Specific Languages in Eclipse JDT, alongside multiple examples in GitHub projects.
在这篇eclipse corner文章中有一个完整的示例,在eclipse帮助中还有更多细节。您可以在清单5的“就地翻译实现”一节中找到一些示例,该节自动化在Eclipse JDT中嵌入特定领域的语言,以及GitHub项目中的多个示例。
#2
21
Eclipse does do this automatically. Right-click the file, choose "Source", then "Externalize strings"
Eclipse会自动执行此操作。右键单击文件,选择“Source”,然后“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.
这并不是您所要求的那样(字符串在常量中)。java文件作为字符串)但是使用的方法确实非常强大。它将它们移动到属性文件中,该文件可以根据您的语言环境动态加载。如您所建议的那样,将它们放在一个单独的Java源文件中,意味着您将同时在应用程序中拥有所有语言,或者您将根据语言环境交付不同的应用程序。
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 :-).
我们用它来做我们的应用,即使是最基本的东西也要用英语和日语——我们更复杂的应用程序要用12种语言——我们绝不是一个小的软件开发公司:-)
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.
如果您想要在Java文件中使用它们,尽管已经提到了一些缺点,但是编写一个程序将属性文件转换成Java源文件比尝试从*形式的Java源代码中提取字符串要容易得多。
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.
然后,您需要做的就是修改Accessor类以使用内置的字符串(在单独的类中),而不是在运行时加载它们。
#3
1
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.
有一些很好的理由可以解释为什么你不想这样做。除了任何生成的文件(我不知道eclipse功能)不会区分字符串设置,例如,作为构造函数参数的测试类,你真正想要的东西作为常量,更大的问题是,你所有的公共静态决赛会编译成类,如果您想要改变类行为,您不仅需要改变保存常量的类,还需要改变引用常量的所有东西。
#4
1
I fully acknowledge what Pax Diablo said. We're using that function too.
我完全承认Pax Diablo说过的话。我们也用这个函数。
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.
当应用到类文件时,函数“Externalize strings”将创建两个文件,一个类消息。类和属性文件message .properties。然后,它将字符串文字的所有直接用法重定向到对消息的调用。获取(字符串键)并使用“Ext. String”向导中为字符串输入的键。
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.
另一个优点是:将字符串文本提取到属性文件的方法使您可以*地使用任何您想要的语言翻译源语言,而无需修改任何代码。属性文件加载程序通过使用具有语言iso代码的相应文件自动加载目标语言文件。因此,您不必担心代码运行的平台,它将自动选择适当的语言(几乎)。请参阅ResourceBundle的文档,了解它是如何工作的。
#5
0
You may want to check out the Jackpot source transformation engine in NetBeans which would allow you to script your source transformations.
您可能想要在NetBeans中检查彩票源转换引擎,它允许您编写源转换脚本。
#1
9
To complete Peter Kelley answer, you might consider for eclipse IDE the AST solution.
要完成Peter Kelley的回答,您可以考虑使用eclipse IDE作为AST解决方案。
You might then write an AST program which parse your source code and does what you want.
然后,您可以编写一个AST程序来解析源代码并执行所需的操作。
A full example is available in this eclipse corner article, also more details in the eclipse help.
And you can find some examples in Listing 5 of the section "Implementation of in-place translation" of Automating the embedding of Domain Specific Languages in Eclipse JDT, alongside multiple examples in GitHub projects.
在这篇eclipse corner文章中有一个完整的示例,在eclipse帮助中还有更多细节。您可以在清单5的“就地翻译实现”一节中找到一些示例,该节自动化在Eclipse JDT中嵌入特定领域的语言,以及GitHub项目中的多个示例。
#2
21
Eclipse does do this automatically. Right-click the file, choose "Source", then "Externalize strings"
Eclipse会自动执行此操作。右键单击文件,选择“Source”,然后“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.
这并不是您所要求的那样(字符串在常量中)。java文件作为字符串)但是使用的方法确实非常强大。它将它们移动到属性文件中,该文件可以根据您的语言环境动态加载。如您所建议的那样,将它们放在一个单独的Java源文件中,意味着您将同时在应用程序中拥有所有语言,或者您将根据语言环境交付不同的应用程序。
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 :-).
我们用它来做我们的应用,即使是最基本的东西也要用英语和日语——我们更复杂的应用程序要用12种语言——我们绝不是一个小的软件开发公司:-)
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.
如果您想要在Java文件中使用它们,尽管已经提到了一些缺点,但是编写一个程序将属性文件转换成Java源文件比尝试从*形式的Java源代码中提取字符串要容易得多。
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.
然后,您需要做的就是修改Accessor类以使用内置的字符串(在单独的类中),而不是在运行时加载它们。
#3
1
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.
有一些很好的理由可以解释为什么你不想这样做。除了任何生成的文件(我不知道eclipse功能)不会区分字符串设置,例如,作为构造函数参数的测试类,你真正想要的东西作为常量,更大的问题是,你所有的公共静态决赛会编译成类,如果您想要改变类行为,您不仅需要改变保存常量的类,还需要改变引用常量的所有东西。
#4
1
I fully acknowledge what Pax Diablo said. We're using that function too.
我完全承认Pax Diablo说过的话。我们也用这个函数。
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.
当应用到类文件时,函数“Externalize strings”将创建两个文件,一个类消息。类和属性文件message .properties。然后,它将字符串文字的所有直接用法重定向到对消息的调用。获取(字符串键)并使用“Ext. String”向导中为字符串输入的键。
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.
另一个优点是:将字符串文本提取到属性文件的方法使您可以*地使用任何您想要的语言翻译源语言,而无需修改任何代码。属性文件加载程序通过使用具有语言iso代码的相应文件自动加载目标语言文件。因此,您不必担心代码运行的平台,它将自动选择适当的语言(几乎)。请参阅ResourceBundle的文档,了解它是如何工作的。
#5
0
You may want to check out the Jackpot source transformation engine in NetBeans which would allow you to script your source transformations.
您可能想要在NetBeans中检查彩票源转换引擎,它允许您编写源转换脚本。