Package: org.codehaus.groovy.ast.tools

## [Java] Class GenericsUtils

- org.codehaus.groovy.ast.tools.GenericsUtils

-     public class GenericsUtils
      extends Object

  Utility methods to deal with generic types.

- - ### Field Summary

    Fields
    Modifiers | Name | Description
    --- | --- | ---
    `static GenericsType[]` | `EMPTY_GENERICS_ARRAY` |
    `static String` | `JAVA_LANG_OBJECT` |

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public void` | `addError(String msg, ASTNode expr)`
    `` | `public static Map<String, ClassNode>` | `addMethodGenerics(MethodNode current, Map<String, ClassNode> oldSpec)`
    `` | `public static GenericsType[]` | `alignGenericTypes(GenericsType[] redirectGenericTypes, GenericsType[] parameterizedTypes, GenericsType[] alignmentTarget)` Given a parameterized type and a generic type information, aligns actual type parameters.
    `` | `public static GenericsType[]` | `applyGenericsContextToPlaceHolders(Map<String, ClassNode> genericsSpec, GenericsType[] oldPlaceHolders)` Transforms generics types from an old context to a new context using the given spec.
    `` | `public static GenericsType` | `buildWildcardType(ClassNode upperBounds)` Generates a wildcard generic type in order to be used for checks against class nodes.
    `` | `public static void` | `clearParameterizedTypeCache()` Clears the parameterized type cache.
    `` | `public static MethodNode` | `correctToGenericsSpec(Map<String, ClassNode> genericsSpec, MethodNode mn)`
    `` | `public static ClassNode` | `correctToGenericsSpec(Map<String, ClassNode> genericsSpec, GenericsType type)`
    `` | `public static ClassNode` | `correctToGenericsSpec(Map<String, ClassNode> genericsSpec, ClassNode type)`
    `` | `public static ClassNode` | `correctToGenericsSpecRecurse(Map<String, ClassNode> genericsSpec, ClassNode type)`
    `` | `public static ClassNode[]` | `correctToGenericsSpecRecurse(Map<String, ClassNode> genericsSpec, ClassNode[] types)` Since: 2.4.1
    `` | `public static ClassNode` | `correctToGenericsSpecRecurse(Map<String, ClassNode> genericsSpec, ClassNode type, List<String> exclusions)`
    `` | `public static Map<String, ClassNode>` | `createGenericsSpec(ClassNode type)`
    `` | `public static Map<String, ClassNode>` | `createGenericsSpec(ClassNode type, Map<String, ClassNode> oldSpec)`
    `` | `public static Map<GenericsType.GenericsTypeName, GenericsType>` | `extractPlaceholders(ClassNode type)` Returns the type parameter/argument relationships of the specified type.
    `` | `public static void` | `extractPlaceholders(ClassNode type, Map<GenericsType.GenericsTypeName, GenericsType> placeholders)` Populates the supplied map with the type parameter/argument relationships of the specified type.
    `` | `public static void` | `extractSuperClassGenerics(ClassNode type, ClassNode target, Map<String, ClassNode> spec)`
    `` | `public static ClassNode` | `findActualTypeByGenericsPlaceholderName(String placeholderName, Map<GenericsType, GenericsType> genericsPlaceholderAndTypeMap)` Gets the actual type according to the placeholder name.
    `` | `public static ClassNode` | `findParameterizedType(ClassNode genericsClass, ClassNode actualType)` Convenience method for findParameterizedType(ClassNode,ClassNode,boolean) when `tryToFindExactType` is `false`.
    `` | `public static ClassNode` | `findParameterizedType(ClassNode genericsClass, ClassNode actualType, boolean tryToFindExactType)` Gets the parameterized type by searching the whole class hierarchy according to generics class and actual receiver.
    `` | `public static ClassNode` | `findParameterizedTypeFromCache(ClassNode genericsClass, ClassNode actualType)` Convenience method for findParameterizedTypeFromCache(ClassNode, ClassNode, boolean) when the `tryToFindExactType` boolean is `false`.
    `` | `public static ClassNode` | `findParameterizedTypeFromCache(ClassNode genericsClass, ClassNode actualType, boolean tryToFindExactType)` Try to get the parameterized type from the cache.
    `` | `public static ClassNode` | `getSuperClass(ClassNode type, ClassNode target)`
    `` | `public static boolean` | `hasNonPlaceHolders(ClassNode type)` Checks if the type has any non-placeholder (aka resolved) generics.
    `` | `public static boolean` | `hasPlaceHolders(ClassNode type)` Checks if the type has any placeholder (aka unresolved) generics.
    `` | `public static boolean` | `hasUnresolvedGenerics(ClassNode type)` Checks for any placeholder (aka unresolved) generics.
    `` | `public static ClassNode` | `makeClassSafe(Class klass)`
    `` | `public static ClassNode` | `makeClassSafe0(ClassNode type, GenericsType genericTypes)`
    `` | `public static ClassNode` | `makeClassSafeWithGenerics(Class klass, ClassNode genericsType)`
    `` | `public static ClassNode` | `makeClassSafeWithGenerics(ClassNode type, GenericsType genericTypes)`
    `` | `public static Map<GenericsType, GenericsType>` | `makeDeclaringAndActualGenericsTypeMap(ClassNode declaringClass, ClassNode actualReceiver)` map declaring generics type to actual generics type, e.g.
    `` | `public static Map<GenericsType, GenericsType>` | `makeDeclaringAndActualGenericsTypeMapOfExactType(ClassNode declaringClass, ClassNode actualReceiver)` The method is similar with GenericsUtils.makeDeclaringAndActualGenericsTypeMap, The main difference is that the method will try to map all placeholders found to the relevant exact types, but the other will not try even if the parameterized type has placeholders
    `` | `public static ClassNode` | `newClass(ClassNode type)`
    `` | `public static ClassNode` | `nonGeneric(ClassNode type)`
    `` | `public static ClassNode` | `parameterizeInterfaceGenerics(ClassNode hint, ClassNode target)` Interface class nodes retrieved from ClassNode.getInterfaces or ClassNode.getAllInterfaces are returned with generic type arguments.
    `` | `public static Tuple2<ClassNode, ClassNode>` | `parameterizeSAM(ClassNode samType)` Gets the parameter and return types of the abstract method of SAM.
    `` | `public static ClassNode` | `parameterizeType(ClassNode hint, ClassNode target)` Interface class nodes retrieved from ClassNode.getInterfaces or ClassNode.getAllInterfaces are returned with generic type arguments.
    `` | `public static ClassNode[]` | `parseClassNodesFromString(String option, SourceUnit sourceUnit, CompilationUnit compilationUnit, MethodNode mn, ASTNode usage)`
    `` | `public static String` | `toGenericTypesString(GenericsType[] genericsTypes)`

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class Object` | `wait, wait, wait, equals, toString, hashCode, getClass, notify, notifyAll`

- - ### Field Detail

    - #### public static final GenericsType[] EMPTY_GENERICS_ARRAY

    - #### public static final String JAVA_LANG_OBJECT

  - ### Method Detail

    - #### @Override
      public void addError(String msg, ASTNode expr)

    - #### public static Map<String, ClassNode> addMethodGenerics(MethodNode current, Map<String, ClassNode> oldSpec)

    - #### @Deprecated
      public static GenericsType[] alignGenericTypes(GenericsType[] redirectGenericTypes, GenericsType[] parameterizedTypes, GenericsType[] alignmentTarget)

      Given a parameterized type and a generic type information, aligns actual type parameters. For example, if a class uses generic type

          <T,U,V>

      (redirectGenericTypes), is used with actual type parameters

          <java.lang.String, U,V>

      , then a class or interface using generic types

          <T,V>

      will be aligned to

          <java.lang.String,V>

      deprecated:
        You shouldn't call this method because it is inherently unreliable
      Parameters:
        `redirectGenericTypes` - the type arguments or the redirect class node
        `parameterizedTypes` - the actual type arguments used on this class node
        `alignmentTarget` - the generic type arguments to which we want to align to
      Returns:
        aligned type arguments

    - #### public static GenericsType[] applyGenericsContextToPlaceHolders(Map<String, ClassNode> genericsSpec, GenericsType[] oldPlaceHolders)

      Transforms generics types from an old context to a new context using the given spec. This method assumes all generics types will be placeholders. WARNING: The resulting generics types may or may not be placeholders after the transformation.

      Parameters:
        `genericsSpec` - the generics context information spec
        `oldPlaceHolders` - the old placeholders
      Returns:
        the new generics types

    - #### public static GenericsType buildWildcardType(ClassNode upperBounds)

      Generates a wildcard generic type in order to be used for checks against class nodes. See GenericsType.isCompatibleWith.

    - #### public static void clearParameterizedTypeCache()

      Clears the parameterized type cache.

      It is useful to IDE as the type being compiled are continuously being edited/altered, see GROOVY-8675

    - #### public static MethodNode correctToGenericsSpec(Map<String, ClassNode> genericsSpec, MethodNode mn)

    - #### public static ClassNode correctToGenericsSpec(Map<String, ClassNode> genericsSpec, GenericsType type)

    - #### public static ClassNode correctToGenericsSpec(Map<String, ClassNode> genericsSpec, ClassNode type)

    - #### public static ClassNode correctToGenericsSpecRecurse(Map<String, ClassNode> genericsSpec, ClassNode type)

    - #### public static ClassNode[] correctToGenericsSpecRecurse(Map<String, ClassNode> genericsSpec, ClassNode[] types)

      Since:
        2.4.1

    - #### public static ClassNode correctToGenericsSpecRecurse(Map<String, ClassNode> genericsSpec, ClassNode type, List<String> exclusions)

    - #### public static Map<String, ClassNode> createGenericsSpec(ClassNode type)

    - #### public static Map<String, ClassNode> createGenericsSpec(ClassNode type, Map<String, ClassNode> oldSpec)

    - #### public static Map<GenericsType.GenericsTypeName, GenericsType> extractPlaceholders(ClassNode type)

      Returns the type parameter/argument relationships of the specified type.

      Parameters:
        `type` - the class node to check

    - #### public static void extractPlaceholders(ClassNode type, Map<GenericsType.GenericsTypeName, GenericsType> placeholders)

      Populates the supplied map with the type parameter/argument relationships of the specified type.

      Parameters:
        `type` - the class node to check
        `placeholders` - the generics type information collector

    - #### public static void extractSuperClassGenerics(ClassNode type, ClassNode target, Map<String, ClassNode> spec)

    - #### public static ClassNode findActualTypeByGenericsPlaceholderName(String placeholderName, Map<GenericsType, GenericsType> genericsPlaceholderAndTypeMap)

      Gets the actual type according to the placeholder name.

      Parameters:
        `placeholderName` - the placeholder name (i.e. "T", "E", etc.)
        `genericsPlaceholderAndTypeMap` - the result of makeDeclaringAndActualGenericsTypeMap(ClassNode, ClassNode)

    - #### public static ClassNode findParameterizedType(ClassNode genericsClass, ClassNode actualType)

      Convenience method for findParameterizedType(ClassNode,ClassNode,boolean) when `tryToFindExactType` is `false`.

    - #### public static ClassNode findParameterizedType(ClassNode genericsClass, ClassNode actualType, boolean tryToFindExactType)

      Gets the parameterized type by searching the whole class hierarchy according to generics class and actual receiver.

      findParameterizedTypeFromCache(ClassNode,ClassNode,boolean) is strongly recommended for better performance.

    - #### public static ClassNode findParameterizedTypeFromCache(ClassNode genericsClass, ClassNode actualType)

      Convenience method for findParameterizedTypeFromCache(ClassNode, ClassNode, boolean) when the `tryToFindExactType` boolean is `false`.

    - #### public static ClassNode findParameterizedTypeFromCache(ClassNode genericsClass, ClassNode actualType, boolean tryToFindExactType)

      Try to get the parameterized type from the cache.

      If no cached item found, cache and return the result of findParameterizedType(ClassNode,ClassNode,boolean)

    - #### public static ClassNode getSuperClass(ClassNode type, ClassNode target)

    - #### public static boolean hasNonPlaceHolders(ClassNode type)

      Checks if the type has any non-placeholder (aka resolved) generics.

      Since:
        3.0.0

    - #### public static boolean hasPlaceHolders(ClassNode type)

      Checks if the type has any placeholder (aka unresolved) generics.

      Since:
        3.0.0

    - #### public static boolean hasUnresolvedGenerics(ClassNode type)

      Checks for any placeholder (aka unresolved) generics.

    - #### public static ClassNode makeClassSafe(Class klass)

    - #### public static ClassNode makeClassSafe0(ClassNode type, GenericsType genericTypes)

    - #### public static ClassNode makeClassSafeWithGenerics(Class klass, ClassNode genericsType)

    - #### public static ClassNode makeClassSafeWithGenerics(ClassNode type, GenericsType genericTypes)

    - #### public static Map<GenericsType, GenericsType> makeDeclaringAndActualGenericsTypeMap(ClassNode declaringClass, ClassNode actualReceiver)

      map declaring generics type to actual generics type, e.g. GROOVY-7204: declaring generics types: T, S extends Serializable actual generics types : String, Long the result map is [ T: String, S: Long ] The resolved types can not help us to choose methods correctly if the argument is a string: T: Object, S: Serializable so we need actual types: T: String, S: Long

    - #### public static Map<GenericsType, GenericsType> makeDeclaringAndActualGenericsTypeMapOfExactType(ClassNode declaringClass, ClassNode actualReceiver)

      The method is similar with GenericsUtils.makeDeclaringAndActualGenericsTypeMap, The main difference is that the method will try to map all placeholders found to the relevant exact types, but the other will not try even if the parameterized type has placeholders

      Parameters:
        `declaringClass` - the generics class node declaring the generics types
        `actualReceiver` - the subclass class node
      Returns:
        the placeholder-to-actualtype mapping
      Since:
        3.0.0

    - #### public static ClassNode newClass(ClassNode type)

    - #### public static ClassNode nonGeneric(ClassNode type)

    - #### @Deprecated
      public static ClassNode parameterizeInterfaceGenerics(ClassNode hint, ClassNode target)

      Interface class nodes retrieved from ClassNode.getInterfaces or ClassNode.getAllInterfaces are returned with generic type arguments. This method allows returning a parameterized interface given the parameterized class node which implements this interface.

      deprecated:
        Use #parameterizeType instead
      Parameters:
        `hint` - the class node where generics types are parameterized
        `target` - the interface we want to parameterize generics types
      Returns:
        a parameterized interface class node

    - #### public static Tuple2<ClassNode, ClassNode> parameterizeSAM(ClassNode samType)

      Gets the parameter and return types of the abstract method of SAM. If the abstract method is not parameterized, we will get generics placeholders, e.g. T, U For example, the abstract method of Function is

                R apply(T t);

      We parameterize the above interface as `Function<String, Integer>`, then the abstract method will be

                Integer apply(String t);

      When we call `parameterizeSAM` on the ClassNode `Function<String, Integer>`, we can get parameter types and return type of the above abstract method, i.e. ClassNode `ClassHelper.STRING_TYPE` and `ClassHelper.Integer_TYPE`
      Parameters:
        `samType` - the class node which contains only one abstract method
      Since:
        3.0.0

    - #### public static ClassNode parameterizeType(ClassNode hint, ClassNode target)

      Interface class nodes retrieved from ClassNode.getInterfaces or ClassNode.getAllInterfaces are returned with generic type arguments. This method allows returning a parameterized interface given the parameterized class node which implements this interface.

      Parameters:
        `hint` - the ClassNode where generics types are parameterized
        `target` - the interface we want to parameterize generics types
      Returns:
        a parameterized interface ClassNode

    - #### public static ClassNode[] parseClassNodesFromString(String option, SourceUnit sourceUnit, CompilationUnit compilationUnit, MethodNode mn, ASTNode usage)

    - #### public static String toGenericTypesString(GenericsType[] genericsTypes)