Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 155894

Summary: Quickfix "Extract to local variable" for generics replicates the error instead of fixing it
Product: [Eclipse Project] JDT Reporter: Wolfgang Knauf <wolfgang.knauf>
Component: UIAssignee: JDT-UI-Inbox <jdt-ui-inbox>
Status: CLOSED WONTFIX QA Contact:
Severity: minor    
Priority: P3    
Version: 3.2   
Target Milestone: ---   
Hardware: PC   
OS: All   
Whiteboard:
Attachments:
Description Flags
Screenshot of the quickfix suggestion. none

Description Wolfgang Knauf CLA 2006-08-31 15:54:50 EDT
This sample comes from EJB3 programming where you can query for entity beans of a certain type this way:

javax.persistence.EntityManager entityManager = ...;
//Create a query ("select * from MyBean")
javax.persistence.Query query = entityManager.createQuery("from MyBean");
//"getResultList" returns a list with only items of "MyBean" !
java.util.List<MyBean> listMyBean = query.getResultList();

Compiler warning:
Severity and Description: "Type safety: The expression of type List needs unchecked conversion to conform to List<MyBean>"
Id = "1071"

The code above works fine, but I think the warning is correct. 

Quickinfo suggests "Extract to local variable" (see attached screenshot)

I created a small sample. 
"getArrayList()" is a method with a non-generic return value, same as "query.getResultList()". In "getArrayListGeneric()" I assign the return value of "getArrayList()" to a generic "ArrayList<Integer>". For this line the quickfix option is available.

  private ArrayList<Integer> getArrayListGeneric()
  {
    ArrayList<Integer> list = this.getArrayList();
    
    return list;
  }
  
  private ArrayList getArrayList()
  {
    return new ArrayList<Integer>();
  }

If I use "Extract to local variable" this comes out:
private ArrayList<Integer> getArrayListGeneric()
  {
    ArrayList arrayList = this.getArrayList();
    ArrayList<Integer> list = arrayList;
    
    return list;
  }

The newly inserted line contains the same warning ;-).
This can be repeated one more time with the some result
 private ArrayList<Integer> getArrayListGeneric()
  {
    ArrayList arrayList = this.getArrayList();
    ArrayList arrayList2 = arrayList;
    ArrayList<Integer> list = arrayList2;
    
    return list;
  }

and so on...
Comment 1 Wolfgang Knauf CLA 2006-08-31 15:56:08 EDT
Created attachment 49209 [details]
Screenshot of the quickfix suggestion.
Comment 2 Olivier Thomann CLA 2006-08-31 16:45:18 EDT
Moving to JDT/UI
Comment 3 Martin Aeschlimann CLA 2006-08-31 18:08:38 EDT
Note, "Extract to local variable" is a quick assist, not a fix. Just tries to help you restructuring the code.
The fix for the scenario is 'Add type parameters to 'ArrayList''.
Comment 4 Martin Aeschlimann CLA 2006-09-05 12:32:02 EDT
setting WORKSFORME
Comment 5 Wolfgang Knauf CLA 2006-09-05 16:37:04 EDT
Hi Martin,

the problem in this situation is that the "Quickfix" suggestion does not help me out of this particular situation, so it should not be displayed. As you wrote in comment #3 the correct fix for my own sample might be "Add type parameters to 'ArrayList'" (not applicable to the "javax.persistence.Query.getResultList" sample anyway).

Is this possible not to show the inappropriate quickfix ?

Thanks

Wolfgang
Comment 6 Martin Aeschlimann CLA 2006-09-06 05:48:35 EDT
We have two modes: If you make a real selection (select the full word), we also show quick assist if they match the given range. If you just have a cursor, we don't show the quick assists, but just the quick fixes for the problem.

The reason is that sometimes you really want to peform a quick assist like 'Assign to local' even there is an error at that location. We need a way to allow this.

No changes planed at the moment.
Comment 7 Wolfgang Knauf CLA 2006-09-06 05:57:10 EDT
Closing as it seems not possible to detect the invalid suggestion.
Comment 8 Eclipse Webmaster CLA 2009-08-30 02:42:09 EDT
As of now 'LATER' and 'REMIND' resolutions are no longer supported.
Please reopen this bug if it is still valid for you.