Class GenericsUtils


  • public class GenericsUtils
    extends java.lang.Object
    Utility methods to deal with generic types.
    • Field Detail

      • EMPTY_GENERICS_ARRAY

        public static final GenericsType[] EMPTY_GENERICS_ARRAY
      • JAVA_LANG_OBJECT

        public static final java.lang.String JAVA_LANG_OBJECT
        See Also:
        Constant Field Values
    • Constructor Detail

      • GenericsUtils

        public GenericsUtils()
    • Method Detail

      • alignGenericTypes

        @Deprecated
        public static GenericsType[] alignGenericTypes​(GenericsType[] redirectGenericTypes,
                                                       GenericsType[] parameterizedTypes,
                                                       GenericsType[] alignmentTarget)
        Deprecated.
        You shouldn't call this method because it is inherently unreliable
        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>
        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
      • extractPlaceholders

        public static void extractPlaceholders​(ClassNode node,
                                               java.util.Map<GenericsType.GenericsTypeName,​GenericsType> map)
        For a given classnode, fills in the supplied map with the parameterized types it defines.
        Parameters:
        node - the class node to check
        map - the generics type information collector
      • toGenericTypesString

        public static java.lang.String toGenericTypesString​(GenericsType[] genericsTypes)
      • parameterizeInterfaceGenerics

        @Deprecated
        public static ClassNode parameterizeInterfaceGenerics​(ClassNode hint,
                                                              ClassNode target)
        Deprecated.
        Use #parameterizeType instead
        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 class node where generics types are parameterized
        target - the interface we want to parameterize generics types
        Returns:
        a parameterized interface class node
      • parameterizeType

        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 class node where generics types are parameterized
        target - the interface we want to parameterize generics types
        Returns:
        a parameterized interface class node
      • makeClassSafe

        public static ClassNode makeClassSafe​(java.lang.Class klass)
      • makeClassSafeWithGenerics

        public static ClassNode makeClassSafeWithGenerics​(java.lang.Class klass,
                                                          ClassNode genericsType)
      • correctToGenericsSpec

        public static MethodNode correctToGenericsSpec​(java.util.Map<java.lang.String,​ClassNode> genericsSpec,
                                                       MethodNode mn)
      • correctToGenericsSpecRecurse

        public static ClassNode correctToGenericsSpecRecurse​(java.util.Map<java.lang.String,​ClassNode> genericsSpec,
                                                             ClassNode type)
      • correctToGenericsSpecRecurse

        public static ClassNode[] correctToGenericsSpecRecurse​(java.util.Map<java.lang.String,​ClassNode> genericsSpec,
                                                               ClassNode[] types)
        Since:
        2.4.1
      • correctToGenericsSpecRecurse

        public static ClassNode correctToGenericsSpecRecurse​(java.util.Map<java.lang.String,​ClassNode> genericsSpec,
                                                             ClassNode type,
                                                             java.util.List<java.lang.String> exclusions)
      • correctToGenericsSpec

        public static ClassNode correctToGenericsSpec​(java.util.Map<java.lang.String,​ClassNode> genericsSpec,
                                                      GenericsType type)
      • correctToGenericsSpec

        public static ClassNode correctToGenericsSpec​(java.util.Map<java.lang.String,​ClassNode> genericsSpec,
                                                      ClassNode type)
      • createGenericsSpec

        public static java.util.Map<java.lang.String,​ClassNode> createGenericsSpec​(ClassNode current)
      • createGenericsSpec

        public static java.util.Map<java.lang.String,​ClassNode> createGenericsSpec​(ClassNode current,
                                                                                         java.util.Map<java.lang.String,​ClassNode> oldSpec)
      • addMethodGenerics

        public static java.util.Map<java.lang.String,​ClassNode> addMethodGenerics​(MethodNode current,
                                                                                        java.util.Map<java.lang.String,​ClassNode> oldSpec)
      • extractSuperClassGenerics

        public static void extractSuperClassGenerics​(ClassNode type,
                                                     ClassNode target,
                                                     java.util.Map<java.lang.String,​ClassNode> spec)
      • applyGenericsContextToPlaceHolders

        public static GenericsType[] applyGenericsContextToPlaceHolders​(java.util.Map<java.lang.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
      • findParameterizedType

        public static ClassNode findParameterizedType​(ClassNode genericsClass,
                                                      ClassNode actualType)
        Get the parameterized type by search the whole class hierarchy according to generics class and actual receiver. findParameterizedTypeFromCache(ClassNode, ClassNode) is strongly recommended for better performance.
        Parameters:
        genericsClass - the generics class
        actualType - the actual type
        Returns:
        the parameterized type
      • clearParameterizedTypeCache

        public static void clearParameterizedTypeCache()
        Clear the parameterized type cache It is useful to IDE as the type being compiled are continuously being edited/altered, see GROOVY-8675
      • makeDeclaringAndActualGenericsTypeMap

        public static java.util.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
      • findActualTypeByGenericsPlaceholderName

        public static ClassNode findActualTypeByGenericsPlaceholderName​(java.lang.String placeholderName,
                                                                        java.util.Map<GenericsType,​GenericsType> genericsPlaceholderAndTypeMap)
        Get the actual type according to the placeholder name
        Parameters:
        placeholderName - the placeholder name, e.g. T, E
        genericsPlaceholderAndTypeMap - the result of makeDeclaringAndActualGenericsTypeMap(ClassNode, ClassNode)
        Returns:
        the actual type