简体   繁体   English

Eclipse组织导入静态

[英]Eclipse organize import for static

Any one know, is there any way to organize static import in Eclipse ? 任何人都知道,有没有办法在Eclipse组织static import Like shift + ctrl + o organize import, we have any keyboard shortcut for static import shift + ctrl + o组织导入一样,我们有任何用于static import键盘快捷键

import static java.lang.Math.PI; // ==> any key board shortcut?
import java.math.BigDecimal;

EDIT 编辑

My Case: 我的情况:

In one of my program I require to initialize 30 fields with Math.PI , I have initialized like: 在我的一个程序中,我需要使用Math.PI初始化30个字段,我已初始化为:

private double var1=PI;
private double var2=PI;
private double var3=PI;
// other lines skipped
private double var30=PI;

To do this, I used Notepad++ to edit multiple lines at once using Shift + Alt + navigation arrows , then inserted code in eclipse . 为此,我使用Notepad++使用Shift + Alt + 导航箭头一次编辑多行,然后在eclipse插入代码。
Now, I want to do static import for the java.lang.Math.PI field (ie. import static java.lang.Math.PI; ) with keyboard shortcut that will fix imports for these 30 fields with single key stroke in Eclipse. 现在,我想使用键盘快捷键对java.lang.Math.PI字段(即import static java.lang.Math.PI; )进行static import ,该键盘快捷键将修复Eclipse中单键击中这30个字段的导入。

With Content Assist option I have to select Add static import for Math.PI 30 times, in my case. 使用Content Assist选项,我必须选择Add static import for Math.PI 30次。

如果您转到Window > Preferences > Java > Editor > Content Assist > Favorites它会为您提供定义org.junit.AssertWindow > Preferences > Java > Editor > Content Assist > Favorites的选项。

Have you ever tried the option: java->editor->save actions->organize imports. 你有没有尝试过这个选项:java-> editor-> save actions-> organiz imports。 It may be help. 这可能会有所帮助。

this is a screen capture 这是一个截屏

If you have several constants the implements-a-nonabstract-interface trick might do. 如果你有几个常量,那么implements-a-nonabstract-interface技巧可能会做。

public interface MathEnviron {
    static final double PI = Math.PI;
    ...
    /** @since: 1.8 */
    default double sin (double x) {
         return Math.sin(x);
    }
}

public class SomeClass implements MathEnviron {

    ... dietAfter(sin(apple*PI));
}

I just discovered that Ctrl+Shift+M (Source > Add Import) can not only be used to add missing imports. 我刚刚发现Ctrl + Shift + M (源>添加导入)不仅可以用来添加丢失的导入。 It can also help with static imports. 它还可以帮助静态导入。 Executed on a reference to a qualified member (read Class.member), the refactoring will add a static import for the defining class and remove the class-dot expression. 在对合格成员的引用(读取Class.member)上执行时,重构将为定义类添加静态导入并删除class-dot表达式。

For example, if you have 例如,如果你有

import java.lang.System;
class Example {
void someMethod() {
System.currentTimeMillis();
 }
}

Place the cursor on currentTimeMillis() and press Ctrl+Shift+M . 将光标放在currentTimeMillis() ,然后按Ctrl + Shift + M. This will transform the code to 这会将代码转换为

import static java.lang.System.currentTimeMillis;
class Example {
void someMethod() {
currentTimeMillis();
 }
}

This feature has probably been here for a while and is documented and was announced in a New & Noteworthy. 这个功能可能已经存在了一段时间,并且已经记录并在新的和值得注意的事件中公布。 I only just discovered it the other day and found that it highly improves working with static imports. 我前几天才发现它,发现它大大改善了静态导入的工作。 Maybe you find it useful too 也许你觉得它也很有用

another great example with good explanation provided 提供了很好解释的另一个很好的例子

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

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