Package org.codehaus.groovy.ast.tools
Class GenericsUtils
- java.lang.Object
- 
- org.codehaus.groovy.ast.tools.GenericsUtils
 
- 
 public class GenericsUtils extends java.lang.ObjectUtility methods to deal with generic types.
- 
- 
Field SummaryFields Modifier and Type Field Description static GenericsType[]EMPTY_GENERICS_ARRAYstatic java.lang.StringJAVA_LANG_OBJECT
 - 
Constructor SummaryConstructors Constructor Description GenericsUtils()
 - 
Method SummaryAll Methods Static Methods Concrete Methods Deprecated Methods Modifier and Type Method Description static java.util.Map<java.lang.String,ClassNode>addMethodGenerics(MethodNode current, java.util.Map<java.lang.String,ClassNode> oldSpec)static GenericsType[]alignGenericTypes(GenericsType[] redirectGenericTypes, GenericsType[] parameterizedTypes, GenericsType[] alignmentTarget)Deprecated.You shouldn't call this method because it is inherently unreliablestatic 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.static GenericsTypebuildWildcardType(ClassNode... types)Generates a wildcard generic type in order to be used for checks against class nodes.static voidclearParameterizedTypeCache()Clears the parameterized type cache.static ClassNodecorrectToGenericsSpec(java.util.Map<java.lang.String,ClassNode> genericsSpec, ClassNode type)static ClassNodecorrectToGenericsSpec(java.util.Map<java.lang.String,ClassNode> genericsSpec, GenericsType type)static MethodNodecorrectToGenericsSpec(java.util.Map<java.lang.String,ClassNode> genericsSpec, MethodNode mn)static ClassNodecorrectToGenericsSpecRecurse(java.util.Map<java.lang.String,ClassNode> genericsSpec, ClassNode type)static ClassNode[]correctToGenericsSpecRecurse(java.util.Map<java.lang.String,ClassNode> genericsSpec, ClassNode[] types)static ClassNodecorrectToGenericsSpecRecurse(java.util.Map<java.lang.String,ClassNode> genericsSpec, ClassNode type, java.util.List<java.lang.String> exclusions)static java.util.Map<java.lang.String,ClassNode>createGenericsSpec(ClassNode current)static java.util.Map<java.lang.String,ClassNode>createGenericsSpec(ClassNode current, java.util.Map<java.lang.String,ClassNode> oldSpec)static java.util.Map<GenericsType.GenericsTypeName,GenericsType>extractPlaceholders(ClassNode type)Returns the type parameter/argument relationships of the specified type.static voidextractPlaceholders(ClassNode type, java.util.Map<GenericsType.GenericsTypeName,GenericsType> placeholders)Populates the supplied map with the type parameter/argument relationships of the specified type.static voidextractSuperClassGenerics(ClassNode type, ClassNode target, java.util.Map<java.lang.String,ClassNode> spec)static ClassNodefindActualTypeByGenericsPlaceholderName(java.lang.String placeholderName, java.util.Map<GenericsType,GenericsType> genericsPlaceholderAndTypeMap)Gets the actual type according to the placeholder name.static ClassNodefindParameterizedType(ClassNode genericsClass, ClassNode actualType)Get the parameterized type by search the whole class hierarchy according to generics class and actual receiver.static ClassNodefindParameterizedTypeFromCache(ClassNode genericsClass, ClassNode actualType)Try to get the parameterized type from the cache.static ClassNodegetSuperClass(ClassNode type, ClassNode target)static booleanhasPlaceHolders(ClassNode type)Checks if the type has any placeholder (aka unresolved) generics.static booleanhasUnresolvedGenerics(ClassNode type)Checks for any placeholder (aka unresolved) generics.static ClassNodemakeClassSafe(java.lang.Class klass)static ClassNodemakeClassSafe0(ClassNode type, GenericsType... genericTypes)static ClassNodemakeClassSafeWithGenerics(java.lang.Class klass, ClassNode genericsType)static ClassNodemakeClassSafeWithGenerics(ClassNode type, GenericsType... genericTypes)static java.util.Map<GenericsType,GenericsType>makeDeclaringAndActualGenericsTypeMap(ClassNode declaringClass, ClassNode actualReceiver)map declaring generics type to actual generics type, e.g.static java.util.Map<GenericsType,GenericsType>makeDeclaringAndActualGenericsTypeMapOfExactType(ClassNode declaringClass, ClassNode actualReceiver)The method is similar withmakeDeclaringAndActualGenericsTypeMap(ClassNode, ClassNode), 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 placeholdersstatic ClassNodenewClass(ClassNode type)static ClassNodenonGeneric(ClassNode type)static ClassNodeparameterizeInterfaceGenerics(ClassNode hint, ClassNode target)Deprecated.Use #parameterizeType insteadstatic ClassNodeparameterizeType(ClassNode hint, ClassNode target)Interface class nodes retrieved fromClassNode.getInterfaces()orClassNode.getAllInterfaces()are returned with generic type arguments.static ClassNode[]parseClassNodesFromString(java.lang.String option, SourceUnit sourceUnit, CompilationUnit compilationUnit, MethodNode mn, ASTNode usage)static java.lang.StringtoGenericTypesString(GenericsType[] genericsTypes)
 
- 
- 
- 
Field Detail- 
EMPTY_GENERICS_ARRAYpublic static final GenericsType[] EMPTY_GENERICS_ARRAY 
 - 
JAVA_LANG_OBJECTpublic static final java.lang.String JAVA_LANG_OBJECT - See Also:
- Constant Field Values
 
 
- 
 - 
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 unreliableGiven 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
 
 - 
buildWildcardTypepublic static GenericsType buildWildcardType(ClassNode... types) Generates a wildcard generic type in order to be used for checks against class nodes. SeeGenericsType.isCompatibleWith(org.codehaus.groovy.ast.ClassNode).- Parameters:
- types- the type to be used as the wildcard upper bound
- Returns:
- a wildcard generics type
 
 - 
extractPlaceholderspublic static java.util.Map<GenericsType.GenericsTypeName,GenericsType> extractPlaceholders(ClassNode type) Returns the type parameter/argument relationships of the specified type.- Parameters:
- type- the class node to check
 
 - 
extractPlaceholderspublic static void extractPlaceholders(ClassNode type, java.util.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
 
 - 
toGenericTypesStringpublic static java.lang.String toGenericTypesString(GenericsType[] genericsTypes) 
 - 
parameterizeInterfaceGenerics@Deprecated public static ClassNode parameterizeInterfaceGenerics(ClassNode hint, ClassNode target) Deprecated.Use #parameterizeType insteadInterface class nodes retrieved fromClassNode.getInterfaces()orClassNode.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
 
 - 
parameterizeTypepublic static ClassNode parameterizeType(ClassNode hint, ClassNode target) Interface class nodes retrieved fromClassNode.getInterfaces()orClassNode.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
 
 - 
makeClassSafepublic static ClassNode makeClassSafe(java.lang.Class klass) 
 - 
makeClassSafeWithGenericspublic static ClassNode makeClassSafeWithGenerics(java.lang.Class klass, ClassNode genericsType) 
 - 
makeClassSafe0public static ClassNode makeClassSafe0(ClassNode type, GenericsType... genericTypes) 
 - 
makeClassSafeWithGenericspublic static ClassNode makeClassSafeWithGenerics(ClassNode type, GenericsType... genericTypes) 
 - 
correctToGenericsSpecpublic static MethodNode correctToGenericsSpec(java.util.Map<java.lang.String,ClassNode> genericsSpec, MethodNode mn) 
 - 
correctToGenericsSpecRecursepublic static ClassNode correctToGenericsSpecRecurse(java.util.Map<java.lang.String,ClassNode> genericsSpec, ClassNode type) 
 - 
correctToGenericsSpecRecursepublic static ClassNode[] correctToGenericsSpecRecurse(java.util.Map<java.lang.String,ClassNode> genericsSpec, ClassNode[] types) - Since:
- 2.4.1
 
 - 
correctToGenericsSpecRecursepublic static ClassNode correctToGenericsSpecRecurse(java.util.Map<java.lang.String,ClassNode> genericsSpec, ClassNode type, java.util.List<java.lang.String> exclusions) 
 - 
correctToGenericsSpecpublic static ClassNode correctToGenericsSpec(java.util.Map<java.lang.String,ClassNode> genericsSpec, GenericsType type) 
 - 
correctToGenericsSpecpublic static ClassNode correctToGenericsSpec(java.util.Map<java.lang.String,ClassNode> genericsSpec, ClassNode type) 
 - 
createGenericsSpecpublic static java.util.Map<java.lang.String,ClassNode> createGenericsSpec(ClassNode current) 
 - 
createGenericsSpecpublic static java.util.Map<java.lang.String,ClassNode> createGenericsSpec(ClassNode current, java.util.Map<java.lang.String,ClassNode> oldSpec) 
 - 
addMethodGenericspublic static java.util.Map<java.lang.String,ClassNode> addMethodGenerics(MethodNode current, java.util.Map<java.lang.String,ClassNode> oldSpec) 
 - 
extractSuperClassGenericspublic static void extractSuperClassGenerics(ClassNode type, ClassNode target, java.util.Map<java.lang.String,ClassNode> spec) 
 - 
parseClassNodesFromStringpublic static ClassNode[] parseClassNodesFromString(java.lang.String option, SourceUnit sourceUnit, CompilationUnit compilationUnit, MethodNode mn, ASTNode usage) 
 - 
applyGenericsContextToPlaceHolderspublic 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
 
 - 
findParameterizedTypepublic 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
 
 - 
hasUnresolvedGenericspublic static boolean hasUnresolvedGenerics(ClassNode type) Checks for any placeholder (aka unresolved) generics.Backported from 3.0.8 
 - 
makeDeclaringAndActualGenericsTypeMappublic 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
 - 
makeDeclaringAndActualGenericsTypeMapOfExactTypepublic static java.util.Map<GenericsType,GenericsType> makeDeclaringAndActualGenericsTypeMapOfExactType(ClassNode declaringClass, ClassNode actualReceiver) The method is similar withmakeDeclaringAndActualGenericsTypeMap(ClassNode, ClassNode), 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 placeholdersBackported from 3.0.0 - Parameters:
- declaringClass- the generics class node declaring the generics types
- actualReceiver- the sub-class class node
- Returns:
- the placeholder-to-actualtype mapping
- Since:
- 2.5.9
 
 - 
hasPlaceHolderspublic static boolean hasPlaceHolders(ClassNode type) Checks if the type has any placeholder (aka unresolved) generics.Backported from 3.0.0 - Since:
- 2.5.10
 
 - 
findActualTypeByGenericsPlaceholderNamepublic static ClassNode findActualTypeByGenericsPlaceholderName(java.lang.String placeholderName, java.util.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)
- Returns:
- the actual type
 
 - 
clearParameterizedTypeCachepublic 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 
 - 
findParameterizedTypeFromCachepublic static ClassNode findParameterizedTypeFromCache(ClassNode genericsClass, ClassNode actualType) Try to get the parameterized type from the cache. If no cached item found, cache and return the result offindParameterizedType(ClassNode, ClassNode)
 
- 
 
-