Utility methods to deal with generic types.
Modifiers | Name | Description |
---|---|---|
static GenericsType[] |
EMPTY_GENERICS_ARRAY |
|
static String |
JAVA_LANG_OBJECT |
Type Params | Return Type | Name and description |
---|---|---|
|
static Map<String, ClassNode> |
addMethodGenerics(MethodNode current, Map<String, ClassNode> oldSpec) |
|
static GenericsType[] |
alignGenericTypes(GenericsType[] redirectGenericTypes, GenericsType[] parameterizedTypes, GenericsType[] alignmentTarget) Given a parameterized type and a generic type information, aligns actual type parameters. |
|
static GenericsType[] |
applyGenericsContextToPlaceHolders(Map<String, ClassNode> genericsSpec, GenericsType[] oldPlaceHolders) Transforms generics types from an old context to a new context using the given spec. |
|
static GenericsType |
buildWildcardType(ClassNode... types) Generates a wildcard generic type in order to be used for checks against class nodes. |
|
static void |
clearParameterizedTypeCache() Clears the parameterized type cache. |
|
static MethodNode |
correctToGenericsSpec(Map<String, ClassNode> genericsSpec, MethodNode mn) |
|
static ClassNode |
correctToGenericsSpec(Map<String, ClassNode> genericsSpec, GenericsType type) |
|
static ClassNode |
correctToGenericsSpec(Map<String, ClassNode> genericsSpec, ClassNode type) |
|
static ClassNode |
correctToGenericsSpecRecurse(Map<String, ClassNode> genericsSpec, ClassNode type) |
|
static ClassNode[] |
correctToGenericsSpecRecurse(Map<String, ClassNode> genericsSpec, ClassNode[] types) @since 2.4.1 |
|
static ClassNode |
correctToGenericsSpecRecurse(Map<String, ClassNode> genericsSpec, ClassNode type, List<String> exclusions) |
|
static Map<String, ClassNode> |
createGenericsSpec(ClassNode current) |
|
static Map<String, ClassNode> |
createGenericsSpec(ClassNode current, Map<String, ClassNode> oldSpec) |
|
static Map<GenericsType.GenericsTypeName, GenericsType> |
extractPlaceholders(ClassNode type) Returns the type parameter/argument relationships of the specified type. |
|
static void |
extractPlaceholders(ClassNode type, Map<GenericsType.GenericsTypeName, GenericsType> placeholders) Populates the supplied map with the type parameter/argument relationships of the specified type. |
|
static void |
extractSuperClassGenerics(ClassNode type, ClassNode target, Map<String, ClassNode> spec) |
|
static ClassNode |
findActualTypeByGenericsPlaceholderName(String placeholderName, Map<GenericsType, GenericsType> genericsPlaceholderAndTypeMap) Gets the actual type according to the placeholder name. |
|
static ClassNode |
findParameterizedType(ClassNode genericsClass, ClassNode actualType) Get the parameterized type by search the whole class hierarchy according to generics class and actual receiver. |
|
static ClassNode |
findParameterizedTypeFromCache(ClassNode genericsClass, ClassNode actualType) Try to get the parameterized type from the cache. |
|
static ClassNode |
getSuperClass(ClassNode type, ClassNode target) |
|
static boolean |
hasPlaceHolders(ClassNode type) Checks if the type has any placeholder (aka unresolved) generics. |
|
static boolean |
hasUnresolvedGenerics(ClassNode type) Checks for any placeholder (aka unresolved) generics. |
|
static ClassNode |
makeClassSafe(Class klass) |
|
static ClassNode |
makeClassSafe0(ClassNode type, GenericsType... genericTypes) |
|
static ClassNode |
makeClassSafeWithGenerics(Class klass, ClassNode genericsType) |
|
static ClassNode |
makeClassSafeWithGenerics(ClassNode type, GenericsType... genericTypes) |
|
static Map<GenericsType, GenericsType> |
makeDeclaringAndActualGenericsTypeMap(ClassNode declaringClass, ClassNode actualReceiver) map declaring generics type to actual generics type, e.g. |
|
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 |
|
static ClassNode |
newClass(ClassNode type) |
|
static ClassNode |
nonGeneric(ClassNode type) |
|
static ClassNode |
parameterizeInterfaceGenerics(ClassNode hint, ClassNode target) Interface class nodes retrieved from ClassNode.getInterfaces or ClassNode.getAllInterfaces are returned with generic type arguments. |
|
static ClassNode |
parameterizeType(ClassNode hint, ClassNode target) Interface class nodes retrieved from ClassNode.getInterfaces or ClassNode.getAllInterfaces are returned with generic type arguments. |
|
static ClassNode[] |
parseClassNodesFromString(String option, SourceUnit sourceUnit, CompilationUnit compilationUnit, MethodNode mn, ASTNode usage) |
|
static String |
toGenericTypesString(GenericsType[] genericsTypes) |
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>
redirectGenericTypes
- the type arguments or the redirect class nodeparameterizedTypes
- the actual type arguments used on this class nodealignmentTarget
- the generic type arguments to which we want to align toTransforms 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.
genericsSpec
- the generics context information specoldPlaceHolders
- the old placeholdersGenerates a wildcard generic type in order to be used for checks against class nodes. See GenericsType#isCompatibleWith(org.codehaus.groovy.ast.ClassNode).
types
- the type to be used as the wildcard upper boundClears the parameterized type cache.
It is useful to IDE as the type being compiled are continuously being edited/altered, see GROOVY-8675
Returns the type parameter/argument relationships of the specified type.
type
- the class node to checkPopulates the supplied map with the type parameter/argument relationships of the specified type.
type
- the class node to checkplaceholders
- the generics type information collectorGets the actual type according to the placeholder name.
placeholderName
- the placeholder name (i.e. "T", "E", etc.)genericsPlaceholderAndTypeMap
- the result of makeDeclaringAndActualGenericsTypeMap(ClassNode, ClassNode)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.
genericsClass
- the generics classactualType
- the actual typeTry to get the parameterized type from the cache. If no cached item found, cache and return the result of findParameterizedType(ClassNode, ClassNode)
Checks if the type has any placeholder (aka unresolved) generics.
Backported from 3.0.0
Checks for any placeholder (aka unresolved) generics.
Backported from 3.0.8
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
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
Backported from 3.0.0
declaringClass
- the generics class node declaring the generics typesactualReceiver
- the sub-class class nodeInterface 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.
hint
- the class node where generics types are parameterizedtarget
- the interface we want to parameterize generics typesInterface 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.
hint
- the class node where generics types are parameterizedtarget
- the interface we want to parameterize generics types