| Constructor: |
public CompilationUnit() {
this(null, null, null);
}
Initializes the CompilationUnit with defaults. |
public CompilationUnit(GroovyClassLoader loader) {
this(null, null, loader);
}
Initializes the CompilationUnit with defaults except for class loader. |
public CompilationUnit(CompilerConfiguration configuration) {
this(configuration, null, null);
}
Initializes the CompilationUnit with no security considerations. |
public CompilationUnit(CompilerConfiguration configuration,
CodeSource security,
GroovyClassLoader loader) {
this(configuration, security, loader, null);
}
Initializes the CompilationUnit with a CodeSource for controlling
security stuff and a class loader for loading classes. |
public CompilationUnit(CompilerConfiguration configuration,
CodeSource security,
GroovyClassLoader loader,
GroovyClassLoader transformLoader) {
super(configuration, loader, null);
this.transformLoader = transformLoader;
this.names = new ArrayList();
this.queuedSources = new LinkedList();
this.sources = new HashMap();
this.summariesBySourceName = new HashMap();
this.summariesByPublicClassName = new HashMap();
this.classSourcesByPublicClassName = new HashMap();
this.ast = new CompileUnit(this.classLoader, security, this.configuration);
this.generatedClasses = new ArrayList();
this.verifier = new Verifier();
this.resolveVisitor = new ResolveVisitor(this);
this.staticImportVisitor = new StaticImportVisitor(this);
this.optimizer = new OptimizerVisitor(this);
phaseOperations = new LinkedList[Phases.ALL + 1];
for (int i = 0; i < phaseOperations.length; i++) {
phaseOperations[i] = new LinkedList();
}
addPhaseOperation(new SourceUnitOperation() {
public void call(SourceUnit source) throws CompilationFailedException {
source.parse();
}
}, Phases.PARSING);
addPhaseOperation(convert, Phases.CONVERSION);
addPhaseOperation(new PrimaryClassNodeOperation() {
public void call(SourceUnit source, GeneratorContext context,
ClassNode classNode) throws CompilationFailedException {
EnumVisitor ev = new EnumVisitor(CompilationUnit.this, source);
ev.visitClass(classNode);
}
}, Phases.CONVERSION);
addPhaseOperation(resolve, Phases.SEMANTIC_ANALYSIS);
addPhaseOperation(staticImport, Phases.SEMANTIC_ANALYSIS);
addPhaseOperation(compileCompleteCheck, Phases.CANONICALIZATION);
addPhaseOperation(classgen, Phases.CLASS_GENERATION);
addPhaseOperation(output);
ASTTransformationVisitor.addPhaseOperations(this);
this.classgenCallback = null;
}
Initializes the CompilationUnit with a CodeSource for controlling
security stuff, a class loader for loading classes, and a class
loader for loading AST transformations.
Note The transform loader must be
able to load compiler classes. That means CompilationUnit.class.classLoader
must be at last a parent to transformLoader. The other loader has no such constraint. Parameters:
transformLoader - - the loader for transforms
loader - - loader used to resolve classes against during compilation
security - - security setting for the compilation
configuration - - compilation configuration
|
| Method from org.codehaus.groovy.control.CompilationUnit Detail: |
public void addClassNode(ClassNode node) {
ModuleNode module = new ModuleNode(this.ast);
this.ast.addModule(module);
module.addClass(node);
}
Adds a ClassNode directly to the unit (ie. without source).
WARNING: the source is needed for error reporting, using
this method without setting a SourceUnit will cause
NullPinterExceptions |
public void addPhaseOperation(CompilationUnit.GroovyClassOperation op) {
phaseOperations[Phases.OUTPUT].addFirst(op);
}
|
public void addPhaseOperation(CompilationUnit.SourceUnitOperation op,
int phase) {
if (phase < 0 || phase > Phases.ALL) throw new IllegalArgumentException("phase " + phase + " is unknown");
phaseOperations[phase].add(op);
}
|
public void addPhaseOperation(CompilationUnit.PrimaryClassNodeOperation op,
int phase) {
if (phase < 0 || phase > Phases.ALL) throw new IllegalArgumentException("phase " + phase + " is unknown");
phaseOperations[phase].add(op);
}
|
public SourceUnit addSource(File file) {
return addSource(new SourceUnit(file, configuration, classLoader, getErrorCollector()));
}
Adds a source file to the unit. |
public SourceUnit addSource(URL url) {
return addSource(new SourceUnit(url, configuration, classLoader, getErrorCollector()));
}
Adds a source file to the unit. |
public SourceUnit addSource(SourceUnit source) {
String name = source.getName();
source.setClassLoader(this.classLoader);
for (Iterator iter = queuedSources.iterator(); iter.hasNext();) {
SourceUnit su = (SourceUnit) iter.next();
if (name.equals(su.getName())) return su;
}
queuedSources.add(source);
return source;
}
Adds a SourceUnit to the unit. |
public SourceUnit addSource(String name,
InputStream stream) {
ReaderSource source = new InputStreamReaderSource(stream, configuration);
return addSource(new SourceUnit(name, source, configuration, classLoader, getErrorCollector()));
}
Adds a InputStream source to the unit. |
public void addSources(String[] paths) {
for (int i = 0; i < paths.length; i++) {
File file = new File(paths[i]);
addSource(file);
}
}
Adds a set of file paths to the unit. |
public void addSources(File[] files) {
for (int i = 0; i < files.length; i++) {
addSource(files[i]);
}
}
Adds a set of source files to the unit. |
public void applyToGeneratedGroovyClasses(CompilationUnit.GroovyClassOperation body) throws CompilationFailedException {
if (this.phase != Phases.OUTPUT && !(this.phase == Phases.CLASS_GENERATION && this.phaseComplete)) {
throw new GroovyBugError("CompilationUnit not ready for output(). Current phase=" + getPhaseDescription());
}
boolean failures = false;
Iterator iterator = this.generatedClasses.iterator();
while (iterator.hasNext()) {
//
// Get the class and calculate its filesystem name
//
GroovyClass gclass = (GroovyClass) iterator.next();
try {
body.call(gclass);
} catch (CompilationFailedException e) {
// fall thorugh, getErrorREporter().failIfErrors() will triger
} catch (NullPointerException npe) {
throw npe;
} catch (GroovyBugError e) {
changeBugText(e, null);
throw e;
} catch (Exception e) {
GroovyBugError gbe = new GroovyBugError(e);
throw gbe;
}
}
getErrorCollector().failIfErrors();
}
|
public void applyToPrimaryClassNodes(CompilationUnit.PrimaryClassNodeOperation body) throws CompilationFailedException {
Iterator classNodes = getPrimaryClassNodes(body.needSortedInput()).iterator();
while (classNodes.hasNext()) {
SourceUnit context = null;
try {
ClassNode classNode = (ClassNode) classNodes.next();
context = classNode.getModule().getContext();
if (context == null || context.phase < = phase) {
body.call(context, new GeneratorContext(this.ast), classNode);
}
} catch (CompilationFailedException e) {
// fall through, getErrorReporter().failIfErrors() will triger
} catch (NullPointerException npe) {
throw npe;
} catch (GroovyBugError e) {
changeBugText(e, context);
throw e;
} catch (Exception e) {
// check the exception for a nested compilation exception
ErrorCollector nestedCollector = null;
for (Throwable next = e.getCause(); next != e && next != null; next = next.getCause()) {
if (!(next instanceof MultipleCompilationErrorsException)) continue;
MultipleCompilationErrorsException mcee = (MultipleCompilationErrorsException) next;
nestedCollector = mcee.collector;
break;
}
if (nestedCollector != null) {
getErrorCollector().addCollectorContents(nestedCollector);
} else {
getErrorCollector().addError(new ExceptionMessage(e, configuration.getDebug(), this));
}
}
}
getErrorCollector().failIfErrors();
}
A loop driver for applying operations to all primary ClassNodes in
our AST. Automatically skips units that have already been processed
through the current phase. |
public void applyToSourceUnits(CompilationUnit.SourceUnitOperation body) throws CompilationFailedException {
//---------------------------------------------------------------------------
// LOOP SIMPLIFICATION FOR SourceUnit OPERATIONS
Iterator keys = names.iterator();
while (keys.hasNext()) {
String name = (String) keys.next();
SourceUnit source = (SourceUnit) sources.get(name);
if ((source.phase < phase) || (source.phase == phase && !source.phaseComplete)) {
try {
body.call(source);
} catch (CompilationFailedException e) {
throw e;
} catch (Exception e) {
GroovyBugError gbe = new GroovyBugError(e);
changeBugText(gbe, source);
throw gbe;
} catch (GroovyBugError e) {
changeBugText(e, source);
throw e;
}
}
}
getErrorCollector().failIfErrors();
}
A loop driver for applying operations to all SourceUnits.
Automatically skips units that have already been processed
through the current phase. |
public void compile() throws CompilationFailedException {
compile(Phases.ALL);
}
Synonym for compile(Phases.ALL). |
public void compile(int throughPhase) throws CompilationFailedException {
//
// To support delta compilations, we always restart
// the compiler. The individual passes are responsible
// for not reprocessing old code.
gotoPhase(Phases.INITIALIZATION);
throughPhase = Math.min(throughPhase, Phases.ALL);
while (throughPhase >= phase && phase < = Phases.ALL) {
for (Iterator it = phaseOperations[phase].iterator(); it.hasNext();) {
Object operation = it.next();
if (operation instanceof PrimaryClassNodeOperation) {
applyToPrimaryClassNodes((PrimaryClassNodeOperation) operation);
} else if (operation instanceof SourceUnitOperation) {
applyToSourceUnits((SourceUnitOperation) operation);
} else {
applyToGeneratedGroovyClasses((GroovyClassOperation) operation);
}
}
if (progressCallback != null) progressCallback.call(this, phase);
completePhase();
applyToSourceUnits(mark);
if (dequeued()) continue;
gotoPhase(phase + 1);
if (phase == Phases.CLASS_GENERATION) {
sortClasses();
}
}
errorCollector.failIfErrors();
}
Compiles the compilation unit from sources. |
public void configure(CompilerConfiguration configuration) {
super.configure(configuration);
this.debug = configuration.getDebug();
if (!this.configured && this.classLoader instanceof GroovyClassLoader) {
appendCompilerConfigurationClasspathToClassLoader(configuration, (GroovyClassLoader) this.classLoader);
}
this.configured = true;
}
Configures its debugging mode and classloader classpath from a given compiler configuration.
This cannot be done more than once due to limitations in URLClassLoader . |
protected ClassVisitor createClassVisitor() {
return new ClassWriter(true);
}
|
protected boolean dequeued() throws CompilationFailedException {
boolean dequeue = !queuedSources.isEmpty();
while (!queuedSources.isEmpty()) {
SourceUnit su = (SourceUnit) queuedSources.removeFirst();
String name = su.getName();
names.add(name);
sources.put(name, su);
}
if (dequeue) {
gotoPhase(Phases.INITIALIZATION);
}
return dequeue;
}
Dequeues any source units add through addSource and resets the compiler phase
to initialization.
Note: this does not mean a file is recompiled. If a SoucreUnit has already passed
a phase it is skipped until a higher phase is reached. |
public CompileUnit getAST() {
return this.ast;
}
Returns the CompileUnit that roots our AST. |
public ClassNode getClassNode(String name) {
final ClassNode[] result = new ClassNode[]{null};
PrimaryClassNodeOperation handler = new PrimaryClassNodeOperation() {
public void call(SourceUnit source, GeneratorContext context, ClassNode classNode) {
if (classNode.getName().equals(name)) {
result[0] = classNode;
}
}
};
try {
applyToPrimaryClassNodes(handler);
} catch (CompilationFailedException e) {
if (debug) e.printStackTrace();
}
return result[0];
}
Convenience routine to get the named ClassNode. |
public Map getClassSourcesByPublicClassName() {
return classSourcesByPublicClassName;
}
|
public List getClasses() {
return generatedClasses;
}
Get the GroovyClasses generated by compile(). |
public ClassNode getFirstClassNode() {
return (ClassNode) ((ModuleNode) this.ast.getModules().get(0)).getClasses().get(0);
}
Convenience routine to get the first ClassNode, for
when you are sure there is only one. |
public Map getSummariesByPublicClassName() {
return summariesByPublicClassName;
}
|
public Map getSummariesBySourceName() {
return summariesBySourceName;
}
|
public GroovyClassLoader getTransformLoader() {
return transformLoader == null ? getClassLoader() : transformLoader;
}
Returns the class loader for loading AST transformations. |
public boolean isPublicClass(String className) {
return summariesByPublicClassName.containsKey(className);
}
|
public Iterator iterator() {
return new Iterator() {
Iterator nameIterator = names.iterator();
public boolean hasNext() {
return nameIterator.hasNext();
}
public Object next() {
String name = (String) nameIterator.next();
return sources.get(name);
}
public void remove() {
throw new UnsupportedOperationException();
}
};
}
Returns an iterator on the unit's SourceUnits. |
protected void mark() throws CompilationFailedException {
applyToSourceUnits(mark);
}
Updates the phase marker on all sources. |
public void setClassgenCallback(CompilationUnit.ClassgenCallback visitor) {
this.classgenCallback = visitor;
}
Sets a ClassgenCallback. You can have only one, and setting
it to null removes any existing setting. |
public void setProgressCallback(CompilationUnit.ProgressCallback callback) {
this.progressCallback = callback;
}
Sets a ProgressCallback. You can have only one, and setting
it to null removes any existing setting. |