Bug 287939 - [code assist] The instanceof and the auto cast feature should also work for an assignment
Summary: [code assist] The instanceof and the auto cast feature should also work for a...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.5   Edit
Hardware: PC Windows NT
: P3 enhancement (vote)
Target Milestone: 3.6 M6   Edit
Assignee: Ayushman Jain CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-08-28 05:54 EDT by Johan Compagner CLA
Modified: 2010-03-08 13:44 EST (History)
2 users (show)

See Also:


Attachments
Fix + Tests v0.1 (6.39 KB, patch)
2010-02-11 06:07 EST, Srikanth Sankaran CLA
no flags Details | Diff
fix + tests v0.2 (11.93 KB, patch)
2010-02-17 06:20 EST, Ayushman Jain CLA
no flags Details | Diff
Proposed patch (11.73 KB, patch)
2010-02-18 01:46 EST, Srikanth Sankaran CLA
srikanth_sankaran: iplog+
srikanth_sankaran: review+
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Johan Compagner CLA 2009-08-28 05:54:45 EDT
I love the feature that you can do:

if (objectClassExpression instanceof Identifier)
{
  objectClassExpression.get[CTRL-SPACE]

and that it becomes:

((Identifier)objectClassExpression).getName()

problem is that it doesnt work for the most used construct i use when using the instanceof expression:

if (objectClassExpression instanceof Identifier)
{
  name = objectClassExpression.get[CTRL-SPACE]

then it will not give me the option getName() and doesnt auto cast it.

It would be nice if anywhere in that if where you access the variable of the instanceof check you will have the code completion if it was already that Class.
Comment 1 Olivier Thomann CLA 2009-10-02 09:13:01 EDT
Reassigning to Ayushman since he is looking at it.
Comment 2 Srikanth Sankaran CLA 2010-02-11 06:07:45 EST
Created attachment 158833 [details]
Fix + Tests v0.1

Initial patch that mostly works.
Comment 3 Ayushman Jain CLA 2010-02-17 06:20:20 EST
Created attachment 159290 [details]
fix + tests v0.2

Here's a fix with minor changes to srikanth's patch above. Added 2 more test cases.

It works fine whenever the code assist is asked immediately after the if(...instanceof..) check. However, it doesnt work if there are any other expressions in between viz:

Object a;
int i = 1;
if(a instance of X){
   if(i == 1)
      i = a.[CTRL-SPACE]
}

Will open a separate bug report for the above and a few other cases where autocast doesnt work.
Comment 4 Srikanth Sankaran CLA 2010-02-18 00:54:57 EST
> It works fine whenever the code assist is asked immediately after the
> if(...instanceof..) check. However, it doesnt work if there are any other
> expressions in between viz:
> 
> Object a;
> int i = 1;
> if(a instance of X){
>    if(i == 1)
>       i = a.[CTRL-SPACE]
> }

For the record, this limitation is not new with this patch and
has always existed. See bug#193909 comment#12.

So even in the case cited as the working example in comment#0,
if you introduce an inner if, completion fails.
Comment 5 Srikanth Sankaran CLA 2010-02-18 01:46:39 EST
Created attachment 159397 [details]
Proposed patch

Same patch as from Ayush with small changes:

(1) Eliminated formatting changes in lines not modified for
this patch.

(2) if (this.expressionPtr > 0
        && this.expressionStack[this.expressionPtr - 1] != null
        && this.expressionStack[this.expressionPtr - 1] instanceof InstanceOfExpression)

is better written as 

if (this.expressionPtr > 0
        && this.expressionStack[this.expressionPtr - 1] instanceof InstanceOfExpression)

as null is guaranteed NOT to be an instance of any type.
Comment 6 Srikanth Sankaran CLA 2010-02-18 01:51:37 EST
Released in HEAD for 3.6M6
Comment 7 Olivier Thomann CLA 2010-03-08 13:44:26 EST
Verified for 3.6M6 using I20100307-2000.