Reviewers: robertvawter,

Description:
Modifying ClientBundle to create resources lazily, which allows the
compiler to dead strip unused resources.


Please review this at http://gwt-code-reviews.appspot.com/1368803/

Affected files:
M user/src/com/google/gwt/resources/rebind/context/AbstractClientBundleGenerator.java


Index: user/src/com/google/gwt/resources/rebind/context/AbstractClientBundleGenerator.java
===================================================================
--- user/src/com/google/gwt/resources/rebind/context/AbstractClientBundleGenerator.java (revision 9785) +++ user/src/com/google/gwt/resources/rebind/context/AbstractClientBundleGenerator.java (working copy)
@@ -51,7 +51,6 @@
 import com.google.gwt.resources.rg.BundleResourceGenerator;
 import com.google.gwt.user.rebind.ClassSourceFileComposerFactory;
 import com.google.gwt.user.rebind.SourceWriter;
-import com.google.gwt.user.rebind.StringSourceWriter;

 import java.io.File;
 import java.io.IOException;
@@ -76,17 +75,14 @@
  *
  * <pre>
  * public ResourceType resource() {
+ *   if (resource == null) {
+ *     resource = new Resource();
+ *   }
  *   return resource;
- * }
- * private void _init0() {
- *   resource = new Resource();
  * }
  * // Other ResourceGenerator-defined fields
  * private static ResourceType resource;
  * private static HashMap&lt;String, ResourcePrototype&gt; resourceMap;
- * static {
- *   new ClientBundle()._init0();
- * }
  * public ResourcePrototype[] getResources() {
  *   return new ResourcePrototype[] { resource() };
  * }
@@ -439,12 +435,6 @@
       // Print the accumulated field definitions
       sw.println(fields.getCode());

-      // Print the static initializer after the fields
-      sw.println("static {");
- sw.indentln("new " + resourceContext.getImplementationSimpleSourceName()
-          + "()._init0();");
-      sw.println("}");
-
       /*
        * The map-accessor methods use JSNI and need a fully-qualified class
        * name, but should not include any sub-bundles.
@@ -736,7 +726,7 @@
    */
   private boolean createFieldsAndAssignments(TreeLogger logger,
       AbstractResourceContext resourceContext, ResourceGenerator rg,
-      List<JMethod> generatorMethods, SourceWriter sw, SourceWriter init,
+      List<JMethod> generatorMethods, SourceWriter sw,
       ClientBundleFields fields) {

     // Defer failure until this phase has ended
@@ -773,11 +763,14 @@
       // Strip off all but the access modifiers
       sw.print(m.getReadableDeclaration(false, true, true, true, true));
       sw.println(" {");
+      sw.indent();
+      // Record the initialization statement for first time access.
+      sw.println("if (" + ident + " == null) {");
+      sw.indentln(ident + " = " + rhs + ";");
+      sw.println("}");
+      sw.outdent();
       sw.indentln("return " + ident + ";");
       sw.println("}");
-
-      // Record the initialization statement for the one-shot init() method
-      init.println(ident + " = " + rhs + ";");
     }

     if (fail) {
@@ -797,20 +790,11 @@
     // Try to provide as many errors as possible before failing.
     boolean success = true;

-    SourceWriter init = new StringSourceWriter();
-    init.println("private void _init0() {");
-    init.indent();
-
// Run the ResourceGenerators to generate implementations of the methods for (Map.Entry<ResourceGenerator, List<JMethod>> entry : generators.entrySet()) {
       success &= createFieldsAndAssignments(logger, resourceContext,
-          entry.getKey(), entry.getValue(), sw, init, fields);
-    }
-
-    init.outdent();
-    init.println("}");
-
-    sw.println(init.toString());
+          entry.getKey(), entry.getValue(), sw, fields);
+    }

     if (!success) {
       throw new UnableToCompleteException();


--
http://groups.google.com/group/Google-Web-Toolkit-Contributors

Reply via email to