Reviewers: scottb,

Description:
Allow enum ordinalization to work correctly for enum classes with no
values (i.e. empty enums).


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

Affected files:
  M dev/core/src/com/google/gwt/dev/jjs/impl/EnumOrdinalizer.java
  M dev/core/test/com/google/gwt/dev/jjs/impl/EnumOrdinalizerTest.java


Index: dev/core/src/com/google/gwt/dev/jjs/impl/EnumOrdinalizer.java
===================================================================
--- dev/core/src/com/google/gwt/dev/jjs/impl/EnumOrdinalizer.java (revision 10048) +++ dev/core/src/com/google/gwt/dev/jjs/impl/EnumOrdinalizer.java (working copy)
@@ -19,6 +19,7 @@
 import com.google.gwt.dev.jjs.SourceInfo;
 import com.google.gwt.dev.jjs.ast.Context;
 import com.google.gwt.dev.jjs.ast.JArrayType;
+import com.google.gwt.dev.jjs.ast.JBlock;
 import com.google.gwt.dev.jjs.ast.JCastOperation;
 import com.google.gwt.dev.jjs.ast.JClassLiteral;
 import com.google.gwt.dev.jjs.ast.JClassType;
@@ -42,6 +43,7 @@
 import com.google.gwt.dev.jjs.ast.JVariableRef;
 import com.google.gwt.dev.jjs.ast.js.JsniFieldRef;
 import com.google.gwt.dev.jjs.ast.js.JsniMethodRef;
+import com.google.gwt.dev.util.collect.Lists;
 import com.google.gwt.dev.util.log.speedtracer.CompilerEventType;
 import com.google.gwt.dev.util.log.speedtracer.SpeedTracerLogger;
 import com.google.gwt.dev.util.log.speedtracer.SpeedTracerLogger.Event;
@@ -790,8 +792,11 @@
      * clinit for an ordinalizable enum.
      */
     private void updateClinit(JMethod method) {
- List<JStatement> stmts = ((JMethodBody) method.getBody()).getStatements();
+      JBlock stmtBlock = ((JMethodBody) method.getBody()).getBlock();
+      List<JStatement> stmts = stmtBlock.getStatements();
       Iterator<JStatement> it = stmts.iterator();
+      List<Integer> toRemove = Lists.create();
+      int index = 0;
       // look for statements of the form EnumValueField = ...
       while (it.hasNext()) {
         JStatement stmt = it.next();
@@ -800,16 +805,19 @@
           if (ref instanceof JFieldRef) {
             JFieldRef enumRef = (JFieldRef) ref;
if (enumRef.getField().getEnclosingType() == method.getEnclosingType()) { - // see if LHS is a field ref that corresponds to the class of this
-              // method
+ // see if LHS is a field ref that corresponds to the class of this method
               if (enumRef.getField() instanceof JEnumField) {
-                it.remove();
+                toRemove = Lists.add(toRemove,index);
               } else if (enumRef.getField().getName().equals("$VALUES")) {
-                it.remove();
+                toRemove = Lists.add(toRemove,index);
               }
             }
           }
         }
+        index++;
+      }
+      for (int i = toRemove.size() - 1; i >= 0; i--) {
+        stmtBlock.removeStmt(toRemove.get(i));
       }
     }
   }
Index: dev/core/test/com/google/gwt/dev/jjs/impl/EnumOrdinalizerTest.java
===================================================================
--- dev/core/test/com/google/gwt/dev/jjs/impl/EnumOrdinalizerTest.java (revision 10048) +++ dev/core/test/com/google/gwt/dev/jjs/impl/EnumOrdinalizerTest.java (working copy)
@@ -225,6 +225,30 @@
     assertTrue(tracker.isOrdinalized("test.EntryPoint$Fruit"));
   }

+  public void testOrdinalizeUnusedEnum()
+      throws UnableToCompleteException  {
+    EnumOrdinalizer.resetTracker();
+
+    setupFruitEnum();
+
+    optimize("void", "Fruit myEnum;");
+
+    EnumOrdinalizer.Tracker tracker = EnumOrdinalizer.getTracker();
+    assertTrue(tracker.isOrdinalized("test.EntryPoint$Fruit"));
+  }
+
+  public void testOrdinalizeUnusedEmptyEnum()
+      throws UnableToCompleteException  {
+    EnumOrdinalizer.resetTracker();
+
+    setupEmptyEnum();
+
+    optimize("void", "EmptyEnum myEnum;");
+
+    EnumOrdinalizer.Tracker tracker = EnumOrdinalizer.getTracker();
+    assertTrue(tracker.isOrdinalized("test.EntryPoint$EmptyEnum"));
+  }
+
   public void testNotOrdinalizableClassLiteralReference()
       throws UnableToCompleteException  {
     EnumOrdinalizer.resetTracker();
@@ -796,6 +820,10 @@
     assertFalse(tracker.isOrdinalized("test.EntryPoint$Vegetable"));
   }

+  private void setupEmptyEnum() {
+    addSnippetClassDecl("public enum EmptyEnum {}");
+  }
+
   private void setupFruitEnum() {
     addSnippetClassDecl("public enum Fruit {APPLE, ORANGE}");
     setupExtraDummyEnum();


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

Reply via email to