Thursday, April 2, 2015

Deferred binding consultation

Hi All,

I am trying to use deferred binding to generate implementation in run-time and encountered with one problem. I created pretty simple generator and use GWT.create to instantiate a class. I am expecting GWT will provide class generated by my generator and it works as expected in dev mode. When I am trying to make same in production, for some reason GWT does not provide generated version. I have no idea what is wrong, maybe somebody have any ideas? Thanks in advance!  
Class, modified by generator
public class GeneratedInterface {

public String getValue(){
return null;
}
}

Generator
public String generate(TreeLogger logger, GeneratorContext ctx, String requestedClass) throws UnableToCompleteException {
JClassType type = ctx.getTypeOracle().findType(requestedClass);
String packageName = type.getPackage().getName();
String simpleName = type.getSimpleSourceName() + "_Generated";
logger.log(TreeLogger.Type.INFO, "Create adapter " + packageName + "." + simpleName + " for " + type.getQualifiedSourceName());
ClassSourceFileComposerFactory composer = new ClassSourceFileComposerFactory(packageName, simpleName);
composer.setSuperclass(GeneratedInterface.class.getName());
PrintWriter printWriter = ctx.tryCreate(logger, packageName, simpleName);
String className = null;
if (printWriter != null) {
SourceWriter writer = composer.createSourceWriter(ctx, printWriter);
writer.println("public String getValue() {");
writer.println(" return \"Test interface\";");
writer.println("}");
writer.commit(logger);
className = composer.getCreatedClassName();
}
return className;
}

EntryPoint 
public class GeneratorTest implements EntryPoint {

public void onModuleLoad() {
GeneratedInterface intf = GWT.create(GeneratedInterface.class);
log.info("deferred binding: " + intf.getClass().getName());
log.info("Value = " + intf.getValue());
    }
}

module.xml
<generate-with class="com.xxx.xxx.gwt.generator.server.TestGenerator">
<when-type-assignable class="com.xxx.xxx.gwt.generator.client.GeneratedInterface"/>
</generate-with>

As you can see, generator just provides implementation for GeneratedInterface.getValue() 

In case of dev mode, in logs I GWT uses generated class 
Thu Apr 02 15:14:23 MSK 2015 com.hp.usage.gwt.generator.client.GeneratorTest INFO: deferred binding: com.hp.usage.gwt.generator.client.GeneratedInterface_Generated
Thu Apr 02 15:14:23 MSK 2015 com.hp.usage.gwt.generator.client.GeneratorTest INFO: Value = Test interface

In case of production mode, in logs I GWT does not use generated class 
Thu Apr 02 15:15:53 GMT+300 2015 com.hp.usage.gwt.generator.client.GeneratorTest INFO: deferred binding: com.hp.usage.gwt.generator.client.GeneratedInterface
Thu Apr 02 15:15:53 GMT+300 2015 com.hp.usage.gwt.generator.client.GeneratorTest INFO: Value = null







--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at http://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment