Decentralize nullmethod/nullfield.

http://gwt-code-reviews.appspot.com/1442802/

Review by: jbrosenberg@google.com

git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@10183 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/dev/core/src/com/google/gwt/dev/jjs/ast/JField.java b/dev/core/src/com/google/gwt/dev/jjs/ast/JField.java
index 412198f..9eb6b29 100644
--- a/dev/core/src/com/google/gwt/dev/jjs/ast/JField.java
+++ b/dev/core/src/com/google/gwt/dev/jjs/ast/JField.java
@@ -67,6 +67,17 @@
     }
   }
 
+  private static class ExternalSerializedNullField implements Serializable {
+    public static final ExternalSerializedNullField INSTANCE = new ExternalSerializedNullField();
+
+    private Object readResolve() {
+      return NULL_FIELD;
+    }
+  }
+
+  public static final JField NULL_FIELD = new JField(SourceOrigin.UNKNOWN, "nullField", null,
+      JNullType.INSTANCE, false, Disposition.FINAL);
+
   private final JDeclaredType enclosingType;
   private final boolean isCompileTimeConstant;
   private final boolean isStatic;
@@ -153,6 +164,8 @@
   protected Object writeReplace() {
     if (enclosingType != null && enclosingType.isExternal()) {
       return new ExternalSerializedForm(this);
+    } else if (this == NULL_FIELD) {
+      return ExternalSerializedNullField.INSTANCE;
     } else {
       return this;
     }
diff --git a/dev/core/src/com/google/gwt/dev/jjs/ast/JMethod.java b/dev/core/src/com/google/gwt/dev/jjs/ast/JMethod.java
index 4b5172e..faa14bc 100644
--- a/dev/core/src/com/google/gwt/dev/jjs/ast/JMethod.java
+++ b/dev/core/src/com/google/gwt/dev/jjs/ast/JMethod.java
@@ -55,8 +55,24 @@
     }
   }
 
+  private static class ExternalSerializedNullMethod implements Serializable {
+    public static final ExternalSerializedNullMethod INSTANCE = new ExternalSerializedNullMethod();
+
+    private Object readResolve() {
+      return NULL_METHOD;
+    }
+  }
+
+  public static final JMethod NULL_METHOD = new JMethod(SourceOrigin.UNKNOWN, "nullMethod", null,
+      JNullType.INSTANCE, false, false, true, false);
+
   private static final String TRACE_METHOD_WILDCARD = "*";
 
+  static {
+    NULL_METHOD.setSynthetic();
+    NULL_METHOD.freezeParamTypes();
+  }
+
   private static void trace(String title, String code) {
     System.out.println("---------------------------");
     System.out.println(title + ":");
@@ -352,6 +368,8 @@
   protected Object writeReplace() {
     if (enclosingType != null && enclosingType.isExternal()) {
       return new ExternalSerializedForm(this);
+    } else if (this == NULL_METHOD) {
+      return ExternalSerializedNullMethod.INSTANCE;
     } else {
       return this;
     }
diff --git a/dev/core/src/com/google/gwt/dev/jjs/ast/JProgram.java b/dev/core/src/com/google/gwt/dev/jjs/ast/JProgram.java
index ca36e85..7af2bd8 100644
--- a/dev/core/src/com/google/gwt/dev/jjs/ast/JProgram.java
+++ b/dev/core/src/com/google/gwt/dev/jjs/ast/JProgram.java
@@ -335,10 +335,6 @@
 
   private final Map<JMethod, JMethod> instanceToStaticMap = new IdentityHashMap<JMethod, JMethod>();
 
-  private JField nullField;
-
-  private JMethod nullMethod;
-
   private Map<JReferenceType, Integer> queryIds;
 
   /**
@@ -855,22 +851,11 @@
   }
 
   public JField getNullField() {
-    if (nullField == null) {
-      nullField =
-          new JField(createSourceInfoSynthetic(JProgram.class), "nullField", null,
-              JNullType.INSTANCE, false, Disposition.FINAL);
-    }
-    return nullField;
+    return JField.NULL_FIELD;
   }
 
   public JMethod getNullMethod() {
-    if (nullMethod == null) {
-      nullMethod =
-          new JMethod(createSourceInfoSynthetic(JProgram.class), "nullMethod", null,
-              JNullType.INSTANCE, false, false, true, false);
-      nullMethod.setSynthetic();
-    }
-    return nullMethod;
+    return JMethod.NULL_METHOD;
   }
 
   public int getQueryId(JReferenceType elementType) {
diff --git a/dev/core/src/com/google/gwt/dev/jjs/ast/js/JsniClassLiteral.java b/dev/core/src/com/google/gwt/dev/jjs/ast/js/JsniClassLiteral.java
index c26a92d..b032731 100644
--- a/dev/core/src/com/google/gwt/dev/jjs/ast/js/JsniClassLiteral.java
+++ b/dev/core/src/com/google/gwt/dev/jjs/ast/js/JsniClassLiteral.java
@@ -28,6 +28,7 @@
 
   public JsniClassLiteral(SourceInfo info, String ident, JType type) {
     super(info, type);
+    assert ident != null;
     this.ident = ident;
   }
 
diff --git a/dev/core/src/com/google/gwt/dev/jjs/ast/js/JsniFieldRef.java b/dev/core/src/com/google/gwt/dev/jjs/ast/js/JsniFieldRef.java
index 862b9c3..35bbbba 100644
--- a/dev/core/src/com/google/gwt/dev/jjs/ast/js/JsniFieldRef.java
+++ b/dev/core/src/com/google/gwt/dev/jjs/ast/js/JsniFieldRef.java
@@ -34,6 +34,7 @@
   public JsniFieldRef(SourceInfo info, String ident, JField field, JDeclaredType enclosingType,
       boolean isLvalue) {
     super(info, field.isStatic() ? null : JNullLiteral.INSTANCE, field, enclosingType);
+    assert ident != null;
     this.ident = ident;
     this.isLvalue = isLvalue;
   }
diff --git a/dev/core/src/com/google/gwt/dev/jjs/ast/js/JsniMethodRef.java b/dev/core/src/com/google/gwt/dev/jjs/ast/js/JsniMethodRef.java
index 189390b..b72ec33 100644
--- a/dev/core/src/com/google/gwt/dev/jjs/ast/js/JsniMethodRef.java
+++ b/dev/core/src/com/google/gwt/dev/jjs/ast/js/JsniMethodRef.java
@@ -35,6 +35,7 @@
   public JsniMethodRef(SourceInfo info, String ident, JMethod method, JClassType jsoType) {
     // Just use a null literal as the qualifier on a non-static method
     super(info, method.isStatic() ? null : JNullLiteral.INSTANCE, method);
+    assert ident != null;
     this.ident = ident;
     this.jsoType = jsoType;
   }
diff --git a/dev/core/src/com/google/gwt/dev/jjs/impl/GwtAstBuilder.java b/dev/core/src/com/google/gwt/dev/jjs/impl/GwtAstBuilder.java
index c58dfa3..3a0559f 100644
--- a/dev/core/src/com/google/gwt/dev/jjs/impl/GwtAstBuilder.java
+++ b/dev/core/src/com/google/gwt/dev/jjs/impl/GwtAstBuilder.java
@@ -249,6 +249,12 @@
           SourceInfo info = x.getSourceInfo();
           if (binding == null) {
             assert ident.startsWith("@null::");
+            if ("@null::nullMethod()".equals(ident)) {
+              processMethod(x, info, JMethod.NULL_METHOD);
+            } else {
+              assert "@null::nullField".equals(ident);
+              processField(x, info, JField.NULL_FIELD, ctx);
+            }
           } else if (binding instanceof TypeBinding) {
             JType type = typeMap.get((TypeBinding) binding);
             processClassLiteral(x, info, type, ctx);
diff --git a/dev/core/src/com/google/gwt/dev/jjs/impl/ImplementClassLiteralsAsFields.java b/dev/core/src/com/google/gwt/dev/jjs/impl/ImplementClassLiteralsAsFields.java
index a391c53..174f526 100644
--- a/dev/core/src/com/google/gwt/dev/jjs/impl/ImplementClassLiteralsAsFields.java
+++ b/dev/core/src/com/google/gwt/dev/jjs/impl/ImplementClassLiteralsAsFields.java
@@ -77,6 +77,19 @@
     normalizerEvent.end();
   }
 
+  private static String createIdent(JMethod method) {
+    StringBuilder sb = new StringBuilder();
+    sb.append(method.getEnclosingType().getName());
+    sb.append("::");
+    sb.append(method.getName());
+    sb.append('(');
+    for (JType type : method.getOriginalParamTypes()) {
+      sb.append(type.getJsniSignatureName());
+    }
+    sb.append(')');
+    return sb.toString();
+  }
+
   private static String getClassName(String fullName) {
     int pos = fullName.lastIndexOf(".");
     return fullName.substring(pos + 1);
@@ -223,8 +236,10 @@
         if (valueOfMethod == null) {
           throw new InternalCompilerException("Could not find enum valueOf() method");
         }
-        call.addArg(new JsniMethodRef(info, null, valuesMethod, program.getJavaScriptObject()));
-        call.addArg(new JsniMethodRef(info, null, valueOfMethod, program.getJavaScriptObject()));
+        call.addArg(new JsniMethodRef(info, createIdent(valuesMethod), valuesMethod, program
+            .getJavaScriptObject()));
+        call.addArg(new JsniMethodRef(info, createIdent(valueOfMethod), valueOfMethod, program
+            .getJavaScriptObject()));
       } else if (isEnumOrSubclass) {
         // A subclass of an enum class
         call.addArg(JNullLiteral.INSTANCE);