Community
Participate
Working Groups
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; } } }
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
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
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()
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
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 ?
OK. I'll probably be able to get what I need out of the DOM. Feel free to close. Regards, Lance
Closing.