Eclipse organizes imports for static

Does anyone know if there is a way to organize static import

in Eclipse

? How shift+ ctrl+ to oorganize imports, we have any keyboard shortcut forstatic import

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

      

EDIT

My case:

In one of my programs I need to initialize 30 fields with Math.PI

, I initialized like this:

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 the same time with shift+ Alt+ navigation arrows, then pasted the code into Eclipse

.
Now I want to do static import

for a field java.lang.Math.PI

(i.e. import static java.lang.Math.PI;

) with a keyboard shortcut that will fix imports for those 30 fields with one key stroke in Eclipse.

With the option, Content Assist

I need to select Add static import for Math.PI

30 times, in my case.

+3


source to share


4 answers


If you go to Window > Preferences > Java > Editor > Content Assist > Favorites

it will give you the ability to define things likeorg.junit.Assert



0


source


Have you ever tried the option: java-> editor-> save actions-> arrange imports. This might be of help.



this is screen capture

0


source


If you have multiple constants, the implements-a-nonabstract-interface trick can be done.

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));
}

      

0


source


I just discovered that Ctrl+Shift+M(Source> Add Imports) can be used for more than just adding missing imports. It can also help with static imports. By reference to a qualified member (read Class.member), the refactoring will add static imports for the defining class and remove the dot class expression.

For example, if you have

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

      

Place the cursor on currentTimeMillis()

and click Ctrl+Shift+M. This will convert 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 announced in New and Notable. I just discovered it the other day and found that it greatly improves static import performance. You may find it helpful.

another great example with a good explanation

0


source







All Articles