Fix web-mode compilation of SingleJsoImpl types when the implementing JSO type is never explicitly referenced by client code.

Patch by: bobv
Review by: scottb


git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@4877 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/dev/core/src/com/google/gwt/dev/jdt/WebModeCompilerFrontEnd.java b/dev/core/src/com/google/gwt/dev/jdt/WebModeCompilerFrontEnd.java
index 28185c6..9324d24 100644
--- a/dev/core/src/com/google/gwt/dev/jdt/WebModeCompilerFrontEnd.java
+++ b/dev/core/src/com/google/gwt/dev/jdt/WebModeCompilerFrontEnd.java
@@ -17,7 +17,10 @@
 
 import com.google.gwt.core.ext.TreeLogger;
 import com.google.gwt.core.ext.UnableToCompleteException;
+import com.google.gwt.core.ext.typeinfo.JClassType;
+import com.google.gwt.core.ext.typeinfo.TypeOracle;
 import com.google.gwt.dev.javac.CompilationState;
+import com.google.gwt.dev.javac.CompilationUnit;
 import com.google.gwt.dev.javac.CompiledClass;
 import com.google.gwt.dev.javac.JdtCompiler.CompilationUnitAdapter;
 import com.google.gwt.dev.jdt.FindDeferredBindingSitesVisitor.MessageSendSite;
@@ -32,7 +35,9 @@
 import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
 import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
 
+import java.util.ArrayList;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
@@ -59,28 +64,62 @@
     this.fragmentLoaderCreator = fragmentLoaderCreator;
   }
 
+  /**
+   * Build the initial set of compilation units.
+   */
   public CompilationUnitDeclaration[] getCompilationUnitDeclarations(
       TreeLogger logger, String[] seedTypeNames)
       throws UnableToCompleteException {
 
-    // Build the initial set of compilation units.
+    TypeOracle oracle = compilationState.getTypeOracle();
+    Set<JClassType> intfTypes = oracle.getSingleJsoImplInterfaces();
     Map<String, CompiledClass> classMapBySource = compilationState.getClassFileMapBySource();
-    ICompilationUnit[] icus = new ICompilationUnit[seedTypeNames.length];
-    for (int i = 0; i < seedTypeNames.length; i++) {
-      String seedTypeName = seedTypeNames[i];
-      CompiledClass compiledClass = classMapBySource.get(seedTypeName);
-      if (compiledClass == null) {
-        logger.log(TreeLogger.ERROR,
-            "Unable to find compilation unit for type '" + seedTypeName + "'");
-        throw new UnableToCompleteException();
+
+    /*
+     * The alreadyAdded set prevents duplicate CompilationUnits from being added
+     * to the icu list in the case of multiple JSO implementations as inner
+     * classes in the same top-level class or seed classes as SingleJsoImpls
+     * (e.g. JSO itself as the SingleImpl for all tag interfaces).
+     */
+    Set<CompilationUnit> alreadyAdded = new HashSet<CompilationUnit>();
+
+    List<ICompilationUnit> icus = new ArrayList<ICompilationUnit>(
+        seedTypeNames.length + intfTypes.size());
+
+    for (String seedTypeName : seedTypeNames) {
+      CompilationUnit unit = getUnitForType(logger, classMapBySource,
+          seedTypeName);
+
+      if (alreadyAdded.add(unit)) {
+        icus.add(new CompilationUnitAdapter(unit));
+      } else {
+        logger.log(TreeLogger.WARN, "Duplicate compilation unit '"
+            + unit.getDisplayLocation() + "'in seed types");
       }
-      icus[i] = new CompilationUnitAdapter(compiledClass.getUnit());
     }
 
-    // Compile, which will pull in everything else via
-    // doFindAdditionalTypesUsingMagic()
-    //
-    CompilationUnitDeclaration[] cuds = compile(logger, icus);
+    /*
+     * Add all SingleJsoImpl types that we know about. It's likely that the
+     * concrete types are never explicitly referenced from the seed types.
+     */
+    for (JClassType intf : intfTypes) {
+      String implName = oracle.getSingleJsoImpl(intf).getQualifiedSourceName();
+      CompilationUnit unit = getUnitForType(logger, classMapBySource, implName);
+
+      if (alreadyAdded.add(unit)) {
+        icus.add(new CompilationUnitAdapter(unit));
+        logger.log(TreeLogger.SPAM, "Forced compilation of unit '"
+            + unit.getDisplayLocation()
+            + "' becasue it contains a SingleJsoImpl type");
+      }
+    }
+
+    /*
+     * Compile, which will pull in everything else via
+     * doFindAdditionalTypesUsingMagic()
+     */
+    CompilationUnitDeclaration[] cuds = compile(logger,
+        icus.toArray(new ICompilationUnit[icus.size()]));
     return cuds;
   }
 
@@ -212,4 +251,23 @@
 
     return dependentTypeNames.toArray(Empty.STRINGS);
   }
+
+  /**
+   * Get the CompilationUnit for a named type or throw an
+   * UnableToCompleteException.
+   */
+  private CompilationUnit getUnitForType(TreeLogger logger,
+      Map<String, CompiledClass> classMapBySource, String typeName)
+      throws UnableToCompleteException {
+
+    CompiledClass compiledClass = classMapBySource.get(typeName);
+    if (compiledClass == null) {
+      logger.log(TreeLogger.ERROR, "Unable to find compilation unit for type '"
+          + typeName + "'");
+      throw new UnableToCompleteException();
+    }
+
+    assert compiledClass.getUnit() != null;
+    return compiledClass.getUnit();
+  }
 }
diff --git a/user/test/com/google/gwt/dev/jjs/test/SingleJsoImplTest.java b/user/test/com/google/gwt/dev/jjs/test/SingleJsoImplTest.java
index 7edef96..7f7f58b 100644
--- a/user/test/com/google/gwt/dev/jjs/test/SingleJsoImplTest.java
+++ b/user/test/com/google/gwt/dev/jjs/test/SingleJsoImplTest.java
@@ -17,6 +17,7 @@
 
 import com.google.gwt.core.client.JavaScriptObject;
 import com.google.gwt.dev.jjs.test.SingleJsoImplTest.JsoHasInnerJsoType.InnerType;
+import com.google.gwt.dev.jjs.test.jsointfs.JsoInterfaceWithUnreferencedImpl;
 import com.google.gwt.junit.client.GWTTestCase;
 
 import java.io.IOException;
@@ -513,4 +514,10 @@
       assertEquals(4.0, ((Log2) l2).log2(16));
     }
   }
+
+  public void testUnreferencedType() {
+    JsoInterfaceWithUnreferencedImpl o = (JsoInterfaceWithUnreferencedImpl) JavaScriptObject.createObject();
+    assertNotNull(o);
+    assertTrue(o.isOk());
+  }
 }
diff --git a/user/test/com/google/gwt/dev/jjs/test/jsoimpls/UnreferencedImplOfJsoInterface.java b/user/test/com/google/gwt/dev/jjs/test/jsoimpls/UnreferencedImplOfJsoInterface.java
new file mode 100644
index 0000000..d037a87
--- /dev/null
+++ b/user/test/com/google/gwt/dev/jjs/test/jsoimpls/UnreferencedImplOfJsoInterface.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright 2009 Google Inc.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.gwt.dev.jjs.test.jsoimpls;
+
+import com.google.gwt.core.client.JavaScriptObject;
+import com.google.gwt.dev.jjs.test.jsointfs.JsoInterfaceWithUnreferencedImpl;
+
+/**
+ * This class exists for the purpose of testing JSO implementation types that
+ * aren't specifically referenced in any Java source.
+ */
+public final class UnreferencedImplOfJsoInterface extends JavaScriptObject implements
+    JsoInterfaceWithUnreferencedImpl {
+  protected UnreferencedImplOfJsoInterface() {
+  }
+
+  public boolean isOk() {
+    if (2 + 2 == 4) {
+      return true;
+    } else {
+      return false;
+    }
+  }
+}
diff --git a/user/test/com/google/gwt/dev/jjs/test/jsointfs/JsoInterfaceWithUnreferencedImpl.java b/user/test/com/google/gwt/dev/jjs/test/jsointfs/JsoInterfaceWithUnreferencedImpl.java
new file mode 100644
index 0000000..42a7e26
--- /dev/null
+++ b/user/test/com/google/gwt/dev/jjs/test/jsointfs/JsoInterfaceWithUnreferencedImpl.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright 2009 Google Inc.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not
+ * use this file except in compliance with the License. You may obtain a copy of
+ * the License at
+ * 
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
+ * License for the specific language governing permissions and limitations under
+ * the License.
+ */
+package com.google.gwt.dev.jjs.test.jsointfs;
+
+/**
+ * This class exists for the purpose of testing JSO implementation types that
+ * aren't specifically referenced in any Java source.
+ */
+public interface JsoInterfaceWithUnreferencedImpl {
+  boolean isOk();
+}