eclipse 一种为现有代码中的常量创建静态导入的简单方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/824011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 13:41:18  来源:igfitidea点击:

A simple way to create static imports for constants in existing code?

javaeclipse

提问by Thorbj?rn Ravn Andersen

If I have a constant BAR in Foo, which I will use in a class C I'll have to write

如果我在 Foo 中有一个常量 BAR,我将在类 C 中使用它,我将不得不编写

Object o = Foo.BAR + "...";

which I can use Ctrl-Shift-M in Eclipse (with the cursor on BAR) to create a static import for like:

我可以在 Eclipse 中使用 Ctrl-Shift-M(光标在 BAR 上)来创建静态导入,例如:

import static Foo.BAR;

Object o = BAR + "....";

I am current updating legacy code with literaly thousands of these I'd like to convert to static imports. Ctrl-Shift-O / Organize imports does not do it. Is there a trick I've missed?

我目前正在更新旧代码,其中有数千个我想转换为静态导入。Ctrl-Shift-O/Organize 导入不会这样做。有没有我错过的技巧?



EDIT: Actually, what I would prefer is a way to tell Eclipse that I want to have Ctrl-Shift-M do its magic on ALL instances in this particular class instead of just the single instance I have the cursor placed at. (It is legacy code so this actually IMPROVES readability :) )

编辑:实际上,我更喜欢的是告诉 Eclipse 我想让 Ctrl-Shift-M 在这个特定类中的所有实例上执行它的魔法,而不仅仅是我放置光标的单个实例。(这是遗留代码,所以这实际上提高了可读性:))



EDIT: I've found that IntelliJ suggest doing this.

编辑:我发现 IntelliJ 建议这样做。

采纳答案by cletus

One cautionary note: excessive use of static imports can actually make your code less readable (imho), particularly if the constant name doesn't clearly indicate the class or enum from which it belongs. You can also get constant names from different classes/enums that sound similar or even have the same name.

一个警告:过度使用静态导入实际上会降低代码的可读性(恕我直言),特别是如果常量名称没有清楚地表明它所属的类或枚举。您还可以从听起来相似甚至具有相同名称的不同类/枚举中获取常量名称。

So what you're forcing the reader to do is hope his IDE will tell him the class (via, say, a tool tip) or they have to navigate to it to find out what it is. Printouts and code snippets become that much less reeadable.

所以你强迫读者做的是希望他的 IDE 会告诉他这个类(通过,比如说,一个工具提示),或者他们必须导航到它来找出它是什么。打印输出和代码片段变得更难阅读。

Honestly I use static imports extremely rarely for these reasons.

老实说,由于这些原因,我很少使用静态导入。

回答by deterb

I don't know any other automated refactorings to do what you're looking for, but there are two key aspects to doing this - one is adding the imports and the other is removing the class name from the constants.

我不知道任何其他自动重构来做你正在寻找的东西,但有两个关键方面可以做到这一点 - 一个是添加导入,另一个是从常量中删除类名。

For adding the imports, I recommend pasting import static application.KeyHolder.*;after the package on every file which uses them.

为了添加导入,我建议import static application.KeyHolder.*;在使用它们的每个文件的包之后粘贴。

For removing the class name, in eclipse do a Java search for KeyHolder.*and search for a field. You can then do a find/replace for "KeyHolder." and replace with "". In order to prevent mismatches make sure the replaced item count is equal to the number of matches for that file. Also, make sure you search down and start after the imports.

为了删除类名,在 eclipse 中做一个 Java 搜索KeyHolder.*并搜索一个字段。然后,您可以查找/替换“KeyHolder”。并替换为“”。为了防止不匹配,请确保替换的项目计数等于该文件的匹配数。另外,请确保在导入后向下搜索并开始。

回答by Chii

Regular expression was invented to solve problems like these! =)

正则表达式就是为了解决这样的问题而发明的!=)

you will need to write a small script (or use the IDE regex search/replace). The only problem is that if there are lots of classes that needs to be statically imported, then its just as much work. Otherwise, if its just one class like FOO.BLAH, then you can use a regex like

您将需要编写一个小脚本(或使用 IDE 正则表达式搜索/替换)。唯一的问题是,如果有很多类需要静态导入,那么它的工作量也一样。否则,如果它只是一个像 FOO.BLAH 这样的类,那么你可以使用像这样的正则表达式

(\bFOO\.(\w+)\b)-> replace with group 2 ($2 or \2 or however your regex searcher does capture replaces).

(\bFOO\.(\w+)\b)-> 替换为第 2 组($2 或 \2 或者您的正则表达式搜索器确实捕获了替换)。

you can try it here : http://www.gskinner.com/RegExr/. Select the replace tab and type in $2

你可以在这里尝试:http: //www.gskinner.com/RegExr/。选择替换选项卡并输入 $2

A problem might arise if you have expressions like this though: FOO f = FOO.blah + "FOO.blah", so watch out for that.

但是FOO f = FOO.blah + "FOO.blah",如果您有这样的表达式,则可能会出现问题: ,因此请注意这一点。

As for the import statement at the top of the file, eclipse has an auto import feature where it does autoimport on format, and it works as long as the name of the import is unique enough. Or, if you cant use it because the name is too common, you can just use a perl script to do the regex instead, and prepend the static imports.

至于文件顶部的导入语句,eclipse 有一个自动导入功能,它对格式进行自动导入,只要导入的名称足够独特,它就可以工作。或者,如果由于名称太常见而无法使用它,则可以仅使用 perl 脚本来执行正则表达式,并在静态导入之前添加。

回答by starblue

Eclipse can do a global search and replace with regular expression over all files in the project.

Eclipse 可以对项目中的所有文件进行全局搜索并替换为正则表达式。

To add the static import at the beginning of all files you could replace the package declaration by itself plus the static import.

要在所有文件的开头添加静态导入,您可以单独替换包声明加上静态导入。

Then do a project wide organize imports, and you're all set.

然后做一个项目范围内的组织导入,你就准备好了。

Be careful, though, this could also make a big mess if you do it wrong.

但是要小心,如果你做错了,这也会造成很大的混乱。

回答by Peter Lawrey

Sounds like a nice idea, but after you have changed all the files, how much better will they be? Perhaps you should think about only changing the files which you are working on for other reasons already.

听起来是个好主意,但是在您更改了所有文件后,它们会变得更好吗?也许您应该考虑只更改您正在处理的文件,因为其他原因。

回答by jqno

If I were faced with such a challenge, I'd probably write a small program to do it for me. Ruby and Python (JRuby and Jython?) are pretty well suited for the task, although you could do it in Java too.

如果我面临这样的挑战,我可能会编写一个小程序来为我完成。Ruby 和 Python(JRuby 和 Jython?)非常适合该任务,尽管您也可以用 Java 来完成。

OK, so that'll probably take you at least the same amout of time as doing it by hand, but at least you'll be having fun :).

好的,所以这可能会花费您至少与手工操作相同的时间,但至少您会玩得开心:)。

回答by user121803

Well something which pops in as a solution. I would recommend using a simple groovy script. Basically the semantics are related to parsing the import statements and the files they refer to. Some basic steps could be like

好吧,作为解决方案出现的东西。我建议使用一个简单的 groovy 脚本。基本上语义与解析导入语句和它们引用的文件有关。一些基本步骤可能类似于

  1. Recurse each file inside directory.Check whether the file extension is .java
  2. Match any line which starts with import
  3. For each such line open the relevant source file(you already know the path from the import statement).
  4. Match the field/method declaration for a static modifier using reflection
  5. Add static after the import statement for matches found in step 5.
  1. 递归目录中的每个文件。检查文件扩展名是否为.java
  2. 匹配任何以 import 开头的行
  3. 对于每个这样的行,打开相关的源文件(您已经知道导入语句的路径)。
  4. 使用反射匹配静态修饰符的字段/方法声明
  5. 在第 5 步中找到的匹配项的导入语句之后添加静态。

If i get some time i will try and post a small groovy script for doing the same.

如果我有时间,我会尝试发布一个小的 groovy 脚本来做同样的事情。

回答by rinjan

Make sure no conflicts with other constant classes

replace all the "Foo." with empty (" ")

use this line at the top. worked for me

确保与其他常量类没有冲突

替换所有“Foo”。空(“”)

在顶部使用此行。对我来说有效

import static Foo.*

导入静态 Foo.*

回答by Grzegorz Oledzki

I don't know if it would fit your needs, but you can always have these constants defined in an interface and have all your classes just implement this interface. Then using some smart RegExp as others suggest you could get rid of all Foo.occurences.

我不知道它是否符合您的需要,但您始终可以在接口中定义这些常量,并且您的所有类都只实现此接口。然后使用一些智能正则表达式,因为其他人建议您可以摆脱所有出现的情况Foo.

Example:

例子:

public interface Foo {
   static final String constant1 = "Some";
   static final String constant2 = "Value";
   //...
}

And then

进而

public class YourClass implements Foo {
   public YourClass() {
      System.out.println("Hello " + constant1);
   }
}