Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to make Eclipse add static imports without autocomplete?

Eclipse can add unambiguous classes with an "on-save" action, but it will not resolve static functions. I don't always use autocomplete, and going back to trigger it is cumbersome.

e.g. I often write code like

printDebug("my value", my_obj);

and I want it to automatically add

import static util.DebugOut.printDebug;

NOTE: To reiterate, I'm not looking for (a) anything that requires ctrl+space, (b) automatic import of a class

like image 373
gatoatigrado Avatar asked Jan 11 '11 20:01

gatoatigrado


2 Answers

I know this doesn't exactly supply what you asked for, but I thought I'd post it anyway. I would suggest using an Eclipse template to do what you are trying to accomplish. For instance, if I were to want to use Math.sin() as if it were statically imported, I would use the following template:

${:importStatic(java.lang.Math.sin)}sin(${cursor});

For you, you want to follow these steps:

  • Go to Windows->Preferences
  • Under Java->Editor->Templates, click "New..."
  • Name the template something quick, like "printDebug" or "debug". Fill in the description
  • Specify the pattern below, and click OK, OK.
  • To use, type "debug" (or whatever the name was) followed by CTRL-Space.

Pattern:

${:importStatic(util.DebugOut.printDebug)}printDebug(${someString},${someObject});

Explanation: The importStatic variable will add the specified static import if it can be resolved and doesn't conflict with an existing import. someString and someObject prompt the user (you) to replace those values with real expressions and allow you to tab to the next one.

With this, you'll probably find it much faster than an automatic import in the end.

Edit

As for your "actual" question, you might find the following to be relevant. It's essentially a duplicate.

  • Eclipse Optimize Imports to Include Static Imports
like image 129
Mark Peters Avatar answered Sep 25 '22 06:09

Mark Peters


See Window->Preferences->Java->Editor->Content Assist->Favorites.

like image 32
Konstantin Komissarchik Avatar answered Sep 26 '22 06:09

Konstantin Komissarchik