Hello,

at the moment I'm playing around a bit with GWT.create and
the generate-with-directive in the GWT.xml-file to be able
to generate classes used for deferred bindings. I have the
following directive in my XML-file:

<generate-with class="mypackage.rebind.AboutDataGenerator">
  <when-type-assignable class="mypackage.data.AboutData"/>
</generate-with>

The generate-method in AboutDataGenerator is here:

public String generate(TreeLogger logger, GeneratorContext context, String 
typeName) throws UnableToCompleteException {
  logger.log(TreeLogger.INFO, "Creating AboutData-class with 
build-informations", null);
  TypeOracle oracle = context.getTypeOracle();
  JClassType requestedClass = oracle.findType(typeName);
  String packageName = requestedClass.getPackage().getName();
  String simpleName = requestedClass.getSimpleSourceName();
  String proxyName = simpleName + "Proxy";
  String proxyClassname = packageName + "." + proxyName;

  PrintWriter pw = context.tryCreate(logger, packageName, proxyName);
  ClassSourceFileComposerFactory factory = new 
ClassSourceFileComposerFactory(packageName, proxyName);

  factory.setSuperclass(simpleName);
  factory.addImport("java.util.Date");

  SourceWriter source = factory.createSourceWriter(context, pw);
  source.println("public " + proxyName + "(){");
  source.indent();
  source.println("buildDate = new Date(" + 
Long.toString(System.currentTimeMillis()) + "L);");
  source.println("isBuiltVersion = \"" + escape(Version.getImplVersion()) + 
"\";");
  source.println("isSvnRevision = " + Integer.toString(Version.REVISION) + ";");
  source.outdent();
  source.println("}");
  source.commit(logger);
  return proxyClassname;
}

The whole thing works in hosted mode, i.e. my About-Dialog happily
shows the build-date and version of the server the build was created
with. The problem starts, wenn I create another dialog leading to a
new call of GWT.create(AboutData.class). Instead of using the already
created class, a new attempt of creation is done leading to the
return of null when trying to get the PrintWriter. This lead to
a NullPointerException when doing the creation of the SourceFactory.

I tried to check if the proxy-class already exist with inserting

  try{
    Class.forName(proxyClassname);
    return proxyClassname;
  }
  catch(ClassNotFoundException cnfe){
    // nothing to do
  }

before creating the PrintWriter but the class is not known and
the creation-process starts again leading to the same exception.

Here is the exception I get in that case:

[ERROR] Uncaught exception escaped
java.lang.RuntimeException: Deferred binding failed for 
'mypackage.data.AboutData' (did you forget to inherit a required module?)
        at 
com.google.gwt.dev.shell.JavaScriptHost.rebindAndCreate(JavaScriptHost.java:157)
        at com.google.gwt.dev.shell.ShellGWT.create(ShellGWT.java:24)
        at com.google.gwt.core.client.GWT.create(transient source for 
com.google.gwt.core.client.GWT:47)
        at mypackage.client.general.AboutPanel.startup(AboutPanel.java:88)
        at mypackage.client.MainPanel$6.onTreeItemSelected(MainPanel.java:294)
        at 
com.google.gwt.user.client.ui.TreeListenerCollection.fireItemSelected(TreeListenerCollection.java:36)
        at com.google.gwt.user.client.ui.Tree.onSelection(Tree.java:836)
        at com.google.gwt.user.client.ui.Tree.elementClicked(Tree.java:701)
        at com.google.gwt.user.client.ui.Tree.onBrowserEvent(Tree.java:361)
        at com.google.gwt.user.client.DOM.dispatchEventImpl(DOM.java:1265)
Caused by: java.lang.NullPointerException: null
        at 
com.google.gwt.user.rebind.ClassSourceFileComposer.print(ClassSourceFileComposer.java:144)
        at 
com.google.gwt.user.rebind.ClassSourceFileComposer.println(ClassSourceFileComposer.java:159)
        at 
com.google.gwt.user.rebind.ClassSourceFileComposer.<init>(ClassSourceFileComposer.java:61)
        at 
com.google.gwt.user.rebind.ClassSourceFileComposerFactory.createSourceWriter(ClassSourceFileComposerFactory.java:98)
        at 
mypackage.rebind.AboutDataGenerator.generate(AboutDataGenerator.java:59)
        at 
com.google.gwt.dev.cfg.RuleGenerateWith.realize(RuleGenerateWith.java:43)
        at 
com.google.gwt.dev.shell.StandardRebindOracle$Rebinder.tryRebind(StandardRebindOracle.java:116)
        at 
com.google.gwt.dev.shell.StandardRebindOracle$Rebinder.rebind(StandardRebindOracle.java:61)
        at 
com.google.gwt.dev.shell.StandardRebindOracle.rebind(StandardRebindOracle.java:173)
        at 
com.google.gwt.dev.shell.ShellModuleSpaceHost.rebind(ShellModuleSpaceHost.java:127)

I solved this specific problem by assigning the AboutDataProxy-class
to a static member of the dialog, so the call only happens once now,
but this is just a test (for mastering the learning curve) and there
are other uses where I can't do that that easy.

How is the correct way?


Regards, Lothar

--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to