Revision: 7548
Author: sp...@google.com
Date: Tue Feb  9 11:36:21 2010
Log: Fixes a bug where switch(this) doesn't work if the type
of this is a non-null enum type.

http://code.google.com/p/google-web-toolkit/source/detail?r=7548

Modified:
 /trunk/dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaAST.java
 /trunk/user/test/com/google/gwt/dev/jjs/test/CompilerTest.java

=======================================
--- /trunk/dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaAST.java Mon Feb 8 08:29:30 2010 +++ /trunk/dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaAST.java Tue Feb 9 11:36:21 2010
@@ -1783,7 +1783,7 @@
     JStatement processStatement(SwitchStatement x) {
       SourceInfo info = makeSourceInfo(x);
       JExpression expression = dispProcessExpression(x.expression);
-      if (expression.getType() instanceof JClassType) {
+      if (isEnumType(expression.getType())) {
         // Must be an enum; synthesize a call to ordinal().
         expression = new JMethodCall(info, expression,
             program.getIndexedMethod("Enum.ordinal"));
@@ -2401,6 +2401,24 @@
               "Could not determine the desired box type");
       }
     }
+
+    /**
+     * Check whether the specified type is definitely for an enum class.
+     *
+     * @param type The type being tested
+     * @return whether it is certainly an enum
+     */
+    private boolean isEnumType(JType type) {
+      if (type instanceof JClassType) {
+        return ((JClassType) type).isEnumOrSubclass() != null;
+      }
+
+      if (type instanceof JNonNullType) {
+        return isEnumType(((JNonNullType) type).getUnderlyingType());
+      }
+
+      return false;
+    }

     private SourceInfo makeSourceInfo(Statement x) {
       int startLine = Util.getLineNumber(x.sourceStart,
=======================================
--- /trunk/user/test/com/google/gwt/dev/jjs/test/CompilerTest.java Mon Jun 15 17:51:24 2009 +++ /trunk/user/test/com/google/gwt/dev/jjs/test/CompilerTest.java Tue Feb 9 11:36:21 2010
@@ -66,6 +66,25 @@
private interface Bm2Listener<E extends Bm2BaseEvent> extends EventListener {
     int handleEvent(E be);
   }
+
+  /**
+   * Used in {...@link #testSwitchOnEnumTypedThis()}.
+   */
+  private static  enum ChangeDirection {
+    NEGATIVE, POSITIVE;
+
+    public String getText() {
+      switch (this) {
+        case POSITIVE:
+          return "POSITIVE";
+        case NEGATIVE:
+          return "NEGATIVE";
+        default:
+          throw new IllegalArgumentException("Unhandled change direction: "
+              + this);
+      }
+    }
+  }

   private static class ConcreteSub extends AbstractSuper {
     public static String foo() {
@@ -1065,7 +1084,12 @@
   public void testSubclassStaticInnerAndClinitOrdering() {
     new CheckSubclassStaticInnerAndClinitOrdering();
   }
-
+
+  public void testSwitchOnEnumTypedThis() {
+    assertEquals("POSITIVE", ChangeDirection.POSITIVE.getText());
+    assertEquals("NEGATIVE", ChangeDirection.NEGATIVE.getText());
+  }
+
   public void testSwitchStatement() {
     switch (0) {
       case 0:

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

Reply via email to