| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.common.base.Preconditions; |
| 20 |
import com.google.common.collect.ImmutableList; |
| 21 |
import com.google.common.collect.Lists; |
| 22 |
import com.google.common.collect.Sets; |
| 23 |
import com.google.javascript.jscomp.deps.DependencyInfo; |
| 24 |
import com.google.javascript.jscomp.deps.SortedDependencies; |
| 25 |
import com.google.javascript.jscomp.deps.SortedDependencies.CircularDependencyException; |
| 26 |
|
| 27 |
import java.io.Serializable; |
| 28 |
import java.util.ArrayList; |
| 29 |
import java.util.Collection; |
| 30 |
import java.util.Collections; |
| 31 |
import java.util.Iterator; |
| 32 |
import java.util.List; |
| 33 |
import java.util.Set; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class JSModule implements DependencyInfo, Serializable { |
| 41 |
private static final long serialVersionUID = 1; |
| 42 |
|
| 43 |
static final DiagnosticType CIRCULAR_DEPENDENCY_ERROR = |
| 44 |
DiagnosticType.error("JSC_CIRCULAR_DEP", |
| 45 |
"Circular dependency detected: {0}"); |
| 46 |
|
| 47 |
|
| 48 |
private final String name; |
| 49 |
|
| 50 |
|
| 51 |
private final List<CompilerInput> inputs = new ArrayList<CompilerInput>(); |
| 52 |
|
| 53 |
|
| 54 |
private final List<JSModule> deps = new ArrayList<JSModule>(); |
| 55 |
|
| 56 |
private int depth; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public JSModule(String name) { |
| 63 |
this.name = name; |
| 64 |
this.depth = -1; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
@Override |
| 69 |
public String getName() { |
| 70 |
return name; |
| 71 |
} |
| 72 |
|
| 73 |
@Override |
| 74 |
public List<String> getProvides() { |
| 75 |
return ImmutableList.<String>of(name); |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
public List<String> getRequires() { |
| 80 |
ImmutableList.Builder<String> builder = ImmutableList.builder(); |
| 81 |
for (JSModule m : deps) { |
| 82 |
builder.add(m.getName()); |
| 83 |
} |
| 84 |
return builder.build(); |
| 85 |
} |
| 86 |
|
| 87 |
@Override |
| 88 |
public String getPathRelativeToClosureBase() { |
| 89 |
throw new UnsupportedOperationException(); |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
public void add(SourceFile file) { |
| 94 |
add(new CompilerInput(file)); |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
public void addFirst(SourceFile file) { |
| 99 |
addFirst(new CompilerInput(file)); |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
public void add(CompilerInput input) { |
| 104 |
inputs.add(input); |
| 105 |
input.setModule(this); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
void addAndOverrideModule(CompilerInput input) { |
| 114 |
inputs.add(input); |
| 115 |
input.overrideModule(this); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
public void addFirst(CompilerInput input) { |
| 120 |
inputs.add(0, input); |
| 121 |
input.setModule(this); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
public void addAfter(CompilerInput input, CompilerInput other) { |
| 126 |
Preconditions.checkState(inputs.contains(other)); |
| 127 |
inputs.add(inputs.indexOf(other), input); |
| 128 |
input.setModule(this); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
public void addDependency(JSModule dep) { |
| 133 |
Preconditions.checkNotNull(dep); |
| 134 |
Preconditions.checkState(dep != this); |
| 135 |
deps.add(dep); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
public void remove(CompilerInput input) { |
| 140 |
input.setModule(null); |
| 141 |
inputs.remove(input); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
public void removeAll() { |
| 146 |
for (CompilerInput input : inputs) { |
| 147 |
input.setModule(null); |
| 148 |
} |
| 149 |
inputs.clear(); |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
public List<JSModule> getDependencies() { |
| 158 |
return deps; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
List<String> getSortedDependencyNames() { |
| 166 |
List<String> names = Lists.newArrayList(); |
| 167 |
for (JSModule module : getDependencies()) { |
| 168 |
names.add(module.getName()); |
| 169 |
} |
| 170 |
Collections.sort(names); |
| 171 |
return names; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
public Set<JSModule> getAllDependencies() { |
| 179 |
Set<JSModule> allDeps = Sets.newHashSet(deps); |
| 180 |
List<JSModule> workList = Lists.newArrayList(deps); |
| 181 |
while (workList.size() > 0) { |
| 182 |
JSModule module = workList.remove(workList.size() - 1); |
| 183 |
for (JSModule dep : module.getDependencies()) { |
| 184 |
if (allDeps.add(dep)) { |
| 185 |
workList.add(dep); |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
return allDeps; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
public Set<JSModule> getThisAndAllDependencies() { |
| 194 |
Set<JSModule> deps = getAllDependencies(); |
| 195 |
deps.add(this); |
| 196 |
return deps; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
public List<CompilerInput> getInputs() { |
| 205 |
return inputs; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
public CompilerInput getByName(String name) { |
| 210 |
for (CompilerInput input : inputs) { |
| 211 |
if (name.equals(input.getName())) { |
| 212 |
return input; |
| 213 |
} |
| 214 |
} |
| 215 |
return null; |
| 216 |
} |
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
public boolean removeByName(String name) { |
| 222 |
boolean found = false; |
| 223 |
Iterator<CompilerInput> iter = inputs.iterator(); |
| 224 |
while (iter.hasNext()) { |
| 225 |
CompilerInput file = iter.next(); |
| 226 |
if (name.equals(file.getName())) { |
| 227 |
iter.remove(); |
| 228 |
file.setModule(null); |
| 229 |
found = true; |
| 230 |
} |
| 231 |
} |
| 232 |
return found; |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
@Override |
| 237 |
public String toString() { |
| 238 |
return name; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
public void clearAsts() { |
| 246 |
for (CompilerInput input : inputs) { |
| 247 |
input.clearAst(); |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
public void sortInputsByDeps(Compiler compiler) { |
| 255 |
|
| 256 |
|
| 257 |
for (CompilerInput input : inputs) { |
| 258 |
input.setCompiler(compiler); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
try { |
| 263 |
List<CompilerInput> sortedList = |
| 264 |
(new SortedDependencies<CompilerInput>( |
| 265 |
Collections.<CompilerInput>unmodifiableList(inputs))) |
| 266 |
.getSortedList(); |
| 267 |
inputs.clear(); |
| 268 |
inputs.addAll(sortedList); |
| 269 |
} catch (CircularDependencyException e) { |
| 270 |
compiler.report( |
| 271 |
JSError.make(CIRCULAR_DEPENDENCY_ERROR, e.getMessage())); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
public static JSModule[] sortJsModules(Collection<JSModule> modules) |
| 284 |
throws CircularDependencyException { |
| 285 |
|
| 286 |
List<JSModule> sortedList = (new SortedDependencies<JSModule>( |
| 287 |
Lists.newArrayList(modules))).getSortedList(); |
| 288 |
return sortedList.toArray(new JSModule[sortedList.size()]); |
| 289 |
} |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
public void setDepth(int dep) { |
| 295 |
this.depth = dep; |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
public int getDepth() { |
| 302 |
return depth; |
| 303 |
} |
| 304 |
} |