use of org.codehaus.groovy.control.CompilationUnit in project groovy by apache.
the class Groovyc method makeCompileUnit.
protected CompilationUnit makeCompileUnit() {
Map<String, Object> options = configuration.getJointCompilationOptions();
if (options != null) {
if (keepStubs) {
options.put("keepStubs", Boolean.TRUE);
}
if (stubDir != null) {
options.put("stubDir", stubDir);
} else {
try {
File tempStubDir = DefaultGroovyStaticMethods.createTempDir(null, "groovy-generated-", "-java-source");
temporaryFiles.add(tempStubDir);
options.put("stubDir", tempStubDir);
} catch (IOException ioe) {
throw new BuildException(ioe);
}
}
return new JavaAwareCompilationUnit(configuration, buildClassLoaderFor());
} else {
return new CompilationUnit(configuration, null, buildClassLoaderFor());
}
}
use of org.codehaus.groovy.control.CompilationUnit in project groovity by disney.
the class Groovity method compileLoop.
@SuppressWarnings("rawtypes")
private void compileLoop(Map<String, Class<Script>> newScripts, Map<String, Long> newScriptDates, Map<String, File> deletedScripts, Map<String, Collection<String>> scriptDependencies, Map<String, Boolean> scriptInits, boolean force, boolean init, int numErrors, ConcurrentHashMap<String, Class> compilerTraits, GroovitySource... sources) {
HashMap<String, GroovitySource> errorSources = new HashMap<>();
for (GroovitySource source : sources) {
try {
GroovityCompilerEvent event = new GroovityCompilerEvent();
event.setPath(source.getPath());
event.setTime(System.currentTimeMillis());
String name = getScriptName(source.getPath());
String nameCaseFixed = fixCase(name);
String className = name.replaceAll("_", "___").replaceAll("/", "_");
// System.out.println("Compiling source "+source.getPath()+" "+name+" "+className);
if (source.exists()) {
if (scripts.containsKey(nameCaseFixed)) {
event.setChange(Change.update);
} else {
event.setChange(Change.add);
}
if (!force) {
Long cur = scriptDates.get(nameCaseFixed);
// System.out.println("comparing "+cur+" to "+source.getLastModified()+" for "+name);
if (cur != null && cur.longValue() == source.getLastModified()) {
continue;
}
}
String sourceCode = null;
try {
sourceCode = source.getSourceCode();
// code transformations pre-compile
TransformedSource transformed = GroovitySourceTransformer.transformSource(sourceCode);
sourceCode = transformed.source;
// groovy script compiler
long time1 = System.currentTimeMillis();
ArrayList<String> dependencies = new ArrayList<String>();
CompilerConfiguration compilerConfiguration = createCompilerConfiguration(transformed != null ? transformed.sourceLineNumbers : null, dependencies);
// a classloader only gets the traits that are available when it is created, so we make a copy
ConcurrentHashMap<String, Class> traitsCopy = new ConcurrentHashMap<>(compilerTraits);
GroovityClassLoader loader = getParentLoader() != null ? new GroovityClassLoader(source.getPath(), getParentLoader(), compilerConfiguration, this, cacheRefreshExecutor, traitsCopy) : new GroovityClassLoader(source.getPath(), Thread.currentThread().getContextClassLoader(), compilerConfiguration, this, cacheRefreshExecutor, traitsCopy);
CompilationUnit cu = new CompilationUnit(compilerConfiguration, null, loader);
SourceUnit su = new TransformedSourceUnit(className.concat(GROOVITY_SOURCE_EXTENSION), transformed, compilerConfiguration, loader, new ErrorCollector(compilerConfiguration));
// errorCollector.sourceUnit = su;
cu.addSource(su);
// Don't compile all or extra class files get generated!
cu.compile(Phases.CLASS_GENERATION);
@SuppressWarnings("unchecked") GroovyClass[] gcs = (GroovyClass[]) cu.getClasses().toArray(new GroovyClass[0]);
Class<Script> scriptClass = loadGroovyClassesAndFindScript(loader, gcs, compilerTraits, traitsCopy, traits);
if (scriptClass != null) {
long time2 = System.currentTimeMillis();
log.info("Compiled Groovy Script: ".concat(source.getPath()) + " in " + (time2 - time1));
} else {
log.severe("UHOH!! Unable to find main class for " + source.getPath());
}
if (jarPhases != null && jarPhases.contains(GroovityPhase.RUNTIME)) {
storeClasses(source, gcs);
}
if (log.isLoggable(Level.FINE)) {
log.fine("Registering script class " + scriptClass.getName() + " for name " + name);
}
newScripts.put(name, scriptClass);
newScriptDates.put(name, source.getLastModified());
scriptDependencies.put(name, dependencies);
scriptInits.put(name, hasInit((GroovityClassLoader) scriptClass.getClassLoader()));
if (log.isLoggable(Level.FINE)) {
log.fine("Found dependencies " + dependencies + " for script " + name);
}
} catch (Throwable th) {
errorSources.put(nameCaseFixed, source);
log.log(Level.FINE, "Error compiling " + source.getPath(), th);
if (sourceCode != null && log.isLoggable(Level.FINE)) {
log.fine("Source code in trouble: \n" + sourceCode);
}
event.setError(th);
} finally {
compileEvents.put(nameCaseFixed, event);
}
} else {
// remove from memory and disk
if (jarDirectory != null) {
deletedScripts.put(name, getClassesFile(source));
} else {
deletedScripts.put(name, null);
}
if (scripts.containsKey(nameCaseFixed)) {
event.setChange(Change.remove);
compileEvents.put(fixCase(name), event);
}
}
} catch (Exception e) {
log.log(Level.SEVERE, "Error compiling groovy " + source.getPath(), e);
}
}
if (!errorSources.isEmpty()) {
boolean retry = (numErrors == 0 || errorSources.size() < numErrors);
if (!retry) {
// let's check if the compiling set of traits is missing anything
for (Map.Entry<String, Class> entry : traits.entrySet()) {
if (!compilerTraits.containsKey(entry.getKey())) {
retry = true;
compilerTraits.put(entry.getKey(), entry.getValue());
}
}
}
if (retry) {
if (log.isLoggable(Level.FINE)) {
log.fine("Retrying error compile on " + errorSources.size());
}
// retry failed compiles after all the rest in case they just need to pick up traits
compileLoop(newScripts, newScriptDates, deletedScripts, scriptDependencies, scriptInits, force, init, errorSources.size(), compilerTraits, errorSources.values().toArray(new GroovitySource[0]));
}
}
}
use of org.codehaus.groovy.control.CompilationUnit in project webpieces by deanhiller.
the class GroovyToJavaClassCompiler method compileImpl.
private void compileImpl(GroovyClassLoader groovyCl, ScriptOutputImpl scriptCode) {
try {
GroovyCompiling.setCompilingGroovy(true);
CompilationUnit compileUnit = new CompilationUnit();
compileUnit.addSource(scriptCode.getFullClassName(), scriptCode.getScriptSourceCode());
compileUnit.setClassLoader(groovyCl);
compileUnit.compile(Phases.CLASS_GENERATION);
for (Object compileClass : compileUnit.getClasses()) {
GroovyClass groovyClass = (GroovyClass) compileClass;
callbacks.compiledGroovyClass(groovyCl, groovyClass);
}
} finally {
GroovyCompiling.setCompilingGroovy(false);
}
}
use of org.codehaus.groovy.control.CompilationUnit in project groovy-core by groovy.
the class CustomCodeVisitorSupport method createCompilationUnit.
protected CompilationUnit createCompilationUnit(CompilerConfiguration config, CodeSource source) {
CompilationUnit unit = super.createCompilationUnit(config, source);
unit.addPhaseOperation(new CustomPrimaryClassNodeOperation(), Phases.SEMANTIC_ANALYSIS);
return unit;
}
use of org.codehaus.groovy.control.CompilationUnit in project groovy-core by groovy.
the class Groovyc method makeCompileUnit.
protected CompilationUnit makeCompileUnit() {
Map<String, Object> options = configuration.getJointCompilationOptions();
if (options != null) {
if (keepStubs) {
options.put("keepStubs", Boolean.TRUE);
}
if (stubDir != null) {
options.put("stubDir", stubDir);
} else {
try {
File tempStubDir = DefaultGroovyStaticMethods.createTempDir(null, "groovy-generated-", "-java-source");
temporaryFiles.add(tempStubDir);
options.put("stubDir", tempStubDir);
} catch (IOException ioe) {
throw new BuildException(ioe);
}
}
return new JavaAwareCompilationUnit(configuration, buildClassLoaderFor());
} else {
return new CompilationUnit(configuration, null, buildClassLoaderFor());
}
}
Aggregations