private MethodAccess createMethodAccessForTarget(String accessTarget, boolean passInstance)
{
boolean isVoid = sig.getReturnType().equals("void");
BodyBuilder builder = new BodyBuilder().begin();
builder.addln("%s instance = (%<s) $1;", getClassName());
builder.addln("try").begin();
if (!isVoid)
{
builder.add("return success(($w) ");
}
// Call the target, even if the eventual method is void
builder.add(accessTarget);
builder.add("(");
if (passInstance)
builder.add("instance");
int p = 0;
for (String type : sig.getParameterTypes())
{
if (passInstance || p != 0)
builder.add(", ");
String ref = String.format("$2[%d]", p++);
builder.add(ClassFabUtils.castReference(ref, type));
}
// Balance the call to success()
if (!isVoid)
builder.add(")");
builder.addln(");");
if (isVoid)
builder.addln("return success(null);");
builder.end(); // try
builder.addln("catch (java.lang.RuntimeException ex) { throw ex; }");
builder.addln("catch (java.lang.Exception ex) { return fail(ex); }");
builder.end();
return instantiateMethodAccessFromBody(builder.toString());
}