Skip to content

Commit 9c8ecac

Browse files
committed
Improve matching/reporting method references
1 parent 326113e commit 9c8ecac

File tree

2 files changed

+51
-11
lines changed

2 files changed

+51
-11
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import org.eclipse.jdt.core.dom.ConstructorInvocation;
5555
import org.eclipse.jdt.core.dom.CreationReference;
5656
import org.eclipse.jdt.core.dom.EnumConstantDeclaration;
57+
import org.eclipse.jdt.core.dom.ExpressionMethodReference;
5758
import org.eclipse.jdt.core.dom.FieldDeclaration;
5859
import org.eclipse.jdt.core.dom.IBinding;
5960
import org.eclipse.jdt.core.dom.IMethodBinding;
@@ -70,7 +71,9 @@
7071
import org.eclipse.jdt.core.dom.SingleMemberAnnotation;
7172
import org.eclipse.jdt.core.dom.SuperConstructorInvocation;
7273
import org.eclipse.jdt.core.dom.SuperMethodInvocation;
74+
import org.eclipse.jdt.core.dom.SuperMethodReference;
7375
import org.eclipse.jdt.core.dom.Type;
76+
import org.eclipse.jdt.core.dom.TypeMethodReference;
7477
import org.eclipse.jdt.core.dom.VariableDeclaration;
7578
import org.eclipse.jdt.core.dom.VariableDeclarationFragment;
7679
import org.eclipse.jdt.core.dom.VariableDeclarationStatement;
@@ -372,6 +375,39 @@ private SearchMatch toCoreMatch(MatchLocator locator, org.eclipse.jdt.core.dom.A
372375
res.setLocalElement(DOMASTNodeUtils.getLocalJavaElement(node));
373376
return res;
374377
}
378+
if (node instanceof ExpressionMethodReference method) {
379+
IJavaElement enclosing = DOMASTNodeUtils.getEnclosingJavaElement(node.getParent());
380+
IMethodBinding mb = method.resolveMethodBinding();
381+
boolean isSynthetic = mb != null && mb.isSynthetic();
382+
var res = new MethodReferenceMatch(enclosing, accuracy, method.getName().getStartPosition(),
383+
method.getStartPosition() + method.getLength() - method.getName().getStartPosition(), false,
384+
isSynthetic, (accuracy & PatternLocator.SUPER_INVOCATION_FLAVOR) != 0, insideDocComment(node), getParticipant(locator), resource);
385+
res.setRaw(mb != null && mb.isRawMethod());
386+
res.setLocalElement(DOMASTNodeUtils.getLocalJavaElement(node));
387+
return res;
388+
}
389+
if (node instanceof TypeMethodReference method) {
390+
IJavaElement enclosing = DOMASTNodeUtils.getEnclosingJavaElement(node.getParent());
391+
IMethodBinding mb = method.resolveMethodBinding();
392+
boolean isSynthetic = mb != null && mb.isSynthetic();
393+
var res = new MethodReferenceMatch(enclosing, accuracy, method.getName().getStartPosition(),
394+
method.getStartPosition() + method.getLength() - method.getName().getStartPosition(), false,
395+
isSynthetic, (accuracy & PatternLocator.SUPER_INVOCATION_FLAVOR) != 0, insideDocComment(node), getParticipant(locator), resource);
396+
res.setRaw(mb != null && mb.isRawMethod());
397+
res.setLocalElement(DOMASTNodeUtils.getLocalJavaElement(node));
398+
return res;
399+
}
400+
if (node instanceof SuperMethodReference method) {
401+
IJavaElement enclosing = DOMASTNodeUtils.getEnclosingJavaElement(node.getParent());
402+
IMethodBinding mb = method.resolveMethodBinding();
403+
boolean isSynthetic = mb != null && mb.isSynthetic();
404+
var res = new MethodReferenceMatch(enclosing, accuracy, method.getName().getStartPosition(),
405+
method.getStartPosition() + method.getLength() - method.getName().getStartPosition(), false,
406+
isSynthetic, (accuracy & PatternLocator.SUPER_INVOCATION_FLAVOR) != 0, insideDocComment(node), getParticipant(locator), resource);
407+
res.setRaw(mb != null && mb.isRawMethod());
408+
res.setLocalElement(DOMASTNodeUtils.getLocalJavaElement(node));
409+
return res;
410+
}
375411
if (node.getLocationInParent() == SingleMemberAnnotation.VALUE_PROPERTY && locator.pattern instanceof MethodPattern) {
376412
var res = new MethodReferenceMatch(DOMASTNodeUtils.getEnclosingJavaElement(node), accuracy,
377413
node.getStartPosition(), node.getLength(), true,

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,9 @@ public LocatorResponse match(MethodRef node, NodeSetWrapper nodeSet, MatchLocato
192192
}
193193
@Override
194194
public LocatorResponse match(MethodReference node, NodeSetWrapper nodeSet, MatchLocator locator) {
195-
if (this.locator.pattern.fineGrain != 0 && (this.locator.pattern.fineGrain & IJavaSearchConstants.METHOD_REFERENCE_EXPRESSION) == 0) {
195+
if (this.locator.pattern.fineGrain != 0 &&
196+
(this.locator.pattern.fineGrain & IJavaSearchConstants.METHOD_REFERENCE_EXPRESSION) == 0 &&
197+
(!(node instanceof SuperMethodReference) || (this.locator.pattern.fineGrain & IJavaSearchConstants.SUPER_REFERENCE) == 0)) {
196198
return toResponse(IMPOSSIBLE_MATCH);
197199
}
198200
SimpleName name = node instanceof TypeMethodReference typeMethodRef ? typeMethodRef.getName() :
@@ -232,10 +234,12 @@ public LocatorResponse match(org.eclipse.jdt.core.dom.Expression expression, Nod
232234

233235
@Override
234236
public LocatorResponse match(Name node, NodeSetWrapper nodeSet, MatchLocator locator) {
235-
if( node.getParent() instanceof MethodInvocation mi && mi.getName() == node) {
236-
// if( nodeSet.getTrustedMatch(mi) > IMPOSSIBLE_MATCH ) {
237-
return toResponse(IMPOSSIBLE_MATCH);
238-
// }
237+
if (// already matched
238+
MethodInvocation.NAME_PROPERTY == node.getLocationInParent() ||
239+
TypeMethodReference.NAME_PROPERTY == node.getLocationInParent() ||
240+
ExpressionMethodReference.NAME_PROPERTY == node.getLocationInParent() ||
241+
SuperMethodReference.NAME_PROPERTY == node.getLocationInParent()) {
242+
return toResponse(IMPOSSIBLE_MATCH);
239243
}
240244

241245
if (node.getLocationInParent() == MemberValuePair.NAME_PROPERTY
@@ -691,12 +695,12 @@ protected int resolveLevelForNodeWithMethodBinding(ASTNode messageSend,
691695
// String q2 = declBindingClass == null ? null : declBindingClass.getQualifiedName();
692696
// String b1 = invocationDeclClass == null ? null : invocationDeclClass.getBinaryName();
693697
// String b2 = declBindingClass == null ? null : declBindingClass.getBinaryName();
694-
int declaringLevel;
695-
ITypeBinding receiverType = initialReceiverType != null ? initialReceiverType : invocationOrDeclarationBinding.getDeclaringClass();
696-
if (shouldResolveSubSuperLevel(messageSend, receiverType, invocationOrDeclarationBinding, invocOrDeclLevel)) {
697-
declaringLevel = resolveSubSuperLevel(messageSend, receiverType, invocationOrDeclarationBinding, invocOrDeclLevel, nullParamsForSubTypeCheck);
698-
} else {
699-
declaringLevel = resolveLevelForType(this.pattern.declaringSimpleName, this.pattern.declaringQualification, invocationOrDeclarationBinding.getDeclaringClass());
698+
int declaringLevel = resolveLevelForType(this.pattern.declaringSimpleName, this.pattern.declaringQualification, invocationOrDeclarationBinding.getDeclaringClass());
699+
if (declaringLevel == IMPOSSIBLE_MATCH) {
700+
ITypeBinding receiverType = initialReceiverType != null ? initialReceiverType : invocationOrDeclarationBinding.getDeclaringClass();
701+
if (shouldResolveSubSuperLevel(messageSend, receiverType, invocationOrDeclarationBinding, invocOrDeclLevel)) {
702+
declaringLevel = resolveSubSuperLevel(messageSend, receiverType, invocationOrDeclarationBinding, invocOrDeclLevel, nullParamsForSubTypeCheck);
703+
}
700704
}
701705

702706
int declaringFlavors = declaringLevel & FLAVORS_MASK;

0 commit comments

Comments
 (0)