info = info.makeChild(SourceOrigin.UNKNOWN);
// c_g_g_d_c_i_DOMImpl
toReturn =
new JMethod(info, requestType.replace("_", "_1").replace('.', '_'), holderType, program
.getTypeJavaLangObject().getNonNull(), false, true, true, AccessModifier.PUBLIC);
toReturn.setBody(new JMethodBody(info));
holderType.addMethod(toReturn);
toReturn.freezeParamTypes();
info.addCorrelation(info.getCorrelator().by(toReturn));
rebindMethods.put(requestType, toReturn);
// Used in the return statement at the end
JExpression mostUsedExpression = null;
JBlock switchBody = new JBlock(info);
for (int i = 0, j = resultTypes.size(); i < j; i++) {
String resultType = resultTypes.get(i);
JExpression instantiation = instantiationExpressions.get(i);
List<Integer> permutations = resultsToPermutations.get(resultType);
if (permutations == null) {
// This rebind result is unused in this permutation
continue;
} else if (resultType.equals(mostUsed)) {
// Save off the fallback expression and go onto the next type
mostUsedExpression = instantiation;
continue;
}
for (int permutationId : permutations) {
// case 33:
switchBody.addStmt(new JCaseStatement(info, program.getLiteralInt(permutationId)));
}
// return new FooImpl();
JReturnStatement ret = new JReturnStatement(info, instantiation);
switchBody.addStmt(ret);
}
assert switchBody.getStatements().size() > 0 : "No case statement emitted "
+ "for supposedly soft-rebind type " + requestType;
// switch (CollapsedPropertyHolder.getPermutationId()) { ... }
JSwitchStatement sw =
new JSwitchStatement(info, new JMethodCall(info, null, permutationIdMethod), switchBody);
// return new FallbackImpl(); at the very end.
assert mostUsedExpression != null : "No most-used expression";
JReturnStatement fallbackReturn = new JReturnStatement(info, mostUsedExpression);
JMethodBody body = (JMethodBody) toReturn.getBody();
body.getBlock().addStmt(sw);
body.getBlock().addStmt(fallbackReturn);
return toReturn;
}