// Internal use only - use findMethod()
public MethodBinding findMethod(ReferenceBinding receiverType, char[] selector, TypeBinding[] argumentTypes, InvocationSite invocationSite, boolean inStaticContext) {
ReferenceBinding currentType = receiverType;
boolean receiverTypeIsInterface = receiverType.isInterface();
ObjectVector found = new ObjectVector(3);
CompilationUnitScope unitScope = compilationUnitScope();
unitScope.recordTypeReferences(argumentTypes);
if (receiverTypeIsInterface) {
unitScope.recordTypeReference(receiverType);
MethodBinding[] receiverMethods = receiverType.getMethods(selector, argumentTypes.length);
if (receiverMethods.length > 0)
found.addAll(receiverMethods);
findMethodInSuperInterfaces(receiverType, selector, found, invocationSite);
currentType = getJavaLangObject();
}
// superclass lookup
long complianceLevel = compilerOptions().complianceLevel;
boolean isCompliant14 = complianceLevel >= ClassFileConstants.JDK1_4;
boolean isCompliant15 = complianceLevel >= ClassFileConstants.JDK1_5;
ReferenceBinding classHierarchyStart = currentType;
MethodVerifier verifier = environment().methodVerifier();
while (currentType != null) {
unitScope.recordTypeReference(currentType);
currentType = (ReferenceBinding) currentType.capture(this, invocationSite == null ? 0 : invocationSite.sourceEnd());
MethodBinding[] currentMethods = currentType.getMethods(selector, argumentTypes.length);
int currentLength = currentMethods.length;
if (currentLength > 0) {
if (isCompliant14 && (receiverTypeIsInterface || found.size > 0)) {
nextMethod: for (int i = 0, l = currentLength; i < l; i++) { // currentLength can be modified inside the loop
MethodBinding currentMethod = currentMethods[i];
if (currentMethod == null) continue nextMethod;
if (receiverTypeIsInterface && !currentMethod.isPublic()) { // only public methods from Object are visible to interface receiverTypes
currentLength--;
currentMethods[i] = null;
continue nextMethod;
}
// if 1.4 compliant, must filter out redundant protected methods from superclasses
// protected method need to be checked only - default access is already dealt with in #canBeSeen implementation
// when checking that p.C -> q.B -> p.A cannot see default access members from A through B.
// if ((currentMethod.modifiers & AccProtected) == 0) continue nextMethod;
// BUT we can also ignore any overridden method since we already know the better match (fixes 80028)
for (int j = 0, max = found.size; j < max; j++) {
MethodBinding matchingMethod = (MethodBinding) found.elementAt(j);
MethodBinding matchingOriginal = matchingMethod.original();
MethodBinding currentOriginal = matchingOriginal.findOriginalInheritedMethod(currentMethod);
if (currentOriginal != null && verifier.isParameterSubsignature(matchingOriginal, currentOriginal)) {
if (isCompliant15) {
if (matchingMethod.isBridge() && !currentMethod.isBridge())
continue nextMethod; // keep inherited methods to find concrete method over a bridge method
}
currentLength--;
currentMethods[i] = null;
continue nextMethod;
}
}
}
}
if (currentLength > 0) {
// append currentMethods, filtering out null entries
if (currentMethods.length == currentLength) {
found.addAll(currentMethods);
} else {
for (int i = 0, max = currentMethods.length; i < max; i++) {
MethodBinding currentMethod = currentMethods[i];
if (currentMethod != null)
found.add(currentMethod);
}
}
}
}
currentType = currentType.superclass();
}
// if found several candidates, then eliminate those not matching argument types
int foundSize = found.size;
MethodBinding[] candidates = null;
int candidatesCount = 0;
MethodBinding problemMethod = null;
boolean searchForDefaultAbstractMethod = isCompliant14 && ! receiverTypeIsInterface && (receiverType.isAbstract() || receiverType.isTypeVariable());
if (foundSize > 0) {
// argument type compatibility check
for (int i = 0; i < foundSize; i++) {
MethodBinding methodBinding = (MethodBinding) found.elementAt(i);
MethodBinding compatibleMethod = computeCompatibleMethod(methodBinding, argumentTypes, invocationSite);
if (compatibleMethod != null) {
if (compatibleMethod.isValidBinding()) {
if (foundSize == 1 && compatibleMethod.canBeSeenBy(receiverType, invocationSite, this)) {
// return the single visible match now
if (searchForDefaultAbstractMethod)
return findDefaultAbstractMethod(receiverType, selector, argumentTypes, invocationSite, classHierarchyStart, found, compatibleMethod);
unitScope.recordTypeReferences(compatibleMethod.thrownExceptions);
return compatibleMethod;
}
if (candidatesCount == 0)
candidates = new MethodBinding[foundSize];
candidates[candidatesCount++] = compatibleMethod;
} else if (problemMethod == null) {
problemMethod = compatibleMethod;
}
}
}
}
// no match was found
if (candidatesCount == 0) {
if (problemMethod != null) {
switch (problemMethod.problemId()) {
case ProblemReasons.TypeArgumentsForRawGenericMethod :
case ProblemReasons.TypeParameterArityMismatch :
return problemMethod;
}
}
// abstract classes may get a match in interfaces; for non abstract
// classes, reduces secondary errors since missing interface method
// error is already reported
MethodBinding interfaceMethod =
findDefaultAbstractMethod(receiverType, selector, argumentTypes, invocationSite, classHierarchyStart, found, null);
if (interfaceMethod != null) return interfaceMethod;
if (found.size == 0) return null;
if (problemMethod != null) return problemMethod;
// still no match; try to find a close match when the parameter
// order is wrong or missing some parameters
// see https://bugs.eclipse.org/bugs/show_bug.cgi?id=69471
// bad guesses are foo(), when argument types have been supplied
// and foo(X, Y), when the argument types are (int, float, Y)
// so answer the method with the most argType matches and least parameter type mismatches
int bestArgMatches = -1;
MethodBinding bestGuess = (MethodBinding) found.elementAt(0); // if no good match so just use the first one found
int argLength = argumentTypes.length;
foundSize = found.size;
nextMethod : for (int i = 0; i < foundSize; i++) {
MethodBinding methodBinding = (MethodBinding) found.elementAt(i);
TypeBinding[] params = methodBinding.parameters;
int paramLength = params.length;
int argMatches = 0;
next: for (int a = 0; a < argLength; a++) {
TypeBinding arg = argumentTypes[a];