diff --git a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/DOMASTNodeUtils.java b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/DOMASTNodeUtils.java index 61cde525682..cd0b9a6ca9d 100644 --- a/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/DOMASTNodeUtils.java +++ b/org.eclipse.jdt.core/search/org/eclipse/jdt/internal/core/search/matching/DOMASTNodeUtils.java @@ -12,6 +12,8 @@ import java.util.List; import org.eclipse.jdt.core.IJavaElement; +import org.eclipse.jdt.core.IMember; +import org.eclipse.jdt.core.IParent; import org.eclipse.jdt.core.ISourceRange; import org.eclipse.jdt.core.ISourceReference; import org.eclipse.jdt.core.JavaModelException; @@ -26,7 +28,7 @@ public static IJavaElement getEnclosingJavaElement(ASTNode node) { if (node instanceof AbstractTypeDeclaration || node instanceof MethodDeclaration || node instanceof FieldDeclaration - //|| node instanceof VariableDeclaration + || node instanceof Initializer || node instanceof CompilationUnit || node instanceof AnnotationTypeMemberDeclaration) { return getDeclaringJavaElement(node); @@ -57,6 +59,34 @@ private static IJavaElement findElementForNodeCustom(ASTNode key) { } } } + if( key instanceof Initializer i) { + ASTNode parentNode = i.getParent(); + int domOccurance = -1; + if( parentNode instanceof AbstractTypeDeclaration typeDecl) { + List parentBody = typeDecl.bodyDeclarations(); + for( int z = 0; z < parentBody.size() && domOccurance == -1; z++ ) { + if( parentBody.get(z) == key) { + domOccurance = z + 1; + } + } + } + IJavaElement parentEl = findElementForNodeViaDirectBinding(parentNode); + if( parentEl instanceof IParent parentElement) { + try { + IJavaElement[] kiddos = parentElement.getChildren(); + for( int q = 0; q < kiddos.length; q++ ) { + if( kiddos[q] instanceof IMember kiddoMember) { + int count = kiddoMember.getOccurrenceCount(); + if( count == domOccurance ) { + return kiddos[q]; + } + } + } + } catch( JavaModelException jme) { + // ignore + } + } + } return null; }