public class ModuleNode
extends ASTNode
Represents a module, which consists typically of a class declaration but could include some imports, some statements and multiple classes intermixed with statements like scripts in Python or Ruby
| Constructor and description |
|---|
ModuleNode(SourceUnit context)Creates a module node for the specified compilation context. |
ModuleNode(CompileUnit unit)Creates a module node for batch compilation. |
| Type Params | Return Type | Name and description |
|---|---|---|
|
public void |
addClass(ClassNode node)Adds a class definition to this module. |
|
public void |
addImport(String name, ClassNode type)Registers a regular import (e.g., import java.util.List).
|
|
public void |
addImport(String name, ClassNode type, List<AnnotationNode> annotations)Registers a regular import with optional annotations. |
|
public void |
addMethod(MethodNode node)Adds a module-level method definition. |
|
public void |
addModuleStarImport(String packageName, List<AnnotationNode> annotations)Adds a module-level star import with optional annotations. |
|
public void |
addStarImport(String packageName)Registers a wildcard import (e.g., import java.util.*).
|
|
public void |
addStarImport(String packageName, List<AnnotationNode> annotations)Registers a wildcard import with optional annotations. |
|
public void |
addStatement(Statement node)Adds a statement to the module's statement block. |
|
public void |
addStaticImport(ClassNode type, String memberName, String simpleName)Registers a static import (e.g., import static java.lang.Math.PI).
|
|
public void |
addStaticImport(ClassNode type, String memberName, String simpleName, List<AnnotationNode> annotations)Registers a static import with optional annotations. |
|
public void |
addStaticStarImport(String name, ClassNode type)Registers a static wildcard import (e.g., import static java.lang.Math.*).
|
|
public void |
addStaticStarImport(String name, ClassNode type, List<AnnotationNode> annotations)Registers a static wildcard import with optional annotations. |
|
protected ClassNode |
createStatementsClass()Creates a synthetic class wrapping this module's statement and method definitions. |
|
protected String |
extractClassFromFileDescription()Extracts the class name from the module's description (typically a file path). |
|
public List<ClassNode> |
getClasses()Returns the module's class definitions. |
|
public SourceUnit |
getContext()Returns the source unit for this module (single-file compilation context). |
|
public String |
getDescription()Returns a description of this module. |
|
public ImportNode |
getImport(String alias)Looks up an import node by its alias name. |
|
public ClassNode |
getImportType(String alias)Looks up the class type for a regular import by its alias name. |
|
public List<ImportNode> |
getImports()Returns a copy of the module's regular import declarations. |
|
public String |
getMainClassName()Returns the simple name of the main class for this module. |
|
public List<MethodNode> |
getMethods()Returns the module's top-level method definitions. |
|
public List<ImportNode> |
getModuleStarImports()Registers a module-level star import (e.g., import module java.base).
|
|
public PackageNode |
getPackage()Returns the package node for this module. |
|
public String |
getPackageName()Returns the module's package name. |
|
public ClassNode |
getScriptClassDummy()Returns a synthetic class wrapping this module's statements and methods. |
|
public List<ImportNode> |
getStarImports()Returns wildcard (star) import declarations (e.g., import java.util.*).
|
|
public BlockStatement |
getStatementBlock()Returns the block of statements defined at module scope. |
|
public Map<String, ImportNode> |
getStaticImports()Returns static import declarations (e.g., import static java.lang.Math.PI).
|
|
public Map<String, ImportNode> |
getStaticStarImports()Returns static wildcard import declarations (e.g., import static java.lang.Math.*).
|
|
public CompileUnit |
getUnit()Returns the compile unit managing this module (batch compilation context). |
|
public boolean |
hasImportsResolved()Checks whether imports have been resolved for this module. |
|
public boolean |
hasPackage()Checks whether this module declares a package. |
|
public boolean |
hasPackageName()Checks whether this module has a named package (not default package). |
|
public boolean |
isEmpty()Checks whether this module is empty (no classes or statements). |
|
public void |
setDescription(String description)Sets a description for this module (typically the source file name). |
|
public void |
setImportsResolved(boolean importsResolved)Marks whether imports have been resolved for this module. |
|
public void |
setPackage(PackageNode packageNode)Sets the package declaration for this module. |
|
public void |
setPackageName(String packageName)Sets the package for this module by name. |
|
public void |
sortClasses()Sorts classes in dependency order based on class hierarchy. |
|
public void |
visit(GroovyCodeVisitor visitor)Accepts a code visitor for AST traversal and processing. |
| Methods inherited from class | Name |
|---|---|
class ASTNode |
copyNodeMetaData, getColumnNumber, getLastColumnNumber, getLastLineNumber, getLineNumber, getMetaDataMap, getText, setColumnNumber, setLastColumnNumber, setLastLineNumber, setLineNumber, setMetaDataMap, setSourcePosition, visit |
Creates a module node for the specified compilation context. Used when compiling a single Groovy file with full source location tracking.
context - the source unit providing file path, line mapping, and error contextCreates a module node for batch compilation. Used when compiling multiple classes in a single compilation batch.
unit - the compile unit managing this module's classesAdds a class definition to this module. If this is the first class added, its name is recorded as the main class name. The class's containing module is set to this module, and it's registered with the compile unit if present.
node - the ClassNode to add Registers a regular import (e.g., import java.util.List).
The alias name will be used to reference the imported class.
name - the alias name for this importtype - the ClassNode to importRegisters a regular import with optional annotations. The alias name will be used to reference the imported class.
name - the alias name for this importtype - the ClassNode to importannotations - annotations to attach to this importAdds a module-level method definition. Module-level methods are typically found in scripts or in trait interface definitions.
node - the MethodNode to addAdds a module-level star import with optional annotations. These are tracked separately from regular wildcard imports for import resolution.
packageName - the module package nameannotations - annotations to attach to this import Registers a wildcard import (e.g., import java.util.*).
All public classes in the package become available without qualification.
packageName - the package name (e.g., "java.util")Registers a wildcard import with optional annotations. All public classes in the package become available without qualification.
packageName - the package name (e.g., "java.util")annotations - annotations to attach to this importAdds a statement to the module's statement block. Module-level statements are collected and later wrapped in a synthetic class during compilation (controlled by createClassForStatements).
node - the Statement to add Registers a static import (e.g., import static java.lang.Math.PI).
A specific static member from a class becomes available without qualification.
type - the ClassNode containing the static membermemberName - the name of the static member (field or method)simpleName - the alias name by which this member is importedRegisters a static import with optional annotations. A specific static member from a class becomes available without qualification.
type - the ClassNode containing the static membermemberName - the name of the static member (field or method)simpleName - the alias name by which this member is importedannotations - annotations to attach to this import Registers a static wildcard import (e.g., import static java.lang.Math.*).
All static members from a class become available without qualification.
name - the package/class name for this static star importtype - the ClassNode from which to import static membersRegisters a static wildcard import with optional annotations. All static members from a class become available without qualification.
name - the package/class name for this static star importtype - the ClassNode from which to import static membersannotations - annotations to attach to this import Creates a synthetic class wrapping this module's statement and method definitions.
Generates a main(String[]) method for script execution and a run() method for statement execution.
Only invoked when the module contains module-level code (scripts or statements).
Extracts the class name from the module's description (typically a file path). Strips file extensions, path separators, and URI schemes to derive a valid class name. Used when generating synthetic script wrapper classes.
Returns the module's class definitions.
If createClassForStatements is true and there are module-level statements,
methods, or this is a package-info script, a synthetic class is created to wrap them.
This class is automatically inserted at the beginning of the list.
The flag is then reset to prevent future synthesis.
Returns the source unit for this module (single-file compilation context). Provides access to source code, line mappings, and error reporting.
Returns a description of this module. Typically the source file name if available (via context), otherwise a user-supplied description.
Looks up an import node by its alias name. Searches regular imports and returns the matching node. Caches import alias mappings as node metadata for performance.
alias - the imported name to look upLooks up the class type for a regular import by its alias name. Resolves simple names to the actual class they reference.
alias - the imported name to look up Returns a copy of the module's regular import declarations.
Includes both simple class imports (e.g., import java.util.List)
and aliased imports. Does not include star imports or static imports.
Returns the simple name of the main class for this module. For scripts, this is the synthetic wrapper class name. For modules with classes, this is typically the first class added.
Returns the module's top-level method definitions. These are module-level methods typically found in scripts or trait interfaces.
Registers a module-level star import (e.g., import module java.base).
Returns module star imports, which are separate from regular wildcard imports
for proper JLS 6.4.1 shadowing resolution (type-import-on-demand shadowing).
import module declarationsReturns the package node for this module. Contains the package declaration and associated annotations.
Returns the module's package name. Returns null if no package is declared (uses the default package).
Returns a synthetic class wrapping this module's statements and methods.
Used for scripts: module-level code is collected and placed in a generated class
with a main(String[]) method. Configures the base class based on compiler config.
Returns wildcard (star) import declarations (e.g., import java.util.*).
These imports make all public classes in a package available without qualification.
Returns the block of statements defined at module scope. These statements become part of the script's run() method (if module is a script) or remain at module level (if module contains only classes).
Returns static import declarations (e.g., import static java.lang.Math.PI).
Maps simple names to ImportNode objects for static member imports.
Returns static wildcard import declarations (e.g., import static java.lang.Math.*).
Maps package names to ImportNode objects for static star imports.
Returns the compile unit managing this module (batch compilation context). Null if this module was compiled individually.
Checks whether imports have been resolved for this module. Import resolution converts import aliases to fully qualified class names and processes import conflicts. This flag tracks whether that phase is complete.
Checks whether this module declares a package.
Checks whether this module has a named package (not default package).
Checks whether this module is empty (no classes or statements). An empty module compiles to no bytecode output.
Sets a description for this module (typically the source file name).
description - the description to setMarks whether imports have been resolved for this module. Set to true after the import resolution phase completes.
importsResolved - true to mark imports as resolvedSets the package declaration for this module.
packageNode - the PackageNode for this moduleSets the package for this module by name. Creates a new PackageNode with the given package name.
packageName - the fully qualified package nameSorts classes in dependency order based on class hierarchy. Inner classes and dependent classes are ordered after their dependencies. This ensures that base classes are defined before derived classes during compilation. Does nothing if the module is empty or contains only one class.
Accepts a code visitor for AST traversal and processing. Implementation is empty; module-level visitation typically proceeds directly to classes and methods contained within.
visitor - the GroovyCodeVisitor to process this nodeCopyright © 2003-2026 The Apache Software Foundation. All rights reserved.