private RFMethod findCompatibleMethod(final ErrorContext logger,
Type domainType, Method searchFor, boolean mustFind,
boolean allowOverloads, boolean boxReturnTypes) {
String methodName = searchFor.getName();
Type[] clientArgs = searchFor.getArgumentTypes();
Type clientReturnType = searchFor.getReturnType();
if (boxReturnTypes) {
clientReturnType = maybeBoxType(clientReturnType);
}
// Pull all methods out of the domain type
Map<String, List<RFMethod>> domainLookup = new LinkedHashMap<String, List<RFMethod>>();
for (RFMethod method : getMethodsInHierarchy(logger, domainType)) {
List<RFMethod> list = domainLookup.get(method.getName());
if (list == null) {
list = new ArrayList<RFMethod>();
domainLookup.put(method.getName(), list);
}
list.add(method);
}
// Find the matching method in the domain object
List<RFMethod> methods = domainLookup.get(methodName);
if (methods == null) {
if (mustFind) {
logger.poison("Could not find any methods named %s in %s", methodName,
print(domainType));
}
return null;
}
if (methods.size() > 1 && !allowOverloads) {
StringBuilder sb = new StringBuilder();
sb.append(String.format("Method overloads found in type %s named %s:\n",
print(domainType), methodName));
for (RFMethod method : methods) {
sb.append(" ").append(print(method)).append("\n");
}
logger.poison(sb.toString());
return null;
}
// Check each overloaded name
for (RFMethod domainMethod : methods) {
Type[] domainArgs = domainMethod.getArgumentTypes();
Type domainReturnType = domainMethod.getReturnType();
if (boxReturnTypes) {
/*
* When looking for the implementation of a Request<Integer>, we want to
* match either int or Integer, so we'll box the domain method's return
* type.