});
}
@Override
protected CompilationUnit createCompilationUnit(CompilerConfiguration configuration, CodeSource source) {
CompilationUnit cu = super.createCompilationUnit(configuration, source);
LocalData local = getLocalData().get();
local.cu = cu;
final StringSetMap cache = local.dependencyCache;
final Map<String,String> precompiledEntries = local.precompiledEntries;
// "." is used to transfer compilation dependencies, which will be
// recollected later during compilation
for (String depSourcePath : cache.get(".")) {
try {
cache.get(depSourcePath);
cu.addSource(getResourceConnection(depSourcePath).getURL());
} catch (ResourceException e) {
/* ignore */
}
}
// remove all old entries including the "." entry
cache.clear();
cu.addPhaseOperation(new CompilationUnit.PrimaryClassNodeOperation() {
@Override
public void call(final SourceUnit source, GeneratorContext context, ClassNode classNode)
throws CompilationFailedException {
// GROOVY-4013: If it is an inner class, tracking its dependencies doesn't really
// serve any purpose and also interferes with the caching done to track dependencies
if (classNode instanceof InnerClassNode) return;
DependencyTracker dt = new DependencyTracker(source, cache, precompiledEntries);
dt.visitClass(classNode);
}
}, Phases.CLASS_GENERATION);
cu.setClassNodeResolver(new ClassNodeResolver() {
@Override
public LookupResult findClassNode(String origName, CompilationUnit compilationUnit) {
CompilerConfiguration cc = compilationUnit.getConfiguration();
String name = origName.replace('.', '/');
for (String ext : cc.getScriptExtensions()) {
try {
String finalName = name+"."+ext;
URLConnection conn = rc.getResourceConnection(finalName);
URL url = conn.getURL();
String path = url.toExternalForm();
ScriptCacheEntry entry = scriptCache.get(path);
Class clazz = null;
if (entry != null) clazz = entry.scriptClass;
if (GroovyScriptEngine.this.isSourceNewer(entry)) {
try {
SourceUnit su = compilationUnit.addSource(url);
return new LookupResult(su, null);
} finally {
forceClose(conn);
}
} else {
precompiledEntries.put(origName, path);
}
if (clazz!=null) {
ClassNode cn = new ClassNode(clazz);
return new LookupResult(null, cn);
}
} catch (ResourceException re) {
// skip
}
}
return super.findClassNode(origName, compilationUnit);
}
});
final List<CompilationCustomizer> customizers = config.getCompilationCustomizers();
if (customizers!=null) {
// GROOVY-4813 : apply configuration customizers
for (CompilationCustomizer customizer : customizers) {
cu.addPhaseOperation(customizer, customizer.getPhase().getPhaseNumber());
}
}
return cu;
}