Bug 33621 - ICompilationUnit.getElementAt(int) returns strange things when parameter is in a field declaration inside anonymous and local classes
Summary: ICompilationUnit.getElementAt(int) returns strange things when parameter is i...
Status: RESOLVED WONTFIX
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 2.1   Edit
Hardware: Macintosh Mac OS X - Carbon (unsup.)
: P3 normal (vote)
Target Milestone: 2.1 RC2   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2003-03-02 11:12 EST by Lance Walton CLA
Modified: 2003-03-04 05:39 EST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Lance Walton CLA 2003-03-02 11:12:45 EST
I have the following example code:

public class EnclosingElementTest {
	private int a;
	
	private Object b = new Object() {
		private int b1;
	};
	
	private class StaticInner {
		private int c;
	}
	
	public EnclosingElementTest() {
		Object localObjectVariable = new Object() {
			private int d;
		};
		
		class Local {
			private int e;
		}
	}
}
Comment 1 Lance Walton CLA 2003-03-02 11:14:30 EST
For some reason the rest of my report got chopped. Here it is:

If I get the ICompilationUnit for the above and then print out the results of getElementAt(int) for each of the field declarations (i.e. passing the position of the variable into getElementAt(int)), I get the following:

a: int a [in EnclosingElementTest [in EnclosingElementTest.java [in [default] [in [project root] [in Test]]]]]
b: Object b [in EnclosingElementTest [in EnclosingElementTest.java [in [default] [in [project root] [in Test]]]]]
b1: Object b [in EnclosingElementTest [in EnclosingElementTest.java [in [default] [in [project root] [in Test]]]]]
c: int c [in StaticInner [in EnclosingElementTest [in EnclosingElementTest.java [in [default] [in [project root] [in Test]]]]]]
d: EnclosingElementTest() [in EnclosingElementTest [in EnclosingElementTest.java [in [default] [in [project root] [in Test]]]]]
e: EnclosingElementTest() [in EnclosingElementTest [in EnclosingElementTest.java [in [default] [in [project root] [in Test]]]]]

It seems that the enclosing element for a, b and c are correct. However:

   * the enclosing element for b1 appears not to be the declaration of b1, but the declaration of the variable referring to the enclosing anonymous class.
   * the enclosing element for d appears to be the method in which the anonymous class (that contains d) appears.
   * the enclosing element for e appears to be the method in which the local class (that contains e) appears.

Surely this isn't correct?

Lance
Comment 2 Lance Walton CLA 2003-03-02 11:16:11 EST
For some reason the rest of my report got chopped. Here it is:If I get the ICompilationUnit for the above and then print out the results of getElementAt(int) for each of the field declarations (i.e. passing the position of the variable into getElementAt(int)), I get the following:a: int a [in EnclosingElementTest [in EnclosingElementTest.java [in [default] [in [project root] [in Test]]]]]b: Object b [in EnclosingElementTest [in EnclosingElementTest.java [in [default] [in [project root] [in Test]]]]]b1: Object b [in EnclosingElementTest [in EnclosingElementTest.java [in [default] [in [project root] [in Test]]]]]c: int c [in StaticInner [in EnclosingElementTest [in EnclosingElementTest.java [in [default] [in [project root] [in Test]]]]]]d: EnclosingElementTest() [in EnclosingElementTest [in EnclosingElementTest.java [in [default] [in [project root] [in Test]]]]]e: EnclosingElementTest() [in EnclosingElementTest [in EnclosingElementTest.java [in [default] [in [project root] [in Test]]]]]It seems that the enclosing element for a, b and c are correct. However:   * the enclosing element for b1 appears not to be the declaration of b1, but the declaration of the variable referring to the enclosing anonymous class.   * the enclosing element for d appears to be the method in which the anonymous class (that contains d) appears.   * the enclosing element for e appears to be the method in which the local class (that contains e) appears.Surely this isn't correct?Lance
Comment 3 Jerome Lanneluc CLA 2003-03-02 17:17:06 EST
There is no IJavaElement for local and anonymous classes. So the closest match 
is the IJavaElement that defines the local or anoymous class:
b1: The field EnclosingElementTest.b
d: The constructor EnclosingElementTest()
e: The constructor EnclosingElementTest()
Comment 4 Lance Walton CLA 2003-03-02 17:27:17 EST
Is there a way for me to tell if a field is declared inside a local/anonymous class or inside the enclosing class?

Regards,

Lance
Comment 5 Philipe Mulet CLA 2003-03-03 05:06:17 EST
There is no such thing as a local type field or method for the JavaModel, when 
you ask about an element at a position, it will answer back the toplevel member 
which contains this position.

You cannot find 'b1' in the JavaModel, anything inside 'b' is said to be 'b'.

If you want finer grain information, you should consider using the DOM AST 
instead. The JavaModel is more lightweight, and we have plans in the future to 
extend it so as to reflect local type/variable declarations.

Is it ok to close ?
Comment 6 Lance Walton CLA 2003-03-03 14:15:18 EST
OK. I'll probably be able to get what I need out of the DOM. Feel free to close.

Regards,

Lance
Comment 7 Philipe Mulet CLA 2003-03-04 05:39:48 EST
Closing.