从java项目中提取所有字符串 [英] Extract all string from a java project

查看:164
本文介绍了从java项目中提取所有字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当大量的源文件,我需要解析和提取所有的字符串文字,并将它们放在一个文件中,播放旧的java常量。

例如:

 标签l =新标签(猫); 

将成为:

  Label l = new Label(Constants.CAT); 

而在 Constants.java

  public final static String CAT =Cat; 

我不希望在属性文本文件中外部化字符串。 >

一个原因是一致性和代码可读性。

另一个原因是我们的客户端代码使用 GWT 支持Java属性文本文件机制。



我可以编写某种解析器(可能使用ant替换任务)?

但是我想知道一个 IDE 已经自动完成这种事情。

解决方案

完成Peter Kelley答案是,您可以考虑使用eclipse IDE AST 解决方案。



然后您可以编写一个解析源代码的AST程序,



这个 eclipse corner文章,还有更多的细节在 eclipse帮助

您可以在自动嵌入Eclipse JDT 中的域特定语言的实现原位翻译一节的清单5以及 GitHub项目中的多个示例


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.

解决方案

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

这篇关于从java项目中提取所有字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆