Package org.codehaus.groovy.syntax
Class ASTHelper
java.lang.Object
org.codehaus.groovy.syntax.ASTHelper
Common base class providing shared AST helper methods used by both the classic and new parsers.
Manages module-level information including the package, imports, and related AST construction utilities.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprotected ModuleNodeThe output module node being constructed.protected static final MapStatic map for type resolutions, cleared on each build for safety. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs an ASTHelper with no controller or class loader.ASTHelper(SourceUnit controller, ClassLoader classLoader) Constructs an ASTHelper with a source unit and class loader. -
Method Summary
Modifier and TypeMethodDescriptionprotected voidAdds a type import to the module.protected voidAdds a type import to the module with optional annotations.protected voidaddStarImport(String importPackage) Adds a star import (imports all classes in a package) to the module.protected voidaddStarImport(String importPackage, List<AnnotationNode> annotations) Adds a star import (imports all classes in a package) to the module with optional annotations.protected voidaddStaticImport(ClassNode type, String name, String alias) Adds a static import to the module.protected voidaddStaticImport(ClassNode type, String name, String alias, List<AnnotationNode> annotations) Adds a static import to the module with optional annotations.protected voidaddStaticStarImport(ClassNode type, String importClass) Adds a static star import (imports all static members) to the module.protected voidaddStaticStarImport(ClassNode type, String importClass, List<AnnotationNode> annotations) Adds a static star import (imports all static members) to the module with optional annotations.protected StringEquivalent todot(base, "").static StringJoins two names with a dot, returning the base name unchanged if empty.Returns the class loader used for resolving external types.Returns the source unit controlling this helper.Returns the package name in which this module is located.protected voidInitializes the output module node and clears resolution cache.voidsetClassLoader(ClassLoader classLoader) Sets the class loader to use for resolving external types.voidsetController(SourceUnit controller) Sets the source unit that controls this helper.setPackage(String packageName, List<AnnotationNode> annotations) Sets the package for this module with optional annotations.voidsetPackageName(String packageName) Sets the package name for this module (without trailing dot).
-
Field Details
-
output
The output module node being constructed. -
resolutions
Static map for type resolutions, cleared on each build for safety.
-
-
Constructor Details
-
ASTHelper
Constructs an ASTHelper with a source unit and class loader.- Parameters:
controller- theSourceUnitthat controls this helperclassLoader- theClassLoaderfor resolving external types
-
ASTHelper
public ASTHelper()Constructs an ASTHelper with no controller or class loader. These must be set later viasetController(SourceUnit)andsetClassLoader(ClassLoader).
-
-
Method Details
-
getPackageName
Returns the package name in which this module is located.- Returns:
- the package name, or
nullif not set
-
setPackageName
Sets the package name for this module (without trailing dot). Convenience method equivalent tosetPackage(packageName, new ArrayList<>()).- Parameters:
packageName- the package name
-
setPackage
Sets the package for this module with optional annotations.- Parameters:
packageName- the package name (will have a dot appended if non-empty)annotations- annotations to apply to the package- Returns:
- the created
PackageNode
-
getClassLoader
Returns the class loader used for resolving external types.- Returns:
- the
ClassLoader, ornullif not set
-
setClassLoader
Sets the class loader to use for resolving external types.- Parameters:
classLoader- theClassLoaderto use
-
getController
Returns the source unit controlling this helper.- Returns:
- the
SourceUnit, ornullif not set
-
setController
Sets the source unit that controls this helper.- Parameters:
controller- theSourceUnitto use
-
dot
Joins two names with a dot, returning the base name unchanged if empty. Useful for constructing fully qualified names.- Parameters:
base- the base name (typically a package), ornullname- the name to append (typically a class name)- Returns:
- the joined name, or
nameif base isnullor empty
-
makeModule
protected void makeModule()Initializes the output module node and clears resolution cache. Called at the start of parsing. -
dot
Equivalent todot(base, ""). Returns the base with a trailing dot, or empty string. Useful for constructing package names.- Parameters:
base- the base name (typically a package)- Returns:
- the base with a trailing dot, or empty string if base is empty
-
addImport
Adds a type import to the module. Equivalent toaddImport(type, name, aliasName, new ArrayList<>()).- Parameters:
type- theClassNodeto importname- the original namealiasName- the alias name to use (or the same as name if not aliased)
-
addImport
protected void addImport(ClassNode type, String name, String aliasName, List<AnnotationNode> annotations) Adds a type import to the module with optional annotations.- Parameters:
type- theClassNodeto importname- the original namealiasName- the alias name to use (or the same as name if not aliased)annotations- annotations for this import
-
addStaticImport
Adds a static import to the module. Equivalent toaddStaticImport(type, name, alias, new ArrayList<>()).- Parameters:
type- theClassNodecontaining the static membername- the static member namealias- the alias to use in the code
-
addStaticImport
protected void addStaticImport(ClassNode type, String name, String alias, List<AnnotationNode> annotations) Adds a static import to the module with optional annotations.- Parameters:
type- theClassNodecontaining the static membername- the static member namealias- the alias to use in the codeannotations- annotations for this import
-
addStaticStarImport
Adds a static star import (imports all static members) to the module. Equivalent toaddStaticStarImport(type, importClass, new ArrayList<>()).- Parameters:
type- theClassNodecontaining the static membersimportClass- the class name
-
addStaticStarImport
protected void addStaticStarImport(ClassNode type, String importClass, List<AnnotationNode> annotations) Adds a static star import (imports all static members) to the module with optional annotations.- Parameters:
type- theClassNodecontaining the static membersimportClass- the class nameannotations- annotations for this import
-
addStarImport
Adds a star import (imports all classes in a package) to the module. Equivalent toaddStarImport(importPackage, new ArrayList<>()).- Parameters:
importPackage- the package name
-
addStarImport
Adds a star import (imports all classes in a package) to the module with optional annotations.- Parameters:
importPackage- the package nameannotations- annotations for this import
-