protected void writeImported(FragmentGeneratorContext context,
JMethod imported) throws UnableToCompleteException {
TreeLogger logger = context.parentLogger.branch(TreeLogger.DEBUG,
"Writing import " + imported.getName(), null);
SourceWriter sw = context.sw;
// Simplifies the rest of writeImported
JParameter[] parameters = imported.getParameters();
if (parameters == null) {
parameters = new JParameter[0];
}
// Method declaration
sw.print("public native ");
sw.print(imported.getReturnType().getQualifiedSourceName());
sw.print(" ");
sw.print(imported.getName());
sw.print("(");
for (int i = 0; i < parameters.length; i++) {
JType returnType = parameters[i].getType();
JParameterizedType pType = returnType.isParameterized();
if (pType != null) {
sw.print(pType.getRawType().getQualifiedSourceName());
} else {
sw.print(returnType.getQualifiedSourceName());
}
sw.print(" ");
sw.print(parameters[i].getName());
if (i < parameters.length - 1) {
sw.print(", ");
}
}
sw.print(")");
sw.println(" /*-{");
sw.indent();
// The return type of the function we're importing.
final JType returnType = imported.getReturnType();
// Don't bother recording a return value for void invocations.
if (!JPrimitiveType.VOID.equals(returnType.isPrimitive())) {
sw.print("var jsReturn = ");
}
sw.print(context.objRef);
sw.print(".");
sw.print(context.fieldName);
// Write the invocation's parameter list
sw.print("(");
for (int i = getImportOffset(); i < parameters.length; i++) {
// Create a sub-context to generate the wrap/unwrap logic
JType subType = parameters[i].getType();
FragmentGeneratorContext subParams = new FragmentGeneratorContext(context);
subParams.returnType = subType;
subParams.parameterName = parameters[i].getName();
FragmentGenerator fragmentGenerator = context.fragmentGeneratorOracle.findFragmentGenerator(
logger, context.typeOracle, subType);
if (fragmentGenerator == null) {
logger.log(TreeLogger.ERROR, "No fragment generator for "
+ returnType.getQualifiedSourceName(), null);
throw new UnableToCompleteException();
}
fragmentGenerator.toJS(subParams);
if (i < parameters.length - 1) {
sw.print(", ");
}
}
sw.println(");");
// Wrap the return type in the correct Java type. Void returns are ignored
if (!JPrimitiveType.VOID.equals(returnType.isPrimitive())) {
FragmentGeneratorContext subContext = new FragmentGeneratorContext(
context);
subContext.returnType = returnType;
subContext.parameterName = "jsReturn";
FragmentGenerator fragmentGenerator = FRAGMENT_ORACLE.findFragmentGenerator(
logger, context.typeOracle, returnType);
sw.print("return ");
fragmentGenerator.fromJS(subContext);
sw.println(";");
}
sw.outdent();
sw.println("}-*/;");
}