Eclipse plugin: Introduce Static Imports

The Static Import guide from Sun states:

So when should you use static import? Very sparingly! Only use it when you’d otherwise be tempted to declare local copies of constants, or to abuse inheritance (the Constant Interface Antipattern). In other words, use it when you require frequent access to static members from one or two classes. If you overuse the static import feature, it can make your program unreadable and unmaintainable, polluting its namespace with all the static members you import. Readers of your code (including you, a few months after you wrote it) will not know which class a static member comes from. Importing all of the static members from a class can be particularly harmful to readability; if you need only one or two members, import them individually. Used appropriately, static import can make your program more readable, by removing the boilerplate of repetition of class names.

Unfortunately, Eclipse didn’t provide a way to change all calls of static method/field to use static import. You can do ‘Add Import’ and Eclipse will create this static import for you but the other instances where this same method/field used will not be changed.

For example, if I have a class that contains static method like this:

package bug;

public class A {

	public static void bbb() {

	}
}

And other class that calls the static method:

package bug;

public class B {
	public B() {
		A.bbb();
		A.bbb();
	}
}

And I want to introduce A.bbb as static import. I can do Add Import (Ctrl+Shift+M) and what I will get is;

package bug;

import static bug.A.bbb;

public class B {
	public B() {
		bbb();
		A.bbb();
	}
}

Hmmm… not really nice, that says that I have to change all occurrence of A.bbb() in the same class (which can be many, after all the previously mentioned guide asked us only to use this import static when there are a lot of repetition of class name).

So I decide to do some hacks and create my first JDT extension plugins (yay!) that exactly do this. You can grab the plugin here (I might release the source later on GitHub):

firdau.si.introduceStaticImport_1.0.0.201001111558

Copy this plugin and drop it in the dropins folder. I must mention also that I use Java 6 for the development. I think Java 5 will also work but I make no guarantee.

With this plugin installed, you will get a new entry if you do right click in Java Editor.

And if you run it, the result is:

package bug;

import static bug.A.bbb;

public class B {
	public B() {
		bbb();
		bbb();
	}
}

Take it easy with the plugin, after all this is my first JDT extension plugin. I have tested it against the code of the plugin itself, but I can’t be responsible for anything that may happen. You may report the problem though…

Note also that the plugin currently is working only for static methods and I do think this should be provided by Eclipse in their default product. Unfortunately, I don’t think my code is good enough for a patch for such.

UPDATE:

The new code can also work with static field.

Leave a Reply

Your email address will not be published. Required fields are marked *