Bug 197850 - [quick assist] Quick assist for converting to static import
Summary: [quick assist] Quick assist for converting to static import
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: UI (show other bugs)
Version: 3.3   Edit
Hardware: All All
: P3 normal with 5 votes (vote)
Target Milestone: 4.9 M2   Edit
Assignee: Fabian Pfaff CLA
QA Contact:
URL:
Whiteboard:
Keywords: noteworthy
: 478840 502847 507111 (view as bug list)
Depends on: 541630
Blocks: 536175 541586
  Show dependency tree
 
Reported: 2007-07-25 14:28 EDT by Daniel Aborg CLA
Modified: 2018-11-28 05:27 EST (History)
19 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Daniel Aborg CLA 2007-07-25 14:28:32 EDT
Build ID: I20070621-1340

Steps To Reproduce:

There should be a quick assist on constants and methods for converting them to a static import.

e.g.

Math.min(1, 2);

Ctrl-1 on "min" will have an entry 'Convert to static import' which will replace the code with:

import static java.lang.Math.min;

[...]

min(1, 2);

Where the static import might also become java.lang.Math.* based on the preferences for imports.


I'm constantly surprised that this isn't available when I try to use it, so I guess it's time to report it.
Comment 1 Martin Aeschlimann CLA 2007-07-26 04:01:04 EDT
This already exists: Add Import (CTRL + M) on the static method/field name.
But you're right, it would make sense as a quick assist.
Comment 2 Daniel Aborg CLA 2007-07-26 14:25:51 EDT
Oooh...  Add Import on static methods is delicious!  We all love you.  It's Ctrl-Shift-M by the way.
Comment 3 Nanda Firdausi CLA 2010-01-11 07:29:49 EST
It will be also better if we have other way to change all calls of certain static method/static field to use this import static.

For example:

package bug.a;

public class A {
	
	public static void bbb() {
		
	}
}

package bug;

import bug.a.A;

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

Currently if we do 'Add Import' in the first A.bbb() it will change the class B to:

package bug;

import static bug.a.A.bbb;
import bug.a.A;

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

But I want:

package bug;

import static bug.a.A.bbb;

public class B {
	public B() {
		bbb();
		bbb();
	}
}
Comment 4 Jilles van Gurp CLA 2012-09-01 04:46:12 EDT
I'm using eclipse Juno and this is still an open issue.

Also, I noticed the add import only fixes the import you are on and not any similar import. A quick fix option should fix all of them. So if you ctrl+1 on Math.sin(...), it should add the static import and then replace all Math.sin occurrences with sin.

I frankly don't see why you would want to fix only one occurrence and the current behavior seems wrong.
Comment 5 Noopur Gupta CLA 2015-10-05 04:46:30 EDT
*** Bug 478840 has been marked as a duplicate of this bug. ***
Comment 6 Eclipse Genie CLA 2015-11-03 14:13:25 EST
New Gerrit change created: https://git.eclipse.org/r/59590
Comment 7 Jens Reimann CLA 2015-11-03 14:14:22 EST
I just pushed a patch which allows to quick assist "import static" on fields and method invocations.
Comment 8 Jens Reimann CLA 2015-12-02 06:41:54 EST
I just wanted to check back and see if somebody did have a look at the patch? Or if I need to make any changes.
Comment 9 Noopur Gupta CLA 2016-01-08 09:41:12 EST
The patch provides a quick assist that performs the conversion to static import only at the invocation location. It does not convert all occurrences of that method/field in the compilation unit, as discussed in this bug. This can be added as another quick assist with suffix message "(replace all occurrences)".

Some comments on patch set 4:
- Do not include whitespace changes in the patch.
- Update copyright year in files.
- Combine the two methods added in QuickAssistProcessor to keep a single method for converting to static imports.
- Make sure import statement is not added for private members and when the static member is directly accessible from the invoking location. See bug 409594 for such scenarios.
- Refer AddImportsOperation#evaluateEdits method and make sure to cover all the applicable scenarios handled there.
Comment 10 Jens Reimann CLA 2016-01-25 10:56:50 EST
I just pushed patch set #6 which should solve those issues.

Sorry for the white spaces, it was pretty hard finding a code formatter setting that worked.

The copyright was changed to 2016. Now that I made some changes in 2016, that actually makes sense.

The two methods are merged. I am not sure this is more readable, but it is done.

I also did take over the "directly accessible" check from "AddImportsOperation#evaluateEdits".
Comment 11 Jens Reimann CLA 2016-01-26 11:06:46 EST
I just had a look at the Hudson jobs triggered by Gerrit [1], [2].

I have no idea what is wrong here. Two times, two different errors. The first one seems to have some issued with X11 and the second one fails at a total different location.

On my local machine the test cases run fine.

Is there a way to re-run those jobs?

[1] https://hudson.eclipse.org/platform/job/eclipse.jdt.ui-Gerrit/540/
[2] https://hudson.eclipse.org/platform/job/eclipse.jdt.ui-Gerrit/542/
Comment 12 Dani Megert CLA 2016-01-26 11:48:48 EST
(In reply to Jens Reimann from comment #11)
> Is there a way to re-run those jobs?
> 
> [1] https://hudson.eclipse.org/platform/job/eclipse.jdt.ui-Gerrit/540/
> [2] https://hudson.eclipse.org/platform/job/eclipse.jdt.ui-Gerrit/542/

I've retriggered both.
Comment 13 Jens Reimann CLA 2016-01-27 05:26:06 EST
Ok, thanks. Patch #7 is Verified +1 now.
Comment 14 Noopur Gupta CLA 2016-02-29 08:29:21 EST
(In reply to Jens Reimann from comment #10)
> I just pushed patch set #6 which should solve those issues.

The latest patch set does not work in the following cases:

package p1;

public class S {
	{
		T.<String>doIt(""); // Invoke at: do|It
		System.out.println(T.str); // Invoke at S|ystem and T|.str - should not be available at Type.
	}	
}

class T {
	static String str;
	static <V> void doIt(V o) {}
}

Other comments:

- Change the name of the quick assist to: "Convert to static import" and update the associated constants in code.

- Add new test cases in AssistQuickFixTest.java.

(In reply to Noopur Gupta from comment #9)
> The patch provides a quick assist that performs the conversion to static
> import only at the invocation location. It does not convert all occurrences
> of that method/field in the compilation unit, as discussed in this bug. This
> can be added as another quick assist with suffix message "(replace all
> occurrences)".

As discussed in the bug, it would be useful if this quick assist converts all occurrences in the compilation unit at the same time. Please add another quick assist for that as mentioned in the above comment.
Comment 15 Robert Roth CLA 2016-07-02 02:40:35 EDT
@Jens Reimann: will you have time to update based on comment:14, or may I take over from where you left and update the patchset/create a new one based on it?
Comment 16 Jens Reimann CLA 2016-07-02 04:23:16 EDT
(In reply to Robert Roth from comment #15)
> @Jens Reimann: will you have time to update based on comment:14, or may I
> take over from where you left and update the patchset/create a new one based
> on it?

Not at the moment. I would be glad if you do so.
Comment 17 Noopur Gupta CLA 2016-09-30 07:15:43 EDT
*** Bug 502847 has been marked as a duplicate of this bug. ***
Comment 18 Noopur Gupta CLA 2016-11-07 09:21:48 EST
*** Bug 507111 has been marked as a duplicate of this bug. ***
Comment 19 Fabian Pfaff CLA 2017-12-12 12:00:09 EST
I've submitted a new changeset because I can't work on the existing changeset.
The issues that you had mentioned should be fixed and I've added a second quickfix that replaces all occurrences.
The change can be found here: https://git.eclipse.org/r/#/c/113254/
Comment 20 Eclipse Genie CLA 2017-12-12 14:39:05 EST
New Gerrit change created: https://git.eclipse.org/r/113254
Comment 21 Lars Vogel CLA 2018-06-22 05:23:32 EDT
Noopur, I removed your - as Sarika is working on the code review.
Comment 22 Sarika Sinha CLA 2018-06-25 05:43:23 EDT
Created Bug 536235 to handle adding of new quick assists to use the static imports also.
Comment 24 Sarika Sinha CLA 2018-06-25 23:52:35 EDT
Thanks Fabian!!
Comment 25 Lars Vogel CLA 2018-06-26 02:02:30 EDT
Fabian, please add to N&N for 4.9
Comment 26 Sarika Sinha CLA 2018-08-01 06:05:47 EDT
Eclipse SDK

Version: 4.9
Build id: I20180731-2000
Comment 27 Lars Vogel CLA 2018-08-14 11:10:18 EDT
(In reply to Lars Vogel from comment #25)
> Fabian, please add to N&N for 4.9

Ping
Comment 28 Jens Reimann CLA 2018-08-15 04:24:50 EDT
(In reply to Lars Vogel from comment #27)
> (In reply to Lars Vogel from comment #25)
> > Fabian, please add to N&N for 4.9
> 
> Ping

I already created a change in Gerrit for that. And it got merged. So it guess there is nothing to be done here.
Comment 29 Jens Reimann CLA 2018-08-15 04:25:47 EDT
(In reply to Jens Reimann from comment #28)
> (In reply to Lars Vogel from comment #27)
> > (In reply to Lars Vogel from comment #25)
> > > Fabian, please add to N&N for 4.9
> > 
> > Ping
> 
> I already created a change in Gerrit for that. And it got merged. So it
> guess there is nothing to be done here.

Please ignore my last comment. Wrong issue, similar subject.
Comment 30 Sarika Sinha CLA 2018-08-21 01:48:30 EDT
Ping!!!

@Fabian please add a N&N.
Comment 31 Eclipse Genie CLA 2018-08-22 13:12:42 EDT
New Gerrit change created: https://git.eclipse.org/r/127873