KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > cfg > Compilations


1 /*
2  * Copyright 2006 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16 package com.google.gwt.dev.cfg;
17
18 import com.google.gwt.core.ext.TreeLogger;
19 import com.google.gwt.core.ext.UnableToCompleteException;
20 import com.google.gwt.dev.jdt.RebindOracle;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 /**
27  * Manages a set of {@link Compilation} objects keyed by deferred binding
28  * decisions. This class encapsulaes the idea of caching a set of compilations,
29  * some of which may be reusable between permutations to avoid redundant
30  * recompilation.
31  */

32 public class Compilations {
33
34   private final List JavaDoc list = new ArrayList JavaDoc();
35
36   public void add(Compilation compilation) {
37     list.add(compilation);
38   }
39
40   /**
41    * Finds an existing compilation that is equivalent to what would be
42    * generated.
43    */

44   public Compilation find(TreeLogger logger, RebindOracle rebindOracle,
45       String JavaDoc[] entryPts) throws UnableToCompleteException {
46     // NOTE: This could be optimized in a wide variety of ways.
47
//
48

49     // Clone the list so that we can easily whittle it down.
50
//
51
List JavaDoc candidates = new ArrayList JavaDoc(list);
52
53     // First, the entry points must all be present and map the same way in the
54
// correct compilation.
55
//
56
for (int i = 0; !candidates.isEmpty() && i < entryPts.length; i++) {
57       String JavaDoc in = entryPts[i];
58       String JavaDoc out = rebindOracle.rebind(logger, in);
59       removeMismatches(candidates, in, out);
60     }
61
62     // For the remaining compilations, all recorded rebinds must still match.
63
//
64
for (Iterator JavaDoc iter = candidates.iterator(); iter.hasNext();) {
65       Compilation c = (Compilation) iter.next();
66       String JavaDoc[] cachedIns = c.getRebindInputs();
67       for (int i = 0; i < cachedIns.length; i++) {
68         String JavaDoc cachedIn = cachedIns[i];
69         String JavaDoc cachedOut = c.getRebindOutput(cachedIn);
70         String JavaDoc out = rebindOracle.rebind(logger, cachedIn);
71         if (!cachedOut.equals(out)) {
72           // Not a candidate anymore; remove it.
73
//
74
iter.remove();
75           break;
76         }
77       }
78     }
79
80     if (candidates.isEmpty()) {
81       // Not found in the cache.
82
//
83
return null;
84     } else if (candidates.size() == 1) {
85       // Found the perfect match.
86
//
87
return (Compilation) candidates.get(0);
88     } else {
89       // Unexpected situation that means something really weird happened.
90
//
91
String JavaDoc msg = "Cannot decided between multiple existing compilations; cannot continue";
92       logger.log(TreeLogger.ERROR, msg, null);
93       throw new UnableToCompleteException();
94     }
95   }
96
97   public Iterator JavaDoc iterator() {
98     return list.iterator();
99   }
100
101   private void removeMismatches(List JavaDoc candidates, String JavaDoc in, String JavaDoc out) {
102     for (Iterator JavaDoc iter = candidates.iterator(); iter.hasNext();) {
103       Compilation c = (Compilation) iter.next();
104       String JavaDoc cachedOut = c.getRebindOutput(in);
105       if (!out.equals(cachedOut)) {
106         iter.remove();
107       }
108     }
109   }
110 }
111
Popular Tags