Wednesday, December 29, 2010

Re: Can I generate a call to GWT.create(...) within generated code

You can... note that for each GWT.create() call, all sources it refers to must exist when you make the call.
So you need to call commit() on your SourceWriter once you have the first class/intf ready to be used in subsequent GWT.create() calls.

Here's a sample code that I use to create an ImageBundle out of my annotations and use it in a subsequent GWT.create() call :

public class WorkflowDataGenerator extends Generator {

/* (non-Javadoc)
* @see com.google.gwt.core.ext.Generator#generate(com.google.gwt.core.ext.TreeLogger, com.google.gwt.core.ext.GeneratorContext, java.lang.String)
*/
@Override
public String generate(TreeLogger logger, GeneratorContext context, String typeName) throws UnableToCompleteException {
TypeOracle typeOracle = context.getTypeOracle();
JClassType classType = typeOracle.findType(typeName);
String packageName = classType.getPackage().getName(); 

JMethod [] methodsInIntf = classType.getMethods();

/*
* Generate code for an ImageBundle out of the images specified in annotation
* This is then utilized when generating code for Image Array below
*/
String classNameWIImgBndl = classType.getName() + "ImageBundle";
PrintWriter pwImgBndl = context.tryCreate(logger, packageName, classNameWIImgBndl);
if(pwImgBndl != null){
logger.log(TreeLogger.INFO, "Generating "+packageName+"."+classNameWIImgBndl);
ClassSourceFileComposerFactory factoryImgBndl = new ClassSourceFileComposerFactory(packageName, classNameWIImgBndl);
factoryImgBndl.makeInterface();
factoryImgBndl.addImport("com.google.gwt.resources.client.ClientBundle");
factoryImgBndl.addImport("com.google.gwt.resources.client.ImageResource");
factoryImgBndl.addImplementedInterface("com.google.gwt.resources.client.ClientBundle");

SourceWriter sw = factoryImgBndl.createSourceWriter(context, pwImgBndl);
sw.indent();

for (JMethod method: methodsInIntf){
logger.log(TreeLogger.INFO, "Preparing method "+method.getName());
                                /* Several calls to sw.println() to emit source as per my need */
sw.println();
}

sw.commit(logger);
}
                /* More code emitted as per my need*/
                ........
                ........
                /*
 * Add code for generating the WorkInstruction Image Array
 */
sw.println("public ImageResource [] createImageArray () {");
sw.indent();
sw.println("ImageResource[] imgArr = new ImageResource ["+(methodsInIntf.length+1)+"];");
sw.println(packageName+"."+classNameWIImgBndl +" wiBundle = GWT.create("+packageName+"."+classNameWIImgBndl+".class);");

int counter=1;
for(JMethod method: methodsInIntf){
sw.println("imgArr["+(counter++)+"] = wiBundle."+method.getName()+"();");
}
sw.println("return imgArr;");
sw.outdent();
sw.println("}");

sw.commit(logger);
return packageName+"."+classNameWIData;
} else {
return packageName+"."+classNameWIData;
}
}
}


Regards,
Nirmal

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment