内联 eclipse 中的所有静态导入
我正在清理一些代码,偶然发现 com.example.StringHelper
包含 6 或 7 个 public static
方法(例如 concatStrings(String...)< /code>,但没有成员字段。有许多类对此类进行子类化,以便它们可以调用
concatStrings(str1, str2)
而无需在其前面添加类前缀,如下所示:StringHelper. concatStrings(str1, str2)
.
我不希望他们仅仅因为这个原因而子类化一个类,所以我在删除 < 后将以下静态导入粘贴到任何子类化文件的顶部。 code>extends StringHelper:
import static com.example.StringHelper.*;
Eclipse 将其简化为仅针对正在使用的方法的特定导入
问题:是否有一种简单的方法可以让 Eclipse “内联”这些静态导入?删除实际的静态导入并在每个调用前加上 StringHelper.
来代替
注意 ? 这是一个简化的人为示例,因此请不要抱怨为什么我们首先需要 StringHelper。
I was cleaning up some code and happened upon com.example.StringHelper
that contained 6 or 7 public static
methods (for example concatStrings(String...)
, but no member fields. There were a number of classes subclassing this class just so they could call concatStrings(str1, str2)
without prefixing it with the class like so: StringHelper.concatStrings(str1, str2)
.
I didn't want them subclassing a class just for that reason, so I broke a bunch off. I pasted the following static import into the top of any file subclassing it after removing the extends StringHelper
:
import static com.example.StringHelper.*;
Eclipse simplified this into specific imports for only the methods being used.
Question: Is there a simple way to have Eclipse "inline" these static imports? Can I get it to remove the actual static import and prefix every call with StringHelper.
instead?
Note
This is a simplified contrived example, so please don't complain about why we need a StringHelper in the first place.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这样就可以做到这一点:
This will do it:
不知道是否有自动方式,但我认为它可以以手动方式提供帮助。删除导入,然后单击有错误的每一行。按 ctrl-1 进行“快速修复”,然后选择为包名称添加前缀的快速修复,而不是添加导入。
Don't know if there's an automatic way, but I think it can help in a manual way. Delete the import, then click on each line with an error on it. Press ctrl-1 for 'quick fix' and choose the quick fix that prefixes the package name rather than add an import.
您可以在此处配置对它们进行分组所需的导入数量。
There you can configure how many imports will be required to group them.
首选项>爪哇>代码风格>组织导入:
“.* 所需的静态导入数量”将其设置为 1。
Preferences > Java > Code Style > Organize Imports:
"Number of static imports needed for .*" set this to 1.