java中类定义的正则表达式

发布于 2024-08-01 16:14:45 字数 149 浏览 7 评论 0原文

问题如下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

清风不识月 2024-08-08 16:14:45

由于您想要在 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.

焚却相思 2024-08-08 16:14:45

对于这样的任务,我首先建议编写一个小程序来验证目录树中的所有 *.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 correct strictfp 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.

梦里兽 2024-08-08 16:14:45

如果没有 java 解析器,就无法确保 100% 正确。 但是,以下内容可能已经足够接近了:

sed -i.bak 's/\\(public\\|private\\|protected\\)\\(.*\\)class/\\1 strictfp \\2 class/g' $file

我认为我的转义是正确的,但我是在 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:

sed -i.bak 's/\\(public\\|private\\|protected\\)\\(.*\\)class/\\1 strictfp \\2 class/g' $file

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

梦与时光遇 2024-08-08 16:14:45

您的代码格式有多标准? 例如,您可以在以 { 结尾的行中查找“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?

浅听莫相离 2024-08-08 16:14:45

星蓝:谢谢! 通过查找 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

篱下浅笙歌 2024-08-08 16:14:45

我可以想到两种可能性:

  • 使​​用解析器而不是正则表达式(RE 并不意味着一切)。 解析器将更好地理解语言语法。
  • 使您的代码库保持一致,这样您就不会拾取错误的“类”字符串。

这些都不是很令人满意。 第一个实际上很难写,第二个很难正确。

我的建议是找到一个 RE 并将其释放到源代码的本地副本上,然后检查所有更改。 无论错误地进行了哪些更改,请调整 RE 并重试。

然后引入编码标准,逐步将所有不符合的定义改为正确的。

I can think of two possibilities:

  • Use a parser rather than a regular expression (REs aren't meant for everything). A parser will far better understand the language syntax.
  • Make your code base consistent so that you will not pick up errant "class" strings.

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.

柠栀 2024-08-08 16:14:45

作为一个完整的包罗万象,不,没有正则表达式。 您本质上需要编写一个 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.

迷你仙 2024-08-08 16:14:45

只是为了让人们知道,我最终决定使用 Javassist 而不是 BCEL,因为 Javassist 似乎使用起来更简单,并且完全适合我的目的。 http://csg.is.titech.ac.jp/~chiba/javassist< /a> .
如果有人好奇,代码会是这样的:


    import javassist.*;
    ClassPool cp = new ClassPool(); 
    CtClass someClass = cp.get("SomeClass"); 
    someClass.setModifiers(Modifier.STRICT);

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:


    import javassist.*;
    ClassPool cp = new ClassPool(); 
    CtClass someClass = cp.get("SomeClass"); 
    someClass.setModifiers(Modifier.STRICT);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文