开发者

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

开发者 https://www.devze.com 2023-02-03 12:52 出处:网络
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.

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


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


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


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;


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

0

精彩评论

暂无评论...
验证码 换一张
取 消