Package org.codehaus.groovy.ast
Class CompileUnit
java.lang.Object
org.codehaus.groovy.ast.CompileUnit
- All Implemented Interfaces:
NodeMetaDataHandler
Represents a complete compilation step involving one or more
ModuleNode instances.
A single CompileUnit is shared across all modules and classes compiled in one invocation,
serving as a central repository for class metadata, import resolution, and compilation state.
The compile unit maintains mappings from fully qualified class names to ClassNode instances,
tracks which classes require compilation, stores source file locations, and manages generated inner classes.
It is attached to MethodNode and ClassNode instances to enable
fully qualified name resolution, import lookup, and other compilation-time queries.
- See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCompileUnit(GroovyClassLoader classLoader, CodeSource codeSource, CompilerConfiguration config) Creates a compile unit with the given classloader, code source, and compiler configuration.CompileUnit(GroovyClassLoader classLoader, CompilerConfiguration config) Creates a compile unit with the given classloader and compiler configuration. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a single class to this compilation unit, checking for duplicates and reporting errors.voidaddClasses(List<ClassNode> list) Adds all classes in the given list to this compilation unit.voidaddClassNodeToCompile(ClassNode node, SourceUnit location) Marks a class for compilation and associates it with its source location.voidAdds a generated inner class to the map of generated inner classes.voidaddModule(ModuleNode node) Adds a module to this compilation unit along with all its classes.Looks up a class by its fully qualified name, checking both compiled classes and classes pending compilation.Returns all classes from all modules in this compilation unit.Returns the map of classes pending compilation.Returns the Groovy classloader used for loading classes during compilation.Returns the code source used for Java security permissions.Returns the compiler configuration for this compilation.getGeneratedInnerClass(String name) Retrieves a generated inner class by name.Returns an unmodifiable view of all generated inner classes.Map<?,?> Returns the metadata map for storing compilation-phase metadata.Returns all modules being compiled in this unit.getScriptSourceLocation(String className) Retrieves the source file location for a class by name.booleanDeprecated.Deprecated.UsegetClassesToCompile()insteadvoidsetMetaDataMap(Map<?, ?> metaDataMap) Sets the metadata map for storing compilation-phase metadata.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.codehaus.groovy.ast.NodeMetaDataHandler
copyNodeMetaData, getNodeMetaData, getNodeMetaData, getNodeMetaData, newMetaDataMap, putNodeMetaData, removeNodeMetaData, setNodeMetaData
-
Constructor Details
-
CompileUnit
Creates a compile unit with the given classloader and compiler configuration. The code source is set tonull.- Parameters:
classLoader- theGroovyClassLoaderto use for loading classesconfig- theCompilerConfigurationdefining compilation behavior
-
CompileUnit
public CompileUnit(GroovyClassLoader classLoader, CodeSource codeSource, CompilerConfiguration config) Creates a compile unit with the given classloader, code source, and compiler configuration.- Parameters:
classLoader- theGroovyClassLoaderto use for loading classescodeSource- theCodeSourcefor code permissions ornullconfig- theCompilerConfigurationdefining compilation behavior
-
-
Method Details
-
getConfig
Returns the compiler configuration for this compilation.- Returns:
- the
CompilerConfiguration
-
getClassLoader
Returns the Groovy classloader used for loading classes during compilation.- Returns:
- the
GroovyClassLoader
-
getCodeSource
Returns the code source used for Java security permissions.- Returns:
- the
CodeSourceornull
-
getMetaDataMap
Returns the metadata map for storing compilation-phase metadata. ImplementsNodeMetaDataHandlerfor consistency with AST nodes.- Specified by:
getMetaDataMapin interfaceNodeMetaDataHandler- Returns:
- the metadata map or
null
-
setMetaDataMap
Sets the metadata map for storing compilation-phase metadata.- Specified by:
setMetaDataMapin interfaceNodeMetaDataHandler- Parameters:
metaDataMap- the metadata map ornull
-
getModules
Returns all modules being compiled in this unit.- Returns:
- a list of
ModuleNodeinstances
-
getClasses
Returns all classes from all modules in this compilation unit.- Returns:
- a list of all
ClassNodeinstances across all modules
-
getClass
Looks up a class by its fully qualified name, checking both compiled classes and classes pending compilation. Returnsnullif the name does not exist in the current compilation unit (does not check .class files on classpath).- Parameters:
name- the fully qualified class name- Returns:
- the
ClassNodeornullif not found in this unit
-
getClassesToCompile
Returns the map of classes pending compilation. These are typically forward references or generated classes queued for later compilation phases.- Returns:
- a map from fully qualified names to
ClassNodeinstances pending compilation
-
getScriptSourceLocation
Retrieves the source file location for a class by name. Useful for mapping compiled classes back to their sourceSourceUnit.- Parameters:
className- the fully qualified class name- Returns:
- the
SourceUnitwhere this class was defined, ornullif not tracked
-
getGeneratedInnerClasses
Returns an unmodifiable view of all generated inner classes. These are typically inner classes generated by Groovy transformations or the compiler.- Returns:
- an unmodifiable map from inner class names to
InnerClassNodeinstances
-
getGeneratedInnerClass
Retrieves a generated inner class by name.- Parameters:
name- the fully qualified inner class name- Returns:
- the
InnerClassNodeornullif not found
-
hasClassNodeToCompile
Deprecated.UsegetClassesToCompile()insteadChecks if there are any classes pending compilation.- Returns:
trueifclassesToCompileis not empty
-
iterateClassNodeToCompile
Deprecated.UsegetClassesToCompile()insteadReturns an iterator over the names of classes pending compilation.- Returns:
- an iterator over fully qualified class names
-
addModule
Adds a module to this compilation unit along with all its classes. If the module isnull(indicating a parsing error), it is silently ignored. Sets this compile unit as the module's owning unit.- Parameters:
node- theModuleNodeto add, ornull
-
addClasses
Adds all classes in the given list to this compilation unit.- Parameters:
list- the list ofClassNodeinstances to add
-
addClass
Adds a single class to this compilation unit, checking for duplicates and reporting errors. If a duplicate is detected, emits aSyntaxExceptiondescribing the conflict and its possible resolution. Removes any pending compilation entry for the class if it exists.- Parameters:
node- theClassNodeto add
-
addClassNodeToCompile
Marks a class for compilation and associates it with its source location. This method does not perform actual compilation—it is only a marker that the class should be compiled by theCompilationUnitat the end of a parse step. No marked class should remain by the end of compilation.- Parameters:
node- theClassNodeto compilelocation- theSourceUnitwhere this class is defined
-
addGeneratedInnerClass
Adds a generated inner class to the map of generated inner classes. These are inner classes created during compilation, typically by transformations.- Parameters:
icn- theInnerClassNodeto register as generated
-
getClassesToCompile()instead