KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > google > gwt > dev > jdt > AstCompiler


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.jdt;
17
18 import com.google.gwt.core.ext.TreeLogger;
19 import com.google.gwt.core.ext.typeinfo.CompilationUnitProvider;
20
21 import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration;
22 import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
23
24 import java.io.File JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32 import java.util.Map JavaDoc;
33 import java.util.Set JavaDoc;
34
35 /**
36  * A facade around the JDT compiler to make many repeated compiles as fast as
37  * possible, where the result of each compile is a fully-resolved abstract
38  * syntax tree that can be walked for detailed analysis.
39  */

40 public class AstCompiler extends AbstractCompiler {
41
42   /**
43    * Manages the caching of <code>CompilationUnitDeclaration</code>.
44    */

45   private class CompilationUnitDeclarationCache {
46
47     private final Map JavaDoc lastModified = new HashMap JavaDoc();
48
49     private final Map JavaDoc map = new HashMap JavaDoc();
50
51     public void remove(String JavaDoc newLoc) {
52       map.remove(newLoc);
53     }
54
55     private void add(String JavaDoc location, CompilationUnitDeclaration item) {
56       File JavaDoc file = new File JavaDoc(location);
57       if (file.exists()) {
58         lastModified.put(location, new Long JavaDoc(file.lastModified()));
59       }
60       if (!map.containsKey(location)) {
61         map.put(location, new ArrayList JavaDoc());
62       }
63       get(location).add(item);
64     }
65
66     private boolean containsKey(String JavaDoc location) {
67       return map.containsKey(location);
68     }
69
70     private List JavaDoc get(Object JavaDoc key) {
71       return (List JavaDoc) map.get(key);
72     }
73
74     private CompilationUnitDeclaration[] getDeclarations() {
75       Set JavaDoc outSet = new HashSet JavaDoc();
76       for (Iterator JavaDoc iter = map.values().iterator(); iter.hasNext();) {
77         List JavaDoc element = (List JavaDoc) iter.next();
78         outSet.addAll(element);
79       }
80       CompilationUnitDeclaration[] out = new CompilationUnitDeclaration[outSet.size()];
81       int i = 0;
82       for (Iterator JavaDoc iter = outSet.iterator(); iter.hasNext();) {
83         CompilationUnitDeclaration element = (CompilationUnitDeclaration) iter.next();
84         out[i] = element;
85         i++;
86       }
87       return out;
88     }
89
90     private void removeAll(Collection JavaDoc c) {
91       map.keySet().removeAll(c);
92     }
93   }
94
95   private final CompilationUnitDeclarationCache cachedResults = new CompilationUnitDeclarationCache();
96
97   public AstCompiler(SourceOracle sourceOracle) {
98     super(sourceOracle, false);
99   }
100
101   public CompilationUnitDeclaration[] getCompilationUnitDeclarations(
102       TreeLogger logger, ICompilationUnit[] units) {
103     List JavaDoc allUnits = Arrays.asList(units);
104     List JavaDoc newUnits = new ArrayList JavaDoc();
105
106     // Check for newer units that need to be processed.
107
for (Iterator JavaDoc iter = allUnits.iterator(); iter.hasNext();) {
108       ICompilationUnitAdapter adapter = (ICompilationUnitAdapter) iter.next();
109       CompilationUnitProvider cup = adapter.getCompilationUnitProvider();
110       String JavaDoc location = cup.getLocation();
111       if (!(cachedResults.containsKey(location))) {
112         newUnits.add(adapter);
113       }
114     }
115     ICompilationUnit[] toBeProcessed = new ICompilationUnit[newUnits.size()];
116     newUnits.toArray(toBeProcessed);
117     CompilationUnitDeclaration[] newCuds = compile(logger, toBeProcessed);
118
119     // Put new cuds into cache.
120
for (int i = 0; i < newCuds.length; i++) {
121       String JavaDoc newLoc = String.valueOf(newCuds[i].getFileName());
122       cachedResults.remove(newLoc);
123       cachedResults.add(newLoc, newCuds[i]);
124     }
125     return cachedResults.getDeclarations();
126   }
127
128   public void invalidateChangedFiles(Set JavaDoc changedFiles, Set JavaDoc typeNames) {
129     cachedResults.removeAll(changedFiles);
130     invalidateUnitsInFiles(changedFiles, typeNames);
131   }
132 }
133
Popular Tags