Bug 444870

Summary: [quick assist] Ctrl+2, L or Ctrl+2, F should also import the required type to avoid a syntax error
Product: [Eclipse Project] JDT Reporter: Lars Vogel <Lars.Vogel>
Component: UIAssignee: JDT-UI-Inbox <jdt-ui-inbox>
Status: ASSIGNED --- QA Contact:
Severity: enhancement    
Priority: P3 CC: daniel_megert, Lars.Vogel, markus.kell.r, noopur_gupta, simon.scholz, stephan.herrmann
Version: 4.5   
Target Milestone: ---   
Hardware: All   
OS: All   
Whiteboard:
Attachments:
Description Flags
Animated Gif none

Description Lars Vogel CLA 2014-09-23 14:47:00 EDT
I frequently use the Ctrl+2, L and end up with an syntax error. For example in Android:

findViewByID(R.id.text1) 

Ctrl+2, L create the following:

View view = findViewByID(R.id.text1);

But I also need to trigger the organize import (of a save action) to also add the import statement. 

I think JDT should also add the import statement. If you agree and don't have the capacity to implement this, let me know, we (Simon or I) can look into this.
Comment 1 Noopur Gupta CLA 2014-09-24 01:30:46 EDT
I cannot reproduce the issue. Example:

import java.util.ArrayList;

class Test extends BugExample {
	{
		(new ArrayList<String>()).stream();
	}
}

Ctrl+2, L also adds the "import java.util.stream.Stream;" by default.

Please provide a stand-alone example where it does not work.
Comment 2 Noopur Gupta CLA 2014-10-01 02:23:30 EDT
Closing as WORKSFORME. Please re-open with a stand-alone example to reproduce the issue.
Comment 3 Lars Vogel CLA 2014-10-01 15:42:27 EDT
Noopur, I have a hard time reproducing this outside of an Android project and I guess you don't want to install the ADT tools.

Could you tell me the class which is triggered by this shortcut? I can try to debug it, and see why it does not work in the Android example.
Comment 4 Markus Keller CLA 2014-10-03 11:20:58 EDT
To find out what happens when you press a shortcut, set a breakpoint in 
KeyBindingDispatcher#executeCommand(..) and step through. I know this became a bit painful with E4, but maybe you could work on that?

QuickAssistProcessor#getAssists(..) and ExtractTempRefactoring#checkInitialConditions(..) are potential entry points.
Comment 5 Lars Vogel CLA 2015-10-27 09:52:48 EDT
Created attachment 257542 [details]
Animated Gif

Here is a simple code example in a standard Java program:

1.) Create the following class
public class Main {

	public static void main(String[] args) {
	}

}

2.) Copy and paste new ArrayList<>() into it (do not type it)
3.) Use Ctrl+2, l to assign it to a local variable

-> Import is not added. See animated Gif as example
Comment 6 Lars Vogel CLA 2015-10-27 09:53:10 EDT
See comment 5
Comment 7 Dani Megert CLA 2015-10-28 04:44:39 EDT
*** Bug 479030 has been marked as a duplicate of this bug. ***
Comment 8 Stephan Herrmann CLA 2015-10-28 19:20:58 EDT
title and comment 0 should probably say "resolve error" instead of "syntax error", right? I would be weird if a quick assist really creates a syntax error.