Fixup permutation IDs so they match the index into the permutation array (and
therefore the arguments to -perms).  This is important so the generated JS
files have ids which match the arguments to -perms.

Patch by: jat, scottb
Review by: scottb, jat


git-svn-id: https://google-web-toolkit.googlecode.com/svn/releases/1.6@4360 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/dev/core/src/com/google/gwt/dev/Permutation.java b/dev/core/src/com/google/gwt/dev/Permutation.java
index 3eba3f2..28bacf7 100644
--- a/dev/core/src/com/google/gwt/dev/Permutation.java
+++ b/dev/core/src/com/google/gwt/dev/Permutation.java
@@ -32,6 +32,18 @@
   private final List<StaticPropertyOracle> propertyOracles = new ArrayList<StaticPropertyOracle>();
   private final SortedMap<String, String> rebindAnswers = new TreeMap<String, String>();
 
+  /**
+   * Clones an existing permutation, but with a new id.
+   *  
+   * @param id new permutation id
+   * @param other Permutation to copy
+   */
+  public Permutation(int id, Permutation other) {
+    this.id = id;
+    this.propertyOracles.addAll(other.propertyOracles);
+    this.rebindAnswers.putAll(other.rebindAnswers);
+  }
+
   public Permutation(int id, StaticPropertyOracle propertyOracle) {
     this.id = id;
     this.propertyOracles.add(propertyOracle);
diff --git a/dev/core/src/com/google/gwt/dev/Precompilation.java b/dev/core/src/com/google/gwt/dev/Precompilation.java
index 8eb7438..4c0fa32 100644
--- a/dev/core/src/com/google/gwt/dev/Precompilation.java
+++ b/dev/core/src/com/google/gwt/dev/Precompilation.java
@@ -19,6 +19,7 @@
 import com.google.gwt.dev.jjs.UnifiedAst;
 
 import java.io.Serializable;
+import java.util.Collection;
 
 /**
  * The result of compilation phase 1, includes a unified AST and metadata
@@ -35,17 +36,24 @@
   private final UnifiedAst unifiedAst;
 
   /**
-   * Constructs a new precompilation.
+   * Constructs a new precompilation.  We create new Permutations with
+   * a new id so that the ids are consecutive and correspond to the index
+   * in the array.
    * 
    * @param unifiedAst the unified AST used by
    *          {@link com.google.gwt.dev.jjs.JavaToJavaScriptCompiler}
    * @param permutations the set of permutations that can be run
    * @param generatedArtifacts the set of artifacts created by generators
    */
-  public Precompilation(UnifiedAst unifiedAst, Permutation[] permutations,
-      ArtifactSet generatedArtifacts) {
+  public Precompilation(UnifiedAst unifiedAst,
+      Collection<Permutation> permutations, ArtifactSet generatedArtifacts) {
     this.unifiedAst = unifiedAst;
-    this.permutations = permutations;
+    this.permutations = new Permutation[permutations.size()];
+    int i = 0;
+    for (Permutation permutation : permutations) {
+      this.permutations[i] = new Permutation(i, permutation);
+      ++i;
+    }
     this.generatedArtifacts = generatedArtifacts;
   }
 
diff --git a/dev/core/src/com/google/gwt/dev/Precompile.java b/dev/core/src/com/google/gwt/dev/Precompile.java
index b8f6997..c0ca5a2 100644
--- a/dev/core/src/com/google/gwt/dev/Precompile.java
+++ b/dev/core/src/com/google/gwt/dev/Precompile.java
@@ -290,8 +290,8 @@
           merged.put(rebindResultsString, permutation);
         }
       }
-      permutations = merged.values().toArray(new Permutation[merged.size()]);
-      return new Precompilation(unifiedAst, permutations, generatedArtifacts);
+      return new Precompilation(unifiedAst, merged.values(),
+          generatedArtifacts);
     } catch (UnableToCompleteException e) {
       // We intentionally don't pass in the exception here since the real
       // cause has been logged.