1010 *******************************************************************************/
1111package org .eclipse .jdt .internal .core .search ;
1212
13+ import java .util .ArrayList ;
1314import java .util .List ;
1415
1516import org .eclipse .jdt .core .IInitializer ;
2021import org .eclipse .jdt .core .JavaModelException ;
2122import org .eclipse .jdt .core .dom .ASTNode ;
2223import org .eclipse .jdt .core .dom .AbstractTypeDeclaration ;
24+ import org .eclipse .jdt .core .dom .Annotation ;
2325import org .eclipse .jdt .core .dom .AnnotationTypeMemberDeclaration ;
2426import org .eclipse .jdt .core .dom .AnonymousClassDeclaration ;
2527import org .eclipse .jdt .core .dom .ClassInstanceCreation ;
@@ -85,31 +87,120 @@ public static IJavaElement getEnclosingJavaElement(ASTNode node) {
8587 return getEnclosingJavaElement (node .getParent ());
8688 }
8789
90+
91+ public static boolean annotationBetweenNodeAndLocalElement (ASTNode node ) {
92+ ASTNode working = node ;
93+ if ( node == null )
94+ return false ;
95+ boolean annotFound = false ;
96+ while (working != null ) {
97+ annotFound |= working instanceof Annotation ;
98+ if (node instanceof VariableDeclaration )
99+ return annotFound ;
100+ if (node instanceof VariableDeclarationStatement )
101+ return annotFound ;
102+ if (node instanceof VariableDeclarationExpression )
103+ return annotFound ;
104+ if (node instanceof TypeParameter typeParam )
105+ return annotFound ;
106+ if (node instanceof AbstractTypeDeclaration
107+ || node instanceof MethodDeclaration
108+ || node instanceof FieldDeclaration
109+ || node instanceof Initializer
110+ || node instanceof ImportDeclaration
111+ || node instanceof PackageDeclaration
112+ || node instanceof CompilationUnit
113+ || node instanceof AnnotationTypeMemberDeclaration
114+ || node instanceof Initializer
115+ || node instanceof LambdaExpression
116+ || node .getLocationInParent () == FieldDeclaration .FRAGMENTS_PROPERTY )
117+ return annotFound ;
118+ working = working .getParent ();
119+ }
120+ return annotFound ;
121+ }
122+
88123 /**
89124 * @param node
90125 * @return an existing (in model) Java element enclosing the node
91126 */
92127 public static IJavaElement getLocalJavaElement (ASTNode node ) {
128+ IJavaElement [] r = getLocalOrOtherJavaElements (node , true );
129+ return r != null && r .length > 0 && r [0 ] != null ? r [0 ] : null ;
130+ }
131+
132+ public static IJavaElement [] getLocalOrOtherJavaElements (ASTNode node , boolean local ) {
93133 if (node == null ) {
94134 return null ;
95135 }
96136 if (node instanceof VariableDeclaration variable ) {
97137 IVariableBinding vb = variable .resolveBinding ();
98138 if ( vb != null )
99- return vb .getJavaElement ();
139+ return new IJavaElement [] { vb .getJavaElement ()} ;
100140 }
101141 if (node instanceof VariableDeclarationStatement variable && !variable .fragments ().isEmpty ()) {
102- IVariableBinding vb = ((List <VariableDeclarationFragment >)variable .fragments ()).iterator ().next ().resolveBinding ();
103- if ( vb != null )
104- return vb .getJavaElement ();
142+ if ( local ) {
143+ IVariableBinding vb = ((List <VariableDeclarationFragment >)variable .fragments ()).iterator ().next ().resolveBinding ();
144+ if ( vb != null )
145+ return new IJavaElement [] {vb .getJavaElement ()};
146+ } else {
147+ ArrayList <IJavaElement > ret = new ArrayList <>();
148+ List <VariableDeclarationFragment > l1 = variable .fragments ();
149+ boolean first = true ;
150+ for ( VariableDeclarationFragment v1 : l1 ) {
151+ if ( first ) {
152+ first = false ;
153+ continue ;
154+ }
155+ IVariableBinding vb = v1 .resolveBinding ();
156+ if ( vb != null ) {
157+ IJavaElement e = vb .getJavaElement ();
158+ if ( e != null ) {
159+ ret .add (e );
160+ }
161+ }
162+ }
163+ return ret .toArray (new IJavaElement [ret .size ()]);
164+ }
105165 }
106166 if (node instanceof VariableDeclarationExpression variable && !variable .fragments ().isEmpty ()) {
107- IVariableBinding vb = ((List <VariableDeclarationFragment >)variable .fragments ()).iterator ().next ().resolveBinding ();
108- if ( vb != null )
109- return vb .getJavaElement ();
167+ if ( local ) {
168+ IVariableBinding vb = ((List <VariableDeclarationFragment >)variable .fragments ()).iterator ().next ().resolveBinding ();
169+ if ( vb != null )
170+ return new IJavaElement [] {vb .getJavaElement ()};
171+ } else {
172+ ArrayList <IJavaElement > ret = new ArrayList <>();
173+ List <VariableDeclarationFragment > l1 = variable .fragments ();
174+ boolean first = true ;
175+ for ( VariableDeclarationFragment v1 : l1 ) {
176+ if ( first ) {
177+ first = false ;
178+ continue ;
179+ }
180+ IVariableBinding vb = v1 .resolveBinding ();
181+ if ( vb != null ) {
182+ IJavaElement e = vb .getJavaElement ();
183+ if ( e != null ) {
184+ ret .add (e );
185+ }
186+ }
187+ }
188+ return ret .toArray (new IJavaElement [ret .size ()]);
189+ }
110190 }
191+
192+ if (!local && node .getLocationInParent () == FieldDeclaration .TYPE_PROPERTY && node .getParent () instanceof FieldDeclaration stmt && stmt .fragments ().size () > 1 ) {
193+ return ((List <VariableDeclarationFragment >)stmt .fragments ())
194+ .stream ()
195+ .map (VariableDeclarationFragment ::resolveBinding )
196+ .filter (x -> x != null )
197+ .map (IVariableBinding ::getJavaElement )
198+ .filter (x -> x != null )
199+ .toArray (IJavaElement []::new );
200+ }
201+
111202 if (node instanceof TypeParameter typeParam ) {
112- return typeParam .resolveBinding ().getJavaElement ();
203+ return new IJavaElement [] { typeParam .resolveBinding ().getJavaElement ()} ;
113204 }
114205 if (node instanceof AbstractTypeDeclaration
115206 || node instanceof MethodDeclaration
@@ -122,9 +213,9 @@ public static IJavaElement getLocalJavaElement(ASTNode node) {
122213 || node instanceof Initializer
123214 || node instanceof LambdaExpression
124215 || node .getLocationInParent () == FieldDeclaration .FRAGMENTS_PROPERTY ) {
125- return getEnclosingJavaElement (node );
216+ return new IJavaElement [] { getEnclosingJavaElement (node ) } ;
126217 }
127- return getLocalJavaElement (node .getParent ());
218+ return getLocalOrOtherJavaElements (node .getParent (), local );
128219 }
129220
130221 public static IJavaElement getDeclaringJavaElement (ASTNode key ) {
0 commit comments