Bug 46192 - ILocalVariable.exists() always returns false
Summary: ILocalVariable.exists() always returns false
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.0   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.0 M5   Edit
Assignee: Jerome Lanneluc CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2003-11-06 08:51 EST by Dani Megert CLA
Modified: 2003-11-24 06:22 EST (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dani Megert CLA 2003-11-06 08:51:26 EST
I20031105

After talking to Jerome, reading the Javadoc and discussing within the JDT UI
team we still consider it a bug that the exists() method returns false:

- it is not logical that the element whose parent exist doesn't exist
- wouldn't sort of a synthetic flag be better?
- how can we know if the local still exists? Do we have to ask the parent?
Comment 1 Jerome Lanneluc CLA 2003-11-06 16:23:05 EST
- it is not logical that the element whose parent exist doesn't exist
Wrong. You can create a method handle whose parent is an existing type without 
the method existing.

- wouldn't sort of a synthetic flag be better?
What do you mean?

- how can we know if the local still exists? Do we have to ask the parent?
It never existed. It doesn't exist. It will never exist. This is just a pseudo-
element resulting from a codeSelect(...). Its lifetime is 0. You cannot use it 
outside the operation that created it. Never keep a reference to it.
Comment 2 Philipe Mulet CLA 2003-11-06 18:06:51 EST
Alternatively, it could always answer true if the enclosing method exists. 
Comment 3 Philipe Mulet CLA 2003-11-06 18:08:08 EST
However, I agree with Jerome that you shouldn't be persisting these, and have 
to ask yourself questions such as #exists(). What are you trying to do ?
Comment 4 Dani Megert CLA 2003-11-07 03:35:14 EST
I was running through existing code (of which we have a lot) but stuff (in my
case adapted code for getting the JavaSourceHover and JavaElementLabels) for the
local variable never showed up. The reason was that we test for
IJavaElement.exists where it falls through. This basically means that
ILocalVariable (returned for existing elements) will never pass any of our JDT
UI code that is protected with exists().
Comment 5 Jerome Lanneluc CLA 2003-11-07 04:54:29 EST
Right, you need to change this code
Comment 6 Philipe Mulet CLA 2003-11-07 05:03:58 EST
Can't we be optimistic and explain that variable elements are living until the 
code is changed again. Thus until this occurs, it will always pretend it exists 
by default. I suspect it could be protected by saying that it exists as long as 
its enclosing element exists, but this is just to be nice.
Comment 7 Jerome Lanneluc CLA 2003-11-07 06:24:24 EST
We have no way to know that the code changed under the local variable handle. 
It is not a child of its method.
Comment 8 Philipe Mulet CLA 2003-11-07 06:55:20 EST
I agree, though for the same reason it could just pretend to exist all the time 
(equally wrong, but lees suspicious to clients ?). No strong opininon.

If always true, then IJavaElement#exists API spec need to be tuned to describe 
this situation.

Side-question: Dani, are you using the memento API of a local variable ? We 
suspect we shouldn't have provided it ever, since it incorrectly encourages 
clients to persist such handles for more than their expected life time (which 
is very short).
Couldn't search results simply extract the info they need from elements, 
instead of storing the element memento ? Names + source ranges.
Comment 9 Dani Megert CLA 2003-11-07 11:22:34 EST
Dirk, please comment as well.
Comment 10 Dirk Baeumer CLA 2003-11-07 11:37:16 EST
For me it feels wrong that exists always returns false for local variable, 
especially since they are IJavaElements. If an API returns IJavaElement the 
exist method for that Java element should return a "reasonable" value 
independent of the concert element that got returned. Meaning, if I do code 
resolve and get a local variable back, it exists and therefore #exists() 
should return true. Otherwise we always have to treat locals special which 
results in lots of instanceof checks. And how will we find out that a 
local "really" doesn't exist.

A solution as outlined by Philippe in comment #6 is IMO the right path to 
follow.
Comment 11 Jerome Lanneluc CLA 2003-11-17 04:30:19 EST
Changed spec and behavior to return whether the parent exists.
Comment 12 David Audel CLA 2003-11-24 06:22:01 EST
Verified.