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

Bug 351426

Summary: [1.7][code assist] CompletionContext.getExpectedTypesKeys() returns wrong type
Product: [Eclipse Project] JDT Reporter: Dani Megert <daniel_megert>
Component: CoreAssignee: Ayushman Jain <amj87.iitr>
Status: VERIFIED FIXED QA Contact:
Severity: normal    
Priority: P1 CC: Olivier_Thomann, satyam.kandula
Version: 3.7   
Target Milestone: 3.7.1   
Hardware: All   
OS: All   
Whiteboard:
Attachments:
Description Flags
proposed fix
none
proposed fix v2.0 + regression tests
daniel_megert: review+
released patch (fix v2.0 + more tests) none

Description Dani Megert CLA 2011-07-07 07:38:28 EDT
BETA_JAVA7.

When invoking content assist at |
    List<String> a3= new ArrayList|
and I call CompletionContext.getExpectedTypesKeys() I get the correct result:
[[L, j, a, v, a, /, u, t, i, l, /, L, i, s, t, <, L, j, a, v, a, /, l, a, n, g, /, S, t, r, i, n, g, ;, >, ;]]


But when I do the same here:
    List<String> a3= new ArrayList<|
I get:
[[L, j, a, v, a, /, l, a, n, g, /, O, b, j, e, c, t, ;]]


This is something we need to fix for 3.7.1 as otherwise people can't easily insert the inferred type after we inserted the diamond.
Comment 1 Ayushman Jain CLA 2011-07-14 14:01:36 EDT
Created attachment 199691 [details]
proposed fix

This patch makes sure the expected type is correct for variable declarations or return statements. The LHS type parameters are used. 

Note that diamond inference is not possible here because we find the completion node before even parsing the complete allocation expression and the arguments in it. So taking the type parameters from LHS or from expected return type is the best we can do.

Dani, let me know if this patch works for you.Thanks!
Comment 2 Olivier Thomann CLA 2011-07-14 14:05:57 EDT
Did you comment checkCancel() on purpose ?
Comment 3 Ayushman Jain CLA 2011-07-14 14:08:37 EDT
(In reply to comment #2)
> Did you comment checkCancel() on purpose ?

That was for testing. I still have to add the tests, so I'll uncomment it out in the final patch
Comment 4 Olivier Thomann CLA 2011-07-14 14:13:42 EDT
(In reply to comment #3)
> That was for testing. I still have to add the tests, so I'll uncomment it out
> in the final patch
ok. just checking :-)
Comment 5 Ayushman Jain CLA 2011-07-18 07:29:36 EDT
Created attachment 199819 [details]
proposed fix v2.0 + regression tests

Dani, would you expect a signature - [L, j, a, v, a, /, l, a, n, g, /, S, t, r, i, n, g, ;] for   List<String> a3= new ArrayList<| ,
or a signature - [[L, j, a, v, a, /, u, t, i, l, /, L, i, s, t, <, L, j, a, v, a, /, l, a, n, g, /, S, t, r, i, n, g, ;, >, ;]] ?

This patch gives the latter expected type signature, while the above patch gives the former signature. Looking at the UI code, I realised this is the way UI expects proposals in this case.

(Patch contains some commented out code which i'll remove once Dani approves that this is the intended behaviour).
Comment 6 Dani Megert CLA 2011-07-26 06:27:10 EDT
(In reply to comment #5)
> Created attachment 199819 [details] [diff]
> proposed fix v2.0 + regression tests
> 
> Dani, would you expect a signature - [L, j, a, v, a, /, l, a, n, g, /, S, t, r,
> i, n, g, ;] for   List<String> a3= new ArrayList<| ,
> or a signature - [[L, j, a, v, a, /, u, t, i, l, /, L, i, s, t, <, L, j, a, v,
> a, /, l, a, n, g, /, S, t, r, i, n, g, ;, >, ;]] ?

The latter.

The patch works for me. Sorry for the late response.
Comment 7 Ayushman Jain CLA 2011-07-26 07:08:42 EDT
(In reply to comment #6)
> The latter.
> 
> The patch works for me. Sorry for the late response.

Yeah i guessed this would be the case looking at the UI code. However I had a doubt because the UI code currently does not handle the following:

X<String, String> x = new X<String, | >

This will be easier for the UI to handle if we give [L, j, a, v, a, /, l, a, n, g, /, S, t, r, i, n, g, ;] as the signature. With the patch in comment 5 however,the UI will have to parse the signature [[L, j, a, v, a, /, u, t, i, l, /, L, i, s, t, <, L, j, a, v, a, /, l, a, n, g, /, S, t, r, i, n, g, ;, L, j, a, v, a, /, l, a, n, g, /, S, t, r, i, n, g,; >, ;]]

to identify the second type arg as String.
Let me know what you think.
Comment 8 Dani Megert CLA 2011-07-26 09:00:21 EDT
(In reply to comment #7)
> (In reply to comment #6)
> > The latter.
> > 
> > The patch works for me. Sorry for the late response.
> 
> Yeah i guessed this would be the case looking at the UI code. However I had a
> doubt because the UI code currently does not handle the following:
> 
> X<String, String> x = new X<String, | >
> 
> This will be easier for the UI to handle if we give [L, j, a, v, a, /, l, a, n,
> g, /, S, t, r, i, n, g, ;] as the signature. With the patch in comment 5
> however,the UI will have to parse the signature [[L, j, a, v, a, /, u, t, i, l,
> /, L, i, s, t, <, L, j, a, v, a, /, l, a, n, g, /, S, t, r, i, n, g, ;, L, j,
> a, v, a, /, l, a, n, g, /, S, t, r, i, n, g,; >, ;]]
> 
> to identify the second type arg as String.
> Let me know what you think.

Yes, for that case but it would not easily allow to insert "String,String" in this case:
    X<String, String> x = new X<|>
which is more common.

NOTE: In order to test it you need the latest JDT UI code from HEAD.
Comment 9 Ayushman Jain CLA 2011-07-27 06:07:21 EDT
Created attachment 200436 [details]
released patch (fix v2.0 + more tests)

Added more tests and polished the fix.
Comment 10 Ayushman Jain CLA 2011-07-27 06:15:17 EDT
Released in BETA_JAVA7 branch
Comment 11 Satyam Kandula CLA 2011-08-02 08:59:58 EDT
Verified for 3.8M1 using build I20110729-1200