org.codehaus.groovy.control
Class CompilationUnit

java.lang.Object
  extended byorg.codehaus.groovy.control.ProcessingUnit
      extended byorg.codehaus.groovy.control.CompilationUnit

public class CompilationUnit
extends ProcessingUnit

Collects all compilation data as it is generated by the compiler system. Allows additional source units to be added and compilation run again (to affect only the deltas).

Version:
$Id: CompilationUnit.java,v 1.4 2004/05/04 06:11:37 cpoirier Exp $
Author:
Chris Poirier

Nested Class Summary
static class CompilationUnit.ClassgenCallback
          A callback interface you can use to "accompany" the classgen() code as it traverses the ClassNode tree.
 class CompilationUnit.LoopBodyForPrimaryClassNodeOperations
          An callback interface for use in the applyToSourceUnits loop driver.
 class CompilationUnit.LoopBodyForSourceUnitOperations
          An callback interface for use in the applyToSourceUnits loop driver.
static class CompilationUnit.ProgressCallback
          A callback interface you can use to get a callback after every unit of the compile process.
 
Field Summary
protected  CompileUnit ast
           
protected  ArrayList classes
           
protected  CompilationUnit.ClassgenCallback classgenCallback
           
protected  boolean configured
           
protected  boolean debug
           
protected  ArrayList names
           
protected  CompilationUnit.ProgressCallback progressCallback
           
protected  HashMap sources
           
protected  Verifier verifier
           
 
Fields inherited from class org.codehaus.groovy.control.ProcessingUnit
classLoader, configuration, errors, fatal, output, phase, phaseComplete, tolerance, warningLevel, warnings
 
Constructor Summary
CompilationUnit()
          Initializes the CompilationUnit with defaults.
CompilationUnit(ClassLoader loader)
          Initializes the CompilationUnit with defaults except for class loader.
CompilationUnit(CompilerConfiguration configuration)
          Initializes the CompilationUnit with no security considerations.
CompilationUnit(CompilerConfiguration configuration, CodeSource security, ClassLoader loader)
          Initializes the CompilationUnit with a CodeSource for controlling security stuff and a class loader for loading classes.
 
Method Summary
 void addClassNode(ClassNode node)
          Adds a ClassNode directly to the unit (ie.
 SourceUnit addSource(File file)
          Adds a source file to the unit.
 SourceUnit addSource(SourceUnit source)
          Adds a SourceUnit to the unit.
 SourceUnit addSource(String name, InputStream stream)
          Adds a InputStream source to the unit.
 SourceUnit addSource(URL url)
          Adds a source file to the unit.
 void addSources(File[] files)
          Adds a set of source files to the unit.
 void addSources(String[] paths)
          Adds a set of file paths to the unit.
 void applyToPrimaryClassNodes(CompilationUnit.LoopBodyForPrimaryClassNodeOperations body)
          A loop driver for applying operations to all primary ClassNodes in our AST.
 void applyToSourceUnits(CompilationUnit.LoopBodyForSourceUnitOperations body)
          A loop driver for applying operations to all SourceUnits.
 void classgen()
          Expands and canonicalizes the ASTs generated during parsing and conversion, then generates classes.
 void compile()
          Synonym for compile(Phases.ALL).
 void compile(int throughPhase)
          Compiles the compilation unit from sources.
 void configure(CompilerConfiguration configuration)
          Reconfigures the CompilationUnit.
 void convert()
          Builds ASTs for all parsed sources.
 CompileUnit getAST()
          Returns the CompileUnit that roots our AST.
 List getClasses()
          Get the GroovyClasses generated by compile().
 ClassNode getClassNode(String name)
          Convenience routine to get the named ClassNode.
 ClassNode getFirstClassNode()
          Convenience routine to get the first ClassNode, for when you are sure there is only one.
 boolean hasErrors()
          Returns true if there are any errors pending.
 Iterator iterator()
          Returns an iterator on the unit's SourceUnits.
protected  void mark()
          Updates the phase marker on all sources.
 void output()
          Outputs the generated class files to permanent storage.
 void parse()
          Parses all sources.
 void setClassgenCallback(CompilationUnit.ClassgenCallback visitor)
          Sets a ClassgenCallback.
 void setProgressCallback(CompilationUnit.ProgressCallback callback)
          Sets a ProgressCallback.
 void write(PrintWriter writer, Janitor janitor)
          Writes error messages to the specified PrintWriter.
 
Methods inherited from class org.codehaus.groovy.control.ProcessingUnit
addError, addError, addFatalError, addWarning, completePhase, fail, getClassLoader, getError, getErrorCount, getErrors, getException, getPhase, getPhaseDescription, getSyntaxError, getWarning, getWarningCount, getWarnings, gotoPhase, nextPhase, setClassLoader
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

sources

protected HashMap sources

names

protected ArrayList names

ast

protected CompileUnit ast

classes

protected ArrayList classes

verifier

protected Verifier verifier

debug

protected boolean debug

configured

protected boolean configured

classgenCallback

protected CompilationUnit.ClassgenCallback classgenCallback

progressCallback

protected CompilationUnit.ProgressCallback progressCallback
Constructor Detail

CompilationUnit

public CompilationUnit()
Initializes the CompilationUnit with defaults.


CompilationUnit

public CompilationUnit(ClassLoader loader)
Initializes the CompilationUnit with defaults except for class loader.


CompilationUnit

public CompilationUnit(CompilerConfiguration configuration)
Initializes the CompilationUnit with no security considerations.


CompilationUnit

public CompilationUnit(CompilerConfiguration configuration,
                       CodeSource security,
                       ClassLoader loader)
Initializes the CompilationUnit with a CodeSource for controlling security stuff and a class loader for loading classes.

Method Detail

configure

public void configure(CompilerConfiguration configuration)
Reconfigures the CompilationUnit.

Overrides:
configure in class ProcessingUnit

getAST

public CompileUnit getAST()
Returns the CompileUnit that roots our AST.


getClasses

public List getClasses()
Get the GroovyClasses generated by compile().


getFirstClassNode

public ClassNode getFirstClassNode()
Convenience routine to get the first ClassNode, for when you are sure there is only one.


getClassNode

public ClassNode getClassNode(String name)
Convenience routine to get the named ClassNode.


addSources

public void addSources(String[] paths)
Adds a set of file paths to the unit.


addSources

public void addSources(File[] files)
Adds a set of source files to the unit.


addSource

public SourceUnit addSource(File file)
Adds a source file to the unit.


addSource

public SourceUnit addSource(URL url)
Adds a source file to the unit.


addSource

public SourceUnit addSource(String name,
                            InputStream stream)
Adds a InputStream source to the unit.


addSource

public SourceUnit addSource(SourceUnit source)
Adds a SourceUnit to the unit.


iterator

public Iterator iterator()
Returns an iterator on the unit's SourceUnits.


addClassNode

public void addClassNode(ClassNode node)
Adds a ClassNode directly to the unit (ie. without source). Used primarily for testing support.


setClassgenCallback

public void setClassgenCallback(CompilationUnit.ClassgenCallback visitor)
Sets a ClassgenCallback. You can have only one, and setting it to null removes any existing setting.


setProgressCallback

public void setProgressCallback(CompilationUnit.ProgressCallback callback)
Sets a ProgressCallback. You can have only one, and setting it to null removes any existing setting.


compile

public void compile()
             throws CompilationFailedException
Synonym for compile(Phases.ALL).

Throws:
CompilationFailedException

compile

public void compile(int throughPhase)
             throws CompilationFailedException
Compiles the compilation unit from sources.

Throws:
CompilationFailedException

parse

public void parse()
           throws CompilationFailedException
Parses all sources.

Throws:
CompilationFailedException

convert

public void convert()
             throws CompilationFailedException
Builds ASTs for all parsed sources.

Throws:
CompilationFailedException

classgen

public void classgen()
              throws CompilationFailedException
Expands and canonicalizes the ASTs generated during parsing and conversion, then generates classes.

Throws:
CompilationFailedException

output

public void output()
            throws CompilationFailedException
Outputs the generated class files to permanent storage.

Throws:
CompilationFailedException

hasErrors

public boolean hasErrors()
Returns true if there are any errors pending.

Overrides:
hasErrors in class ProcessingUnit

mark

protected void mark()
             throws CompilationFailedException
Updates the phase marker on all sources.

Throws:
CompilationFailedException

applyToSourceUnits

public void applyToSourceUnits(CompilationUnit.LoopBodyForSourceUnitOperations body)
                        throws CompilationFailedException
A loop driver for applying operations to all SourceUnits. Automatically skips units that have already been processed through the current phase.

Throws:
CompilationFailedException

applyToPrimaryClassNodes

public void applyToPrimaryClassNodes(CompilationUnit.LoopBodyForPrimaryClassNodeOperations body)
                              throws CompilationFailedException
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.

Throws:
CompilationFailedException

write

public void write(PrintWriter writer,
                  Janitor janitor)
Writes error messages to the specified PrintWriter.

Overrides:
write in class ProcessingUnit


Copyright © 2003-2004 The Codehaus. All Rights Reserved.