Skip to content

Commit b94bc73

Browse files
committed
Partial fix for testBug209778
Signed-off-by: Rob Stryker <[email protected]> Attempt to fix regression testBug110336d Signed-off-by: Rob Stryker <[email protected]>
1 parent d30a7c5 commit b94bc73

File tree

2 files changed

+137
-42
lines changed

2 files changed

+137
-42
lines changed

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/core/search/DOMASTNodeUtils.java

Lines changed: 101 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*******************************************************************************/
1111
package org.eclipse.jdt.internal.core.search;
1212

13+
import java.util.ArrayList;
1314
import java.util.List;
1415

1516
import org.eclipse.jdt.core.IInitializer;
@@ -20,6 +21,7 @@
2021
import org.eclipse.jdt.core.JavaModelException;
2122
import org.eclipse.jdt.core.dom.ASTNode;
2223
import org.eclipse.jdt.core.dom.AbstractTypeDeclaration;
24+
import org.eclipse.jdt.core.dom.Annotation;
2325
import org.eclipse.jdt.core.dom.AnnotationTypeMemberDeclaration;
2426
import org.eclipse.jdt.core.dom.AnonymousClassDeclaration;
2527
import 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) {

org.eclipse.jdt.core.javac/src/org/eclipse/jdt/internal/core/search/matching/DOMJavaSearchDelegate.java

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -462,48 +462,42 @@ private SearchMatch toCoreMatch(MatchLocator locator, org.eclipse.jdt.core.dom.A
462462
}
463463
if (node instanceof Type nt) {
464464
//IBinding b = DOMASTNodeUtils.getBinding(nt);
465-
IJavaElement element = DOMASTNodeUtils.getEnclosingJavaElement(node);
466-
if (element instanceof LocalVariable) {
467-
element = element.getParent();
465+
IJavaElement enclosing = DOMASTNodeUtils.getEnclosingJavaElement(node);
466+
if (enclosing instanceof LocalVariable) {
467+
enclosing = enclosing.getParent();
468468
}
469-
TypeReferenceMatch ret = new TypeReferenceMatch(element, accuracy, node.getStartPosition(),
469+
TypeReferenceMatch ret = new TypeReferenceMatch(enclosing, accuracy, node.getStartPosition(),
470470
node.getLength(), DOMASTNodeUtils.insideDocComment(node), getParticipant(locator), resource);
471471
if (nt.isParameterizedType()) {
472472
if (((ParameterizedType) nt).typeArguments().size() == 0) {
473473
ret.setRaw(true);
474474
}
475475
}
476-
IJavaElement localJavaElement = DOMASTNodeUtils.getLocalJavaElement(node);
477-
if (!Objects.equals(localJavaElement, element)) {
478-
boolean isAnonymous = false;
479-
try {
480-
isAnonymous = element instanceof IType && ((IType)element).isAnonymous();
481-
} catch(JavaModelException jme) {
482-
// ignore
483-
}
484-
if( !isAnonymous) {
485-
ret.setLocalElement(localJavaElement);
476+
IJavaElement local = DOMASTNodeUtils.getLocalJavaElement(node);
477+
IJavaElement[] otherJavaElements = DOMASTNodeUtils.getLocalOrOtherJavaElements(node, false);
478+
//boolean localElementIsWithinAnnotation = DOMASTNodeUtils.annotationBetweenNodeAndLocalElement(node);
479+
if (!Objects.equals(local, enclosing)) {
480+
if( !isAnonymousIType(enclosing)) {
481+
ret.setLocalElement(local);
486482
}
487483
}
488-
if (node.getLocationInParent() == VariableDeclarationStatement.TYPE_PROPERTY && node.getParent() instanceof VariableDeclarationStatement stmt && stmt.fragments().size() > 1) {
489-
ret.setOtherElements(((List<VariableDeclarationFragment>)stmt.fragments())
490-
.subList(1, stmt.fragments().size())
491-
.stream()
492-
.map(VariableDeclarationFragment::resolveBinding)
493-
.filter(x -> x != null)
494-
.map(IVariableBinding::getJavaElement)
495-
.filter(x -> x != null)
496-
.toArray(IJavaElement[]::new));
484+
ArrayList<IJavaElement> otherElements = new ArrayList<>();
485+
for( int i = 0; i < otherJavaElements.length; i++ ) {
486+
if (!Objects.equals(otherJavaElements[i], enclosing)) {
487+
boolean isAnonymous = false;
488+
try {
489+
isAnonymous = enclosing instanceof IType && ((IType)enclosing).isAnonymous();
490+
} catch(JavaModelException jme) {
491+
// ignore
492+
}
493+
if( !isAnonymous) {
494+
if( local == null || !local.equals(otherJavaElements[i]))
495+
otherElements.add(otherJavaElements[i]);
496+
}
497+
}
497498
}
498-
if (node.getLocationInParent() == FieldDeclaration.TYPE_PROPERTY && node.getParent() instanceof FieldDeclaration stmt && stmt.fragments().size() > 1) {
499-
ret.setOtherElements(((List<VariableDeclarationFragment>)stmt.fragments())
500-
.subList(1, stmt.fragments().size())
501-
.stream()
502-
.map(VariableDeclarationFragment::resolveBinding)
503-
.filter(x -> x != null)
504-
.map(IVariableBinding::getJavaElement)
505-
.filter(x -> x != null)
506-
.toArray(IJavaElement[]::new));
499+
if( otherElements.size() > 0 ) {
500+
ret.setOtherElements(otherElements.toArray(new IJavaElement[otherElements.size()]));
507501
}
508502
return ret;
509503
}
@@ -603,6 +597,16 @@ private SearchMatch toCoreMatch(MatchLocator locator, org.eclipse.jdt.core.dom.A
603597
return null;
604598
}
605599

600+
private boolean isAnonymousIType(IJavaElement type) {
601+
boolean isAnonymous = false;
602+
try {
603+
isAnonymous = type instanceof IType && ((IType)type).isAnonymous();
604+
} catch(JavaModelException jme) {
605+
// ignore
606+
}
607+
return isAnonymous;
608+
}
609+
606610
private String findRawTextForNodesCompilationUnit(ASTNode node) {
607611
JavacBindingResolver resolver = JdtCoreDomPackagePrivateUtility.getJavacBindingResolverOrNull(node);
608612
if( resolver != null ) {

0 commit comments

Comments
 (0)