Package org.codehaus.groovy.ast.tools
Class GenericsUtils
java.lang.Object
org.codehaus.groovy.ast.tools.GenericsUtils
public class GenericsUtils
extends java.lang.Object
Utility methods to deal with generic types.
-
Field Summary
Fields Modifier and Type Field Description static GenericsType[]
EMPTY_GENERICS_ARRAY
static java.lang.String
JAVA_LANG_OBJECT
-
Constructor Summary
Constructors Constructor Description GenericsUtils()
-
Method Summary
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 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 ClassNode
correctToGenericsSpec(java.util.Map<java.lang.String,ClassNode> genericsSpec, ClassNode type)
static ClassNode
correctToGenericsSpec(java.util.Map<java.lang.String,ClassNode> genericsSpec, GenericsType type)
static MethodNode
correctToGenericsSpec(java.util.Map<java.lang.String,ClassNode> genericsSpec, MethodNode mn)
static ClassNode
correctToGenericsSpecRecurse(java.util.Map<java.lang.String,ClassNode> genericsSpec, ClassNode type)
static ClassNode[]
correctToGenericsSpecRecurse(java.util.Map<java.lang.String,ClassNode> genericsSpec, ClassNode[] types)
static ClassNode
correctToGenericsSpecRecurse(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 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.static void
extractSuperClassGenerics(ClassNode type, ClassNode target, java.util.Map<java.lang.String,ClassNode> spec)
static ClassNode
findActualTypeByGenericsPlaceholderName(java.lang.String placeholderName, java.util.Map<GenericsType,GenericsType> genericsPlaceholderAndTypeMap)
Gets the actual type according to the placeholder name.static ClassNode
findParameterizedType(ClassNode genericsClass, ClassNode actualType)
Convenience method forfindParameterizedType(ClassNode,ClassNode,boolean)
whentryToFindExactType
isfalse
.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.static ClassNode
findParameterizedTypeFromCache(ClassNode genericsClass, ClassNode actualType)
Convenience method forfindParameterizedTypeFromCache(ClassNode, ClassNode, boolean)
when thetryToFindExactType
boolean isfalse
.static ClassNode
findParameterizedTypeFromCache(ClassNode genericsClass, ClassNode actualType, boolean tryToFindExactType)
Try to get the parameterized type from the cache.static ClassNode
getSuperClass(ClassNode type, ClassNode target)
static boolean
hasNonPlaceHolders(ClassNode type)
Checks if the type has any non-placeholder (aka resolved) generics.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(java.lang.Class klass)
static ClassNode
makeClassSafe0(ClassNode type, GenericsType... genericTypes)
static ClassNode
makeClassSafeWithGenerics(java.lang.Class klass, ClassNode genericsType)
static ClassNode
makeClassSafeWithGenerics(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 ClassNode
newClass(ClassNode type)
static ClassNode
nonGeneric(ClassNode type)
static ClassNode
parameterizeInterfaceGenerics(ClassNode hint, ClassNode target)
Deprecated.Use #parameterizeType insteadstatic Tuple2<ClassNode[],ClassNode>
parameterizeSAM(ClassNode samType)
Gets the parameter and return types of the abstract method of SAM.static ClassNode
parameterizeType(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.String
toGenericTypesString(GenericsType[] genericsTypes)
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Field Details
-
EMPTY_GENERICS_ARRAY
-
JAVA_LANG_OBJECT
public static final java.lang.String JAVA_LANG_OBJECT- See Also:
- Constant Field Values
-
-
Constructor Details
-
GenericsUtils
public GenericsUtils()
-
-
Method Details
-
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 nodeparameterizedTypes
- the actual type arguments used on this class nodealignmentTarget
- the generic type arguments to which we want to align to- Returns:
- aligned type arguments
-
buildWildcardType
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
-
extractPlaceholders
public 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
-
extractPlaceholders
public 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 checkplaceholders
- the generics type information collector
-
toGenericTypesString
-
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 parameterizedtarget
- the interface we want to parameterize generics types- Returns:
- a parameterized interface class node
-
parameterizeType
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 parameterizedtarget
- the interface we want to parameterize generics types- Returns:
- a parameterized interface class node
-
nonGeneric
-
newClass
-
makeClassSafe
-
makeClassSafeWithGenerics
-
makeClassSafe0
-
makeClassSafeWithGenerics
-
correctToGenericsSpec
public static MethodNode correctToGenericsSpec(java.util.Map<java.lang.String,ClassNode> genericsSpec, MethodNode mn) -
correctToGenericsSpecRecurse
-
correctToGenericsSpecRecurse
public static ClassNode[] correctToGenericsSpecRecurse(java.util.Map<java.lang.String,ClassNode> genericsSpec, ClassNode[] types)- Since:
- 2.4.1
-
correctToGenericsSpecRecurse
-
correctToGenericsSpec
public static ClassNode correctToGenericsSpec(java.util.Map<java.lang.String,ClassNode> genericsSpec, GenericsType type) -
correctToGenericsSpec
-
createGenericsSpec
-
createGenericsSpec
-
addMethodGenerics
public static java.util.Map<java.lang.String,ClassNode> addMethodGenerics(MethodNode current, java.util.Map<java.lang.String,ClassNode> oldSpec) -
extractSuperClassGenerics
-
getSuperClass
-
parseClassNodesFromString
public static ClassNode[] parseClassNodesFromString(java.lang.String option, SourceUnit sourceUnit, CompilationUnit compilationUnit, MethodNode mn, ASTNode usage) -
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 specoldPlaceHolders
- the old placeholders- Returns:
- the new generics types
-
clearParameterizedTypeCache
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
-
findParameterizedTypeFromCache
public static ClassNode findParameterizedTypeFromCache(ClassNode genericsClass, ClassNode actualType)Convenience method forfindParameterizedTypeFromCache(ClassNode, ClassNode, boolean)
when thetryToFindExactType
boolean isfalse
. -
findParameterizedTypeFromCache
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)
-
findParameterizedType
Convenience method forfindParameterizedType(ClassNode,ClassNode,boolean)
whentryToFindExactType
isfalse
. -
findParameterizedType
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. -
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 -
makeDeclaringAndActualGenericsTypeMapOfExactType
public 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 placeholders- Parameters:
declaringClass
- the generics class node declaring the generics typesactualReceiver
- the sub-class class node- Returns:
- the placeholder-to-actualtype mapping
- Since:
- 3.0.0
-
hasNonPlaceHolders
Checks if the type has any non-placeholder (aka resolved) generics.- Since:
- 3.0.0
-
hasPlaceHolders
Checks if the type has any placeholder (aka unresolved) generics.- Since:
- 3.0.0
-
hasUnresolvedGenerics
Checks for any placeholder (aka unresolved) generics. -
parameterizeSAM
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 ofFunction
isR apply(T t);
We parameterize the above interface asFunction<String, Integer>
, then the abstract method will beInteger apply(String t);
When we callparameterizeSAM
on the ClassNodeFunction<String, Integer>
, we can get parameter types and return type of the above abstract method, i.e. ClassNodeClassHelper.STRING_TYPE
andClassHelper.Integer_TYPE
- Parameters:
samType
- the class node which contains only one abstract method- Since:
- 3.0.0
-
findActualTypeByGenericsPlaceholderName
public 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 ofmakeDeclaringAndActualGenericsTypeMap(ClassNode, ClassNode)
-