Class ImportNode

All Implemented Interfaces:
GroovydocHolder<AnnotatedNode>, NodeMetaDataHandler

public class ImportNode extends AnnotatedNode
Represents an import statement in Groovy source code, supporting single-type imports, wildcard imports, and static imports. Provides a unified representation for different import styles with methods to query import type and generate display text.
See Also:
  • Constructor Details

    • ImportNode

      public ImportNode(ClassNode type, String alias)
      Creates an import for a single type (e.g., import pack.Type or import pack.Type as Alias).
      Parameters:
      type - the ClassNode being imported (never null)
      alias - an optional alias name, or null if no alias is specified
      Throws:
      NullPointerException - if type is null
    • ImportNode

      public ImportNode(String packageName)
      Creates an import for all types in a package (e.g., import pack.*).
      Parameters:
      packageName - the fully qualified package name (never null)
      Throws:
      NullPointerException - if packageName is null
    • ImportNode

      public ImportNode(ClassNode type)
      Creates a static wildcard import for all static members of a type (e.g., import static pack.Type.*).
      Parameters:
      type - the ClassNode whose static members are imported (never null)
      Throws:
      NullPointerException - if type is null
    • ImportNode

      public ImportNode(ClassNode type, String fieldName, String alias)
      Creates a static import for a specific field or method of a type (e.g., import static pack.Type.name or import static pack.Type.name as alias).
      Parameters:
      type - the ClassNode containing the static member (never null)
      fieldName - the name of the static field or method being imported (never null)
      alias - an optional alias name, or null if no alias is specified
      Throws:
      NullPointerException - if type or fieldName is null
  • Method Details

    • getText

      public String getText()
      Generates the text representation of this import statement as it would appear in source code. For example: "import java.util.List", "import static java.util.Collections.*", etc.
      Overrides:
      getText in class ASTNode
      Returns:
      the text representation of this import
    • isStar

      public boolean isStar()
    • isStatic

      public boolean isStatic()
    • getAlias

      public String getAlias()
      Returns the alias name for this import, if specified.
      Returns:
      the alias, or null if no alias is provided
    • getClassName

      public String getClassName()
      Returns the fully qualified class name of the imported type.
      Returns:
      the class name, or null if this import refers to a package
    • getFieldName

      public String getFieldName()
      Returns the name of the static field or method being imported via static import.
      Returns:
      the field name, or null if this is not a static field/method import
    • getPackageName

      public String getPackageName()
      Returns the package name for a wildcard import, or the package containing the imported type or static member.
      Returns:
      the package name, or null if this import references a specific type
    • getType

      public ClassNode getType()
      Returns the ClassNode being imported or providing static members.
      Returns:
      the imported type, or null if this is a package-level wildcard import
    • setType

      public void setType(ClassNode type)
      Updates the ClassNode being imported. This may be used during compilation phases to replace placeholder types with resolved types.
      Parameters:
      type - the new ClassNode (never null)
      Throws:
      NullPointerException - if type is null
    • equals

      public boolean equals(Object that)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • visit

      public void visit(GroovyCodeVisitor visitor)
      Description copied from class: ASTNode
      Accepts a code visitor for AST traversal and transformation. Subclasses must implement this method to support visitor pattern-based processing. The visitor pattern enables decoupling of AST structure from processing logic.
      Overrides:
      visit in class ASTNode
      Parameters:
      visitor - the GroovyCodeVisitor to process this node