Java源重构7000个引用 [英] Java source refactoring of 7000 references

查看:101
本文介绍了Java源重构7000个引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要更改整个代码库中使用的方法的签名。

I need to change the signature of a method used all over the codebase.

具体来说,方法 void log(String)将需要两个额外的参数( Class c,String methodName ),这些参数需要由调用者提供,具体取决于调用它的方法。我不能简单地传递 null 或类似的东西。

Specifically, the method void log(String) will take two additional arguments (Class c, String methodName), which need to be provided by the caller, depending on the method where it is called. I can't simply pass null or similar.

为了了解范围,Eclipse找到了7000个引用对于那种方法,所以如果我改变它,整个项目就会失败。我需要花费数周时间手动修复它。

To give an idea of the scope, Eclipse found 7000 references to that method, so if I change it the whole project will go down. It will take weeks for me to fix it manually.

据我所知,Eclipse的Eclipse重构插件不能完成任务,但我真的想自动化它。
那么,我怎样才能完成工作?

As far as I can tell Eclipse's refactoring plugin of Eclipse is not up to the task, but I really want to automate it.
So, how can I get the job done?

推荐答案

太好了,我可以复制< a href =https://stackoverflow.com/questions/3838967/can-i-automatically-refactor-an-entire-java-project-and-rename-uppercase-method-p/3839193#3839193>之前的答案我的......我只需要编辑一点点:

Great, I can copy a previous answer of mine and I just need to edit a tiny little bit:

我想你需要做什么使用像 javaparser 这样的源代码解析器来执行此操作。

I think what you need to do is use a source code parser like javaparser to do this.

对于每个java源文件,将其解析为CompilationUnit,创建一个访问者,可能使用 ModifierVisitorAdapter 作为基类,并覆盖(至少) 访问(MethodCallExpr,arg) 。然后将更改的CompilationUnit写入新文件并在之后执行diff。

For every java source file, parse it to a CompilationUnit, create a Visitor, probably using ModifierVisitorAdapter as base class, and override (at least) visit(MethodCallExpr, arg). Then write the changed CompilationUnit to a new File and do a diff afterwards.

我建议不要更改原始源文件,但创建一个shadow文件树可能对我有好处想法(例如旧文件: src / main / java / com / mycompany / MyClass.java ,新文件 src / main / refactored / com / mycompany /MyClass.java ,这样就可以区分整个目录了。

I would advise against changing the original source file, but creating a shadow file tree may me a good idea (e.g. old file: src/main/java/com/mycompany/MyClass.java, new file src/main/refactored/com/mycompany/MyClass.java, that way you can diff the entire directories).

这篇关于Java源重构7000个引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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