}
protected void writeConstructor(FragmentGeneratorContext context,
JMethod constructor) throws UnableToCompleteException {
TreeLogger logger = context.parentLogger.branch(TreeLogger.DEBUG,
"Writing constructor " + constructor.getName(), null);
SourceWriter sw = context.sw;
JParameter[] parameters = constructor.getParameters();
if (parameters == null) {
parameters = new JParameter[0];
}
// Method declaration
sw.print("public native ");
sw.print(constructor.getReturnType().getQualifiedSourceName());
sw.print(" ");
sw.print(constructor.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.
JType returnType = constructor.getReturnType();
sw.print("var jsReturn = ");
// If the imported method is acting as an invocation of a JavaScript
// constructor, use the new Foo() syntax, otherwise treat is an an
// invocation on a field on the underlying JSO.
sw.print("new ");
Constructor constructorAnnotation = hasTag(logger, constructor,
Constructor.class);
sw.print(constructorAnnotation.value());
// 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);