Implements Class.desiredAssertionStatus, which is also required by 
Sun's compiler to compile assert statements.

Patch by: tobyr
Review by: scottb
Issue: 2039



git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@1751 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/dev/core/src/com/google/gwt/dev/jjs/JavaToJavaScriptCompiler.java b/dev/core/src/com/google/gwt/dev/jjs/JavaToJavaScriptCompiler.java
index 0ab0875..1812878 100644
--- a/dev/core/src/com/google/gwt/dev/jjs/JavaToJavaScriptCompiler.java
+++ b/dev/core/src/com/google/gwt/dev/jjs/JavaToJavaScriptCompiler.java
@@ -302,7 +302,8 @@
       //
 
       // Create the tree from JDT
-      GenerateJavaAST.exec(allTypeDeclarations, typeMap, jprogram, jsProgram);
+      GenerateJavaAST.exec(allTypeDeclarations, typeMap, jprogram, jsProgram,
+          options.isEnableAssertions());
 
       // GenerateJavaAST can uncover semantic JSNI errors; report & abort
       // 
diff --git a/dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaAST.java b/dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaAST.java
index 5e742ed..a11bcc5 100644
--- a/dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaAST.java
+++ b/dev/core/src/com/google/gwt/dev/jjs/impl/GenerateJavaAST.java
@@ -404,6 +404,8 @@
     private MethodScope currentMethodScope;
 
     private int[] currentSeparatorPositions;
+    
+    private boolean enableAsserts;
 
     private final JsProgram jsProgram;
 
@@ -414,10 +416,11 @@
     private final TypeMap typeMap;
 
     public JavaASTGenerationVisitor(TypeMap typeMap, JProgram program,
-        JsProgram jsProgram) {
+        JsProgram jsProgram, boolean enableAsserts) {
       this.typeMap = typeMap;
       this.program = program;
       this.jsProgram = jsProgram;
+      this.enableAsserts = enableAsserts;
     }
 
     public void processEnumType(JEnumType type) {
@@ -534,6 +537,24 @@
               new JReturnStatement(program, null, classLit));
         }
 
+        // Implement Class.desiredAssertionStatus
+        if (currentClass == program.getTypeJavaLangClass()) {
+          JMethod method =
+              program.getIndexedMethod("Class.desiredAssertionStatus");
+          assert method != null;
+          JMethodBody body = (JMethodBody) method.getBody();
+          List<JStatement> statements = body.getStatements();
+
+          // There must always be at least 1 statement, because the method 
+          // has a non-void return type.
+          assert statements.size() > 0;
+
+          SourceInfo info = statements.get(0).getSourceInfo();
+          statements.clear();
+          statements.add(new JReturnStatement(program, info,
+              program.getLiteralBoolean(enableAsserts)));
+        }
+
         if (currentClass instanceof JEnumType) {
           processEnumType((JEnumType) currentClass);
         }
@@ -2603,9 +2624,9 @@
    * a JProgram structure.
    */
   public static void exec(TypeDeclaration[] types, TypeMap typeMap,
-      JProgram jprogram, JsProgram jsProgram) {
+      JProgram jprogram, JsProgram jsProgram, boolean enableAsserts) {
     JavaASTGenerationVisitor v = new JavaASTGenerationVisitor(typeMap,
-        jprogram, jsProgram);
+        jprogram, jsProgram, enableAsserts);
     for (int i = 0; i < types.length; ++i) {
       v.processType(types[i]);
     }
diff --git a/user/super/com/google/gwt/emul/java/lang/Class.java b/user/super/com/google/gwt/emul/java/lang/Class.java
index 97b2468..807358d 100644
--- a/user/super/com/google/gwt/emul/java/lang/Class.java
+++ b/user/super/com/google/gwt/emul/java/lang/Class.java
@@ -117,6 +117,12 @@
   private Class() {
   }
 
+  public boolean desiredAssertionStatus() {
+    // This body is ignored by the JJS compiler and a new one is 
+    // synthesized at compile-time based on the actual compilation arguments.  
+    return false;
+  }
+
   public native T[] getEnumConstants() /*-{
     return this.@java.lang.Class::enumConstantsFunc
         && (this.@java.lang.Class::enumConstantsFunc)();
diff --git a/user/test/com/google/gwt/dev/jjs/test/ClassObjectTest.java b/user/test/com/google/gwt/dev/jjs/test/ClassObjectTest.java
index 1391ea2..35342dc 100644
--- a/user/test/com/google/gwt/dev/jjs/test/ClassObjectTest.java
+++ b/user/test/com/google/gwt/dev/jjs/test/ClassObjectTest.java
@@ -67,6 +67,16 @@
     assertEquals(Foo[].class, f[0].getClass());
   }
 
+  public void testAssertionStatus() {
+    boolean assertionStatus = ClassObjectTest.class.desiredAssertionStatus(); 
+    try {
+      assert false;
+      assertFalse(assertionStatus);
+    } catch (AssertionError e) {
+      assertTrue(assertionStatus);        
+    }
+  }
+  
   public void testClass() {
     Object o = new Foo();
     assertEquals(Foo.class, o.getClass());
diff --git a/user/test/com/google/gwt/dev/jjs/test/CoverageTest.java b/user/test/com/google/gwt/dev/jjs/test/CoverageTest.java
index e8ee773..7e14d64 100644
--- a/user/test/com/google/gwt/dev/jjs/test/CoverageTest.java
+++ b/user/test/com/google/gwt/dev/jjs/test/CoverageTest.java
@@ -151,13 +151,9 @@
     private void testAssertStatement() {
       // AssertStatement
 
-      // See if assertions are even enabled.
-      try {
-        assert false;
-        // Assertions are not on, early out.
+      // Only test asserts if they're enabled
+      if (! CoverageTest.class.desiredAssertionStatus()) {
         return;
-      } catch (AssertionError e) {
-        // Assertions are on, run the test.
       }
 
       i = 1;