public Code compile(final Sources sources) throws CompileException {
if (sources == null) {
throw new IllegalArgumentException("Sources are null.");
}
try {
CompilationUnit cu = new CompilationUnit(config, null, groovyClassLoader);
Map<Source,SourceUnit> sourceUnitMap = new HashMap<Source,SourceUnit>();
Set<Source> sourceSet = sources.getSourceSet();
for (Source source : sourceSet) {
SourceUnit su = addToCompilationUnit(cu, source, sources);
//System.out.println("SU Name: " + su.getName());
sourceUnitMap.put(source, su);
}
int phase = (config.getTargetDirectory() == null) ? Phases.CLASS_GENERATION : Phases.OUTPUT;
cu.compile(phase);
Map<Source,CompiledSourceInfo> compiledSourceInfos = new HashMap<Source,CompiledSourceInfo>();
for (Entry<Source, SourceUnit> entry : sourceUnitMap.entrySet()) {
Source source = entry.getKey();
SourceUnit su = entry.getValue();
Set<String> classNames = new HashSet<String>();
List<ClassNode> nodes = su.getAST().getClasses();
for (ClassNode node : nodes) {
classNames.add(node.getName());
}
CompiledSourceInfo compiledSourceInfo = new CompiledSourceInfo(source,
su.getAST().getMainClassName(), classNames, source.getLastModified());
//System.out.println("SU MainClassName: " + su.getAST().getMainClassName());
compiledSourceInfos.put(source, compiledSourceInfo);
}
@SuppressWarnings("unchecked")
List<GroovyClass> groovyClasses = cu.getClasses();
Map<String, Bytecode> bytecodes = new HashMap<String,Bytecode>();
for (GroovyClass groovyClass : groovyClasses) {
String name = groovyClass.getName();
bytecodes.put(name, new Bytecode(name, groovyClass.getBytes()));
}