简体   繁体   English

Java中的自动化和可编程重构

[英]Automated and programmable refactoring in Java

I came across a task to internationalise the project which including the code (now 80% of the variable names are in Italian, which need to be changed to English). 我遇到了一个项目国际化的任务,其中包括代码(现在80%的变量名都是意大利语,需要更改为英语)。 The translation part is fine, but how to find a way to refactor the code is driving me nuts. 翻译部分很好,但是如何找到重构代码的方法让我疯狂。 Almost every IDE in the market has such refactoring function, but none of them work automatically or programmably. 几乎市场上的每个IDE都具有这样的重构功能,但它们都不能自动或可编程地工作。

My question is, do they just use the regex to scan through all the files in the project and then refactor them or follow some sophisticated rules? 我的问题是,他们只是使用正则表达式扫描项目中的所有文件,然后重构它们或遵循一些复杂的规则?

I have used a combination of JavaParser and another tool I forget the name of which allowed me to trace usages of all variables in classes from the compiled .class/.jar file. 我已经使用了JavaParser和另一个工具的组合我忘记了它的名称,它允许我从已编译的.class / .jar文件中跟踪类中所有变量的用法。

The JavaParser is certainly a tool that could do the job. JavaParser当然是一个可以完成这项工作的工具。 I will take a look tomorrow what the other componnet was. 我明天会看看其他组件是什么。 Could have been Class Dependency Analyser . 可能是Class Dependency Analyzer

OP appears to want to do mass renaming. OP似乎想要进行大规模重命名。

While not the tool you might expect, you can use our Java obfuscator to achieve this. 虽然不是您可能期望的工具,但您可以使用我们的Java混淆器来实现此目的。 This solution does mass renaming ignoring scopes, but I suspect that works fine for this. 这个解决方案大规模重命名忽略范围,但我怀疑它适用于此。

Many source obfuscators take identifiers and scramble them in a consistent fashion. 许多源混淆器采用标识符并以一致的方式对其进行加扰。 This means somewhere in the obfuscator, there is map from legacy identifiers to obfuscated identifiers, eg, "foo" -> "bar" meaning "replace 'foo' by 'bar'". 这意味着在混淆器中的某处,存在从遗留标识符到混淆的标识符的映射,例如,“foo” - >“bar”意味着“用'bar'替换'foo'”。

Our obfuscator reads such a map before it starts (and that's the key to a solution). 我们的混淆器在它开始之前读取这样的地图(这是解决方案的关键)。 This map was presumably generated by a previous obfuscation step; 该地图可能是由先前的混淆步骤产生的; reading and reusing such a map ensures that any incremental/additional obfuscation is done in the same way as as the original. 读取和重用这样的映射可确保以与原始映射相同的方式完成任何增量/附加混淆。

If what you do is run our obfuscator on a set of source files, you get a complete map of every identifier and what it was mapped to (randomly), in a text file looking like (as above): 如果您所做的是在一组源文件上运行我们的混淆器,您将获得一个完整的每个标识符的映射以及它映射到的内容(随机),在一个文本文件中(如上所示):

 foo -> i3718234
 fas -> h823478
 ...

If you then run the obfuscator again and feed it the same souces and this map file, it will (by design) map the identifiers the same way, producing the same result as the first time. 如果您再次运行混淆器并将其提供相同的源和此映射文件,它将(按设计)以相同的方式映射标识符,从而产生与第一次相同的结果。

To do OPs task, one could run our obfuscator on his code to get: 要执行OP任务,可以在他的代码上运行我们的混淆器来获得:

  italianID1 -> i23432
  italianID2 -> g232343
  ...

Using a text editor, he could modify this file to: 使用文本编辑器,他可以将此文件修改为:

  italianID1 -> englishID1
  italianID2 -> englishID2
  ...

Names he doesnt want touched are edited (obviously) to this form: 他不想触摸的名字被编辑(显然)到这个形式:

   foo -> foo
   bar -> bar

(A shorthand for this, is to simply leave off the "-> id" part. "foo" by itself is interpreted as "foo -> foo"). (简而言之,就是简单地省略“ - > id”部分。“foo”本身被解释为“foo - > foo”)。

Now running the obfuscator will rename all the italian IDs to englishIDs. 现在运行混淆器会将所有意大利语ID重命名为englishIDs。

Check out my bio for a link. 看看我的生物链接。 Its a commercial tool, but its probably cheaper than a day of OP's labor and I think it solves his problem, waaaaay easier than building his own refactoring tool. 它是一个商业工具,但它可能比一天OP的劳动力便宜,我认为它解决了他的问题,比建立自己的重构工具更容易。

A downside: I think he'll be forced to reformat his code. 缺点:我认为他将被迫重新格式化他的代码。 That's OK, the obfuscator includes a reformatter. 没关系,混淆器包括一个重新格式化器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM