Bug 333964 - [DOM] Class initializers' local variables' IVariableBinding have no declaring class
Summary: [DOM] Class initializers' local variables' IVariableBinding have no declaring...
Status: VERIFIED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.7   Edit
Hardware: PC Windows 7
: P3 major (vote)
Target Milestone: 3.7 M5   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-01-11 07:24 EST by arlindolima CLA
Modified: 2011-01-25 04:50 EST (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description arlindolima CLA 2011-01-11 07:24:19 EST
Build Identifier: M20100909-0800

I've noticed, local variables declared in an instance initializer have no "declaring class" (IVariableBinding.getDeclaringClass() returns null).

However, classes declared inside an instance initializer return the enclosing class as the declaring class.

I think this inconsistency should be considered a bug. Knowing a local variable's enclosing class is _extremely_ useful.

Reproducible: Always

Steps to Reproduce:
1. Example code:

public class Initializer {
    {
        int i = 2;       // has no declaring class
        class Inside { } // has declaring class
    }
}
Comment 1 Olivier Thomann CLA 2011-01-11 08:52:08 EST
It would be an API breakage to return a declaring class for a local variable.

	/**
	 * Returns the type binding representing the class or interface
	 * that declares this field.
	 * <p>
	 * The declaring class of a field is the class or interface of which it is
	 * a member. Local variables have no declaring class. The field length of an
	 * array type has no declaring class.
	 * </p>
	 *
	 * @return the binding of the class or interface that declares this field,
	 *   or <code>null</code> if none
	 */
	public ITypeBinding getDeclaringClass();

Closing as INVALID.

You can retrieve the declaring method for the local variable. If you get null for the declaring method and null for the declaring class, then you know that you have a local variable inside an initializer (static or not).
Comment 2 arlindolima CLA 2011-01-11 09:03:49 EST
It would be very useful to know the declaring class (for building a "semi-qualified" name - I know local variables have no qualified name). But not a bug, indeed. Should this be considered as a RFE?
Comment 3 Olivier Thomann CLA 2011-01-11 10:29:31 EST
(In reply to comment #2)
> It would be very useful to know the declaring class (for building a
> "semi-qualified" name - I know local variables have no qualified name). But not
> a bug, indeed. Should this be considered as a RFE?
Yes, please. Open a new bug report. A new method would be required on IVariableBinding.

In your case would the binding's key be good enough ?
Comment 4 arlindolima CLA 2011-01-11 11:01:32 EST
Yes, I'm currently using the binding key. But knowing the enclosing class matters would be a very desirable feature.
Comment 5 Jay Arthanareeswaran CLA 2011-01-25 04:50:16 EST
Verified for 3.7M5