Package org.codehaus.groovy.ast

# Class GenericsType

java.lang.Object
org.codehaus.groovy.ast.ASTNode
org.codehaus.groovy.ast.GenericsType

All Implemented Interfaces:
  `NodeMetaDataHandler`

public class GenericsType extends ASTNode
Represents generic type information for parameterized types in Groovy/Java, including type variables, wildcard types, and type bounds. Supports placeholders for type parameters, wildcards with upper/lower bounds, and tracks resolution state for multi-phase compilation. Provides compatibility checking for generic type constraints.

See Also:
  - `ClassNode`
  - `GenericsType.GenericsTypeName`

- ## Nested Class Summary

  Nested Classes
  Modifier and Type
  Class
  Description
  `static class`
  `GenericsType.GenericsTypeName`
  Represents the name of a `GenericsType` for use as a map key.

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `static final GenericsType[]`
  `EMPTY_ARRAY`

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `GenericsType(ClassNode basicType)`
  Creates a simple generics type with no bounds for a concrete type.
  `GenericsType(ClassNode type, ClassNode[] upperBounds, ClassNode lowerBound)`
  Creates a generics type with optional upper and lower bounds.

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static boolean`
  `areDistinctTypeVariables(ClassNode left, ClassNode right)`
  True when both nodes are type-variable placeholders with the same spelling but known, different generic declarations (class `T` vs method `T`).
  `static AnnotatedNode`
  `genericDeclarationOf(ClassNode type)`
  The generic declaration recorded on a placeholder `ClassNode`, or `null` if `type` is not a type variable or has no stamp.
  `AnnotatedNode`
  `getGenericDeclaration()`
  Returns the generic declaration that introduced this type variable, or `null` if this is not a type-variable placeholder or the declaration is unknown.
  `ClassNode`
  `getLowerBound()`
  Returns the lower bound for this wildcard type (e.g., Bound in "? super Bound"), or null if this is not a lower-bounded wildcard.
  `String`
  `getName()`
  Returns the name of this generic type.
  `ClassNode`
  `getType()`
  Returns the underlying `ClassNode` for this generic type.
  `ClassNode[]`
  `getUpperBounds()`
  Returns the upper bounds for this wildcard or placeholder type (e.g., `Bound` in `? extends Bound` or `Bound1 & Bound2` in `T extends Bound1 & Bound2`), or null if this type has no upper bounds.
  `boolean`
  `isCompatibleWith(ClassNode classNode)`
  Determines if the provided type is compatible with this generic type specification.
  `boolean`
  `isPlaceholder()`
  Returns true if this generic type represents a type variable placeholder (e.g., T in <T>).
  `boolean`
  `isResolved()`
  Returns true if this generic type has been resolved during compilation phases.
  `boolean`
  `isWildcard()`
  Returns true if this generic type represents a wildcard (e.g., ? or ? extends/super Bound).
  `void`
  `setGenericDeclaration(AnnotatedNode genericDeclaration)`
  Records the generic declaration that introduced this type variable.
  `static void`
  `setGenericDeclaration(GenericsType[] types, AnnotatedNode declaration)`
  Stamps every placeholder in `types` with `declaration`.
  `void`
  `setName(String name)`
  Sets the name of this generic type.
  `void`
  `setPlaceholder(boolean placeholder)`
  Marks this generic type as a placeholder type variable.
  `void`
  `setResolved(boolean resolved)`
  Marks this generic type as resolved.
  `void`
  `setType(ClassNode type)`
  Sets the underlying `ClassNode` for this generic type.
  `void`
  `setWildcard(boolean wildcard)`
  Marks this generic type as a wildcard.
  `String`
  `toString()`

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

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

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

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

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

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

- ## Field Details

  - ### EMPTY_ARRAY

    public static final GenericsType[] EMPTY_ARRAY

- ## Constructor Details

  - ### GenericsType

    public GenericsType(ClassNode type, ClassNode[] upperBounds, ClassNode lowerBound)
    Creates a generics type with optional upper and lower bounds. The type is marked as a placeholder if the provided type is a generics placeholder.
    Parameters:
      `type` - the `ClassNode` representing the main generic type (never null)
      `upperBounds` - optional array of upper bound `ClassNode`s (e.g., for "? extends Bound" or "T extends Bound")
      `lowerBound` - optional lower bound `ClassNode` (e.g., for "? super Bound")

  - ### GenericsType

    public GenericsType(ClassNode basicType)
    Creates a simple generics type with no bounds for a concrete type.
    Parameters:
      `basicType` - the `ClassNode` representing the concrete type (never null)

- ## Method Details

  - ### getType

    public ClassNode getType()
    Returns the underlying `ClassNode` for this generic type.
    Returns:
      the type

  - ### setType

    public void setType(ClassNode type)
    Sets the underlying `ClassNode` for this generic type.
    Parameters:
      `type` - the `ClassNode` to set (never null)
    Throws:
      `NullPointerException` - if type is null

  - ### toString

    public String toString()
    Overrides:
      `toString` in class `Object`

  - ### getName

    public String getName()
    Returns the name of this generic type. For wildcard types, returns "?"; otherwise returns the type name.
    Returns:
      the type name

  - ### setName

    public void setName(String name)
    Sets the name of this generic type.
    Parameters:
      `name` - the type name (never null)
    Throws:
      `NullPointerException` - if name is null

  - ### isResolved

    public boolean isResolved()
    Returns true if this generic type has been resolved during compilation phases.
    Returns:
      true if resolved

  - ### setResolved

    public void setResolved(boolean resolved)
    Marks this generic type as resolved. Setting to true also implicitly sets resolved=true.
    Parameters:
      `resolved` - true to mark as resolved

  - ### isPlaceholder

    public boolean isPlaceholder()
    Returns true if this generic type represents a type variable placeholder (e.g., T in <T>).
    Returns:
      true if this is a placeholder type variable

  - ### getGenericDeclaration

    public AnnotatedNode getGenericDeclaration()
    Returns the generic declaration that introduced this type variable, or `null` if this is not a type-variable placeholder or the declaration is unknown.
    Returns:
      the declaring `ClassNode` or `MethodNode`, or `null`
    Since:
      6.0.0

  - ### setGenericDeclaration

    public void setGenericDeclaration(AnnotatedNode genericDeclaration)
    Records the generic declaration that introduced this type variable. Callers must pass the class, method or constructor that owns the type-parameter section, not a use-site `ClassNode`.
    Parameters:
      `genericDeclaration` - the declaring `ClassNode` or `MethodNode`
    Since:
      6.0.0

  - ### setGenericDeclaration

    public static void setGenericDeclaration(GenericsType[] types, AnnotatedNode declaration)
    Stamps every placeholder in `types` with `declaration`.
    Since:
      6.0.0

  - ### genericDeclarationOf

    public static AnnotatedNode genericDeclarationOf(ClassNode type)
    The generic declaration recorded on a placeholder `ClassNode`, or `null` if `type` is not a type variable or has no stamp.
    Since:
      6.0.0

  - ### areDistinctTypeVariables

    public static boolean areDistinctTypeVariables(ClassNode left, ClassNode right)
    True when both nodes are type-variable placeholders with the same spelling but known, different generic declarations (class `T` vs method `T`). Different names are left to the existing bound and redirect checks. Unstamped placeholders keep name-only behaviour.
    Since:
      6.0.0

  - ### setPlaceholder

    public void setPlaceholder(boolean placeholder)
    Marks this generic type as a placeholder type variable. Setting to true also sets resolved=true and clears the wildcard flag, since placeholders and wildcards are mutually exclusive.
    Parameters:
      `placeholder` - true to mark as a placeholder

  - ### isWildcard

    public boolean isWildcard()
    Returns true if this generic type represents a wildcard (e.g., ? or ? extends/super Bound).
    Returns:
      true if this is a wildcard type

  - ### setWildcard

    public void setWildcard(boolean wildcard)
    Marks this generic type as a wildcard. Clears the placeholder flag if set, since wildcards and placeholders are mutually exclusive.
    Parameters:
      `wildcard` - true to mark as a wildcard

  - ### getLowerBound

    public ClassNode getLowerBound()
    Returns the lower bound for this wildcard type (e.g., Bound in "? super Bound"), or null if this is not a lower-bounded wildcard.
    Returns:
      the lower bound `ClassNode`, or null

  - ### getUpperBounds

    public ClassNode[] getUpperBounds()
    Returns the upper bounds for this wildcard or placeholder type (e.g., `Bound` in `? extends Bound` or `Bound1 & Bound2` in `T extends Bound1 & Bound2`), or null if this type has no upper bounds.
    Returns:
      array of upper bound `ClassNode`s, or null

  - ### isCompatibleWith

    public boolean isCompatibleWith(ClassNode classNode)
    Determines if the provided type is compatible with this generic type specification. The check is complete and recursive, including nested generic parameters. Accounts for wildcards, placeholders, bounds, and generic type covariance rules.
    Parameters:
      `classNode` - the `ClassNode` to check for compatibility
    Returns:
      true if classNode satisfies this generic type specification