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,, java.lang.String)
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);

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

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

                /* More code emitted as per my need*/
 * Add code for generating the WorkInstruction Image Array
sw.println("public ImageResource [] createImageArray () {");
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;");

return packageName+"."+classNameWIData;
} else {
return packageName+"."+classNameWIData;


You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to
To unsubscribe from this group, send email to
For more options, visit this group at

No comments:

Post a Comment