logger.log(TreeLogger.DEBUG,
"No rules are defined, so no substitution can occur", null);
return null;
}
Rule minCostRuleSoFar = null;
for (Iterator<Rule> iter = rules.iterator(); iter.hasNext();) {
Rule rule = iter.next();
// Branch the logger.
//
TreeLogger branch = Messages.TRACE_CHECKING_RULE.branch(logger, rule,
null);
if (rule.isApplicable(branch, genCtx, typeName)) {
// See if this rule has already been used. This is needed to prevent
// infinite loops with 'when-assignable' conditions.
//
if (!usedRules.contains(rule)) {
usedRules.add(rule);
Messages.TRACE_RULE_MATCHED.log(logger, null);
return rule;
} else {
// We are skipping this rule because it has already been used
// in a previous iteration.
//
}
} else {
Messages.TRACE_RULE_DID_NOT_MATCH.log(logger, null);
// keep track of fallback partial matches
if (minCostRuleSoFar == null) {
minCostRuleSoFar = rule;
}
assert rule.getFallbackEvaluationCost() != 0;
// if we found a better match, keep that as the best candidate so far
if (rule.getFallbackEvaluationCost() <= minCostRuleSoFar.getFallbackEvaluationCost()) {
logger.log(TreeLogger.DEBUG, "Found better fallback match for " + rule);
minCostRuleSoFar = rule;
}
}
}