java中类定义的正则表达式
问题如下:
Java 中有一个庞大的代码库(数十个包中的数百个文件),我们需要能够在每个类定义中添加和删除关键字 strictfp。 我计划使用 sed 或 awk 来执行此替换。 但是,我想避免评论或其他地方的“类”一词被替换。 有人能想出解决办法吗?
The problem is as follows:
There is a massive codebase in Java (hundreds of files in tens of packages) where we need the ability to add and remove the keyword strictfp in each class definition. I am planning to use either sed or awk to perform this substitution. However, I would like to avoid the word "class" in comments or elsewhere from being replaced. Can anyone think of a solution for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
由于您想要在 strictfp 和 no strictfp 之间来回切换,因此在构建过程中修改 .class 文件可能是更好的主意。 然后您可以将此选择作为构建选项并进行切换,而无需不断更改源文件。
Since you want to switch back and forth between strictfp and no strictfp, it may be a better idea to modify the .class files as part of the build process. Then you can make this choice a build option and switch it without constantly changing source files.
对于这样的任务,我首先建议编写一个小程序来验证目录树中的所有
*.class
文件是否确实具有(或不具有)正确的strictfp
环境。 然后,无论您选择做什么来解决源代码修改问题,您都可以在编译器之后运行检查器以确保您做了正确的事情。这实际上只是 TDD 应用于您的特定问题。
For such a task, I would first recommend writing a small program that verifies all the
*.class
files in a directory tree actually have (or do not have) the correctstrictfp
setting. Then, whatever you choose to do to solve the source code modification problem, you can run the checker after the compiler to make sure that you've done the right thing.This is actually just TDD applied to your particular problem.
如果没有 java 解析器,就无法确保 100% 正确。 但是,以下内容可能已经足够接近了:
我认为我的转义是正确的,但我是在 Windows 机器上编写此内容并且尚未对其进行测试。
如果您有“public/private/protected”后跟“class”的注释,上面的内容将会出错。
No way to be certain to get this 100% right without a java parser. But, the following may be close enough:
I think I got the escapes right, but I'm writing this from a windows machine and haven't tested it.
The above will trip up if you have comments with 'public/private/protected' followed by 'class'.
您的代码格式有多标准? 例如,您可以在以
{
结尾的行中查找“class”,或者在下一行以{
开头的行中查找“class”吗?How standard is the formatting of your code? For instance, could you look for 'class' in a line that ends with
{
or where the next line begins with{
or some such?星蓝:谢谢! 通过查找 BCEL,我发现了 JMangler,它非常完美,因为我可以修改现有的 .class 文件以添加 strictfp 修饰符。
http://javalab.cs.uni-bonn.de/研究/jmangler/tutorial/using.html
Starblue: Thanks! From looking up BCEL, I found JMangler, which is perfect, since I can modify existing .class files to add the strictfp modifier.
http://javalab.cs.uni-bonn.de/research/jmangler/tutorial/using.html
我可以想到两种可能性:
这些都不是很令人满意。 第一个实际上很难写,第二个很难正确。
我的建议是找到一个 RE 并将其释放到源代码的本地副本上,然后检查所有更改。 无论错误地进行了哪些更改,请调整 RE 并重试。
然后引入编码标准,逐步将所有不符合的定义改为正确的。
I can think of two possibilities:
Neither of these are very satisfactory. The first is actually quite hard to write, the second hard to get right.
My advice would be to just find an RE and turn it loose on a local copy of the source code, then examine all the changes. Whatever changes that were made in error, tweak the RE and try again.
Then introduce coding standards and gradually change all the non-conforming definitions to be correct.
作为一个完整的包罗万象,不,没有正则表达式。 您本质上需要编写一个 Java 解释器(或至少是一个解析器),以便解释类的任何理论构造。 话虽这么说,如果单词类是行上的第一个非空白位,或者它前面有一个访问修饰符,该访问修饰符是行上的第一个非空白位,那么您确实应该是安全的。
As a complete catch-all, no, there's no RegEx for that. You'd essentially need to write a Java interpreter (or at least a parser) in order to account for any theoretical construction of a class. That being said, you really should be safe if the word class is the first non-whitespace bit on the line, or if it's preceded by an access modifier that is the first non-whitespace bit on the line.
只是为了让人们知道,我最终决定使用 Javassist 而不是 BCEL,因为 Javassist 似乎使用起来更简单,并且完全适合我的目的。 http://csg.is.titech.ac.jp/~chiba/javassist< /a> .
如果有人好奇,代码会是这样的:
Just to let people know, I decided to go with Javassist in the end instead of BCEL, since Javassist seems to be a lot simpler to use and is perfectly adequate for my purposes. http://csg.is.titech.ac.jp/~chiba/javassist .
In case anyone is curious, the code would go something like this: