Package org.codehaus.groovy.ast

# Class ImportNode

java.lang.Object
org.codehaus.groovy.ast.ASTNode
org.codehaus.groovy.ast.AnnotatedNode
org.codehaus.groovy.ast.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:
  - `ClassNode`
  - `AnnotatedNode`

- ## Field Summary

  ### Fields inherited from interface groovy.lang.groovydoc.GroovydocHolder

  `DOC_COMMENT`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `ImportNode(String packageName)`
  Creates an import for all types in a package (e.g., `import pack.*`).
  `ImportNode(ClassNode type)`
  Creates a static wildcard import for all static members of a type (e.g., `import static pack.Type.*`).
  `ImportNode(ClassNode type, String alias)`
  Creates an import for a single type (e.g., `import pack.Type` or `import pack.Type as Alias`).
  `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`).

- ## Method Summary

  Modifier and Type
  Method
  Description
  `boolean`
  `equals(Object that)`
  `String`
  `getAlias()`
  Returns the alias name for this import, if specified.
  `String`
  `getClassName()`
  Returns the fully qualified class name of the imported type.
  `String`
  `getFieldName()`
  Returns the name of the static field or method being imported via static import.
  `String`
  `getPackageName()`
  Returns the package name for a wildcard import, or the package containing the imported type or static member.
  `String`
  `getText()`
  Generates the text representation of this import statement as it would appear in source code.
  `ClassNode`
  `getType()`
  Returns the `ClassNode` being imported or providing static members.
  `int`
  `hashCode()`
  `boolean`
  `isStar()`
  `boolean`
  `isStatic()`
  `void`
  `setType(ClassNode type)`
  Updates the `ClassNode` being imported.
  `void`
  `visit(GroovyCodeVisitor visitor)`
  Accepts a code visitor for AST traversal and transformation.

  ### Methods inherited from class org.codehaus.groovy.ast.AnnotatedNode

  `addAnnotation, addAnnotation, addAnnotations, getAnnotations, getAnnotations, getDeclaringClass, getGroovydoc, getInstance, hasNoRealSourcePosition, isSynthetic, setDeclaringClass, setHasNoRealSourcePosition, setSynthetic`

  ### Methods inherited from class org.codehaus.groovy.ast.ASTNode

  `copyNodeMetaData, getColumnNumber, getLastColumnNumber, getLastLineNumber, getLineNumber, getMetaDataMap, setColumnNumber, setLastColumnNumber, setLastLineNumber, setLineNumber, setMetaDataMap, setSourcePosition`

  ### Methods inherited from class java.lang.Object

  `clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait`

  ### Methods inherited from interface org.codehaus.groovy.ast.NodeMetaDataHandler

  `copyNodeMetaData, getNodeMetaData, getNodeMetaData, getNodeMetaData, newMetaDataMap, putNodeMetaData, removeNodeMetaData, setNodeMetaData`

- ## 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