Bug 287939

Summary: [code assist] The instanceof and the auto cast feature should also work for an assignment
Product: [Eclipse Project] JDT Reporter: Johan Compagner <jcompagner>
Component: CoreAssignee: Ayushman Jain <amj87.iitr>
Status: VERIFIED FIXED QA Contact:
Severity: enhancement    
Priority: P3 CC: Olivier_Thomann, srikanth_sankaran
Version: 3.5   
Target Milestone: 3.6 M6   
Hardware: PC   
OS: Windows NT   
Whiteboard:
Attachments:
Description Flags
Fix + Tests v0.1
none
fix + tests v0.2
none
Proposed patch srikanth_sankaran: iplog+, srikanth_sankaran: review+

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.