9

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

gatoatigrado
  • 16,580
  • 18
  • 81
  • 143

4 Answers4

9

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.

Community
  • 1
  • 1
Mark Peters
  • 80,126
  • 17
  • 159
  • 190
  • The templating idea is certainly useful to avoid inane suggestions by autocomplete, but not quite what I'm looking for, I want to skip autocomplete altogether for these short names. – gatoatigrado Jan 13 '11 at 16:57
5

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

Konstantin Komissarchik
  • 28,879
  • 6
  • 61
  • 61
  • Those preferences are useful, but won't help in this specific case. It won't affect what's added using a feature like Organize Imports. So if you don't use autocomplete in the first place (as in the OP's case) this won't change anything. It does, however, pretty much negate the need for my answer. – Mark Peters Jan 11 '11 at 21:44
2

You can switch to other IDE like IDEA, where it just work, or try workaround like this:

e.g. I often write code like

util.DebugOut.printDebug("my value", my_obj);

when point cursor on printDebug and ctrl-shift-m

import is added:

import static util.DebugOut.printDebug;
Maciek Kreft
  • 882
  • 9
  • 14
  • No luck. Could you go to Preferences > Keys and tell me what you have bound? Mine says "Add import", but this doesn't work for static functions, only classes. – gatoatigrado Apr 03 '11 at 05:41
-1

See Preferences->Java->Code Style->Organize Imports. There you can set up the static imports, so that the import statement is automatically added

Prakash G. R.
  • 4,746
  • 1
  • 24
  • 35