Top Description Inners Fields Constructors Methods
org.codehaus.groovy.ast.tools

public Class GenericsUtils

extends Object
Class Inheritance
Imports
groovy.lang.Tuple2, groovy.transform.stc.IncorrectTypeHintException, org.codehaus.groovy.GroovyBugError, org.codehaus.groovy.ast.ASTNode, .ClassHelper, .ClassNode, .GenericsType, .MethodNode, .ModuleNode, .Parameter, org.codehaus.groovy.ast.decompiled.DecompiledClassNode, org.codehaus.groovy.ast.expr.DeclarationExpression, org.codehaus.groovy.ast.stmt.EmptyStatement, .ExpressionStatement, org.codehaus.groovy.control.CompilationUnit, .ParserPlugin, .ResolveVisitor, .SourceUnit, org.codehaus.groovy.runtime.memoize.ConcurrentSoftCache, .EvictableCache, java.lang.ref.SoftReference, java.util.ArrayList, .Arrays, .Collections, .HashMap, .HashSet, .LinkedHashMap, .LinkedList, .List, .Map, .Objects, .Set, java.util.function.Function, .Predicate

Utility methods to deal with generic types.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
public static final GenericsType[]
public static final String
private static final EvictableCache<GenericsUtils.ParameterizedTypeCacheKey, SoftReference<ClassNode>>
private static final boolean

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public static Map<String, ClassNode>
public static GenericsType[]

Returns:

aligned type arguments
alignGenericTypes
(final GenericsType[]
the type arguments or the redirect class node
redirectGenericTypes
,
final GenericsType[]
the actual type arguments used on this class node
parameterizedTypes
,
final GenericsType[]
the generic type arguments to which we want to align to
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.
public static GenericsType[]

Returns:

the new generics types
applyGenericsContextToPlaceHolders
(Map<String, ClassNode>
the generics context information spec
genericsSpec
,
GenericsType[]
the old placeholders
oldPlaceHolders
)

Transforms generics types from an old context to a new context using the given spec.

public static GenericsType
buildWildcardType(final ClassNode... upperBounds)

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

private static boolean
public static void
clearParameterizedTypeCache()

Clears the parameterized type cache.

public static MethodNode
public static ClassNode
public static ClassNode
public static ClassNode
public static ClassNode[]
public static ClassNode
private static Map<GenericsType, GenericsType>
correlateTypeParametersAndTypeArguments(final ClassNode declaringClass, final ClassNode actualReceiver, final boolean tryToFindExactType)

public static Map<String, ClassNode>
public static Map<String, ClassNode>
private static GenericsType
public static Map<GenericsType.GenericsTypeName, GenericsType>
extractPlaceholders(final ClassNode
the class node to check
type
)

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

public static void
extractPlaceholders(final ClassNode
the class node to check
type
,
final Map<GenericsType.GenericsTypeName, GenericsType>
the generics type information collector
placeholders
)

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

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

private static void
extractSuperClassGenerics(final GenericsType[] usage, final GenericsType[] declaration, final Map<String, ClassNode> spec)

private static void
public static ClassNode
findActualTypeByGenericsPlaceholderName(final String
the placeholder name (i.e. "T", "E", etc.)
placeholderName
,
final Map<GenericsType, GenericsType> genericsPlaceholderAndTypeMap)

Gets the actual type according to the placeholder name.

public static ClassNode
findParameterizedType(final ClassNode genericsClass, final ClassNode actualType)

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

public static ClassNode
findParameterizedType(final ClassNode genericsClass, final ClassNode actualType, final boolean tryToFindExactType)

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

public static ClassNode
findParameterizedTypeFromCache(final ClassNode genericsClass, final ClassNode actualType)

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

public static ClassNode
findParameterizedTypeFromCache(final ClassNode genericsClass, final ClassNode actualType, boolean tryToFindExactType)

Try to get the parameterized type from the cache.

public static ClassNode
getSuperClass(final ClassNode type, final ClassNode target)

public static boolean
hasNonPlaceHolders(final ClassNode type)

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

public static boolean
hasPlaceHolders(final ClassNode type)

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

public static boolean
hasUnresolvedGenerics(final ClassNode type)

Checks for any placeholder (aka unresolved) generics.

public static ClassNode
makeClassSafe(Class<T> klass)

public static ClassNode
makeClassSafe0(ClassNode type, GenericsType... genericTypes)

public static ClassNode
makeClassSafeWithGenerics(Class<T> klass, ClassNode genericsType)

public static ClassNode
public static Map<GenericsType, GenericsType>
makeDeclaringAndActualGenericsTypeMap(final ClassNode declaringClass, final 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>

Returns:

the placeholder-to-actualtype mapping
makeDeclaringAndActualGenericsTypeMapOfExactType
(final ClassNode
the generics class node declaring the generics types
declaringClass
,
final ClassNode
the subclass class node
actualReceiver
)

The method is similar with GenericsUtils#makeDeclaringAndActualGenericsTypeMap(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

public static ClassNode
public static ClassNode
nonGeneric(final ClassNode type)

public static ClassNode

Returns:

a parameterized interface class node
parameterizeInterfaceGenerics
(final ClassNode
the class node where generics types are parameterized
hint
,
final ClassNode
the interface we want to parameterize generics types
target
)

Deprecated Use #parameterizeType instead
Interface class nodes retrieved from org.codehaus.groovy.ast.ClassNode#getInterfaces() or org.codehaus.groovy.ast.ClassNode#getAllInterfaces() are returned with generic type arguments.
public static Tuple2<ClassNode[], ClassNode>
parameterizeSAM(final ClassNode
the class node which contains only one abstract method
samType
)

Gets the parameter and return types of the abstract method of SAM.

public static ClassNode

Returns:

a parameterized interface ClassNode
parameterizeType
(ClassNode
the ClassNode where generics types are parameterized
hint
,
final ClassNode
the interface we want to parameterize generics types
target
)

Interface class nodes retrieved from org.codehaus.groovy.ast.ClassNode#getInterfaces() or org.codehaus.groovy.ast.ClassNode#getAllInterfaces() are returned with generic type arguments.

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

private static ClassNode
resolveClassNode(final SourceUnit sourceUnit, final CompilationUnit compilationUnit, final MethodNode mn, final ASTNode usage, final ClassNode parsedNode)

public static String
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

EMPTY_GENERICS_ARRAYback to summary
public static final GenericsType[] EMPTY_GENERICS_ARRAY
JAVA_LANG_OBJECTback to summary
public static final String JAVA_LANG_OBJECT
PARAMETERIZED_TYPE_CACHEback to summary
private static final EvictableCache<GenericsUtils.ParameterizedTypeCacheKey, SoftReference<ClassNode>> PARAMETERIZED_TYPE_CACHE
PARAMETERIZED_TYPE_CACHE_ENABLEDback to summary
private static final boolean PARAMETERIZED_TYPE_CACHE_ENABLED

Constructor Detail

GenericsUtilsback to summary
public GenericsUtils()

Method Detail

addMethodGenericsback to summary
public static Map<String, ClassNode> addMethodGenerics(MethodNode current, Map<String, ClassNode> oldSpec)
alignGenericTypesback to summary
public static GenericsType[] alignGenericTypes(final GenericsType[] redirectGenericTypes, final GenericsType[] parameterizedTypes, final 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:GenericsType[]

the type arguments or the redirect class node

parameterizedTypes:GenericsType[]

the actual type arguments used on this class node

alignmentTarget:GenericsType[]

the generic type arguments to which we want to align to

Returns:GenericsType[]

aligned type arguments

Annotations
@Deprecated
applyGenericsContextToPlaceHoldersback to summary
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:Map<String, ClassNode>

the generics context information spec

oldPlaceHolders:GenericsType[]

the old placeholders

Returns:GenericsType[]

the new generics types

buildWildcardTypeback to summary
public static GenericsType buildWildcardType(final ClassNode... upperBounds)

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

checkPlaceHoldersback to summary
private static boolean checkPlaceHolders(final ClassNode type, final Predicate<GenericsType> p)
clearParameterizedTypeCacheback to summary
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

correctToGenericsSpecback to summary
public static MethodNode correctToGenericsSpec(Map<String, ClassNode> genericsSpec, MethodNode mn)
correctToGenericsSpecback to summary
public static ClassNode correctToGenericsSpec(Map<String, ClassNode> genericsSpec, GenericsType type)
correctToGenericsSpecback to summary
public static ClassNode correctToGenericsSpec(Map<String, ClassNode> genericsSpec, ClassNode type)
correctToGenericsSpecRecurseback to summary
public static ClassNode correctToGenericsSpecRecurse(Map<String, ClassNode> genericsSpec, ClassNode type)
correctToGenericsSpecRecurseback to summary
public static ClassNode[] correctToGenericsSpecRecurse(Map<String, ClassNode> genericsSpec, ClassNode[] types)
Since
2.4.1
correctToGenericsSpecRecurseback to summary
public static ClassNode correctToGenericsSpecRecurse(Map<String, ClassNode> genericsSpec, ClassNode type, List<String> exclusions)
correlateTypeParametersAndTypeArgumentsback to summary
private static Map<GenericsType, GenericsType> correlateTypeParametersAndTypeArguments(final ClassNode declaringClass, final ClassNode actualReceiver, final boolean tryToFindExactType)
createGenericsSpecback to summary
public static Map<String, ClassNode> createGenericsSpec(ClassNode current)
createGenericsSpecback to summary
public static Map<String, ClassNode> createGenericsSpec(ClassNode current, Map<String, ClassNode> oldSpec)
erasureback to summary
private static GenericsType erasure(GenericsType gt)
See Also
org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport#extractType(GenericsType)
extractPlaceholdersback to summary
public static Map<GenericsType.GenericsTypeName, GenericsType> extractPlaceholders(final ClassNode type)

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

Parameters
type:ClassNode

the class node to check

extractPlaceholdersback to summary
public static void extractPlaceholders(final ClassNode type, final Map<GenericsType.GenericsTypeName, GenericsType> placeholders)

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

Parameters
type:ClassNode

the class node to check

placeholders:Map<GenericsType.GenericsTypeName, GenericsType>

the generics type information collector

extractSuperClassGenericsback to summary
public static void extractSuperClassGenerics(final ClassNode type, final ClassNode target, final Map<String, ClassNode> spec)
extractSuperClassGenericsback to summary
private static void extractSuperClassGenerics(final GenericsType[] usage, final GenericsType[] declaration, final Map<String, ClassNode> spec)
extractSuperClassGenericsback to summary
private static void extractSuperClassGenerics(ClassNode[] usage, ClassNode[] declaration, Map<String, ClassNode> spec)
findActualTypeByGenericsPlaceholderNameback to summary
public static ClassNode findActualTypeByGenericsPlaceholderName(final String placeholderName, final Map<GenericsType, GenericsType> genericsPlaceholderAndTypeMap)

Gets the actual type according to the placeholder name.

Parameters
placeholderName:String

the placeholder name (i.e. "T", "E", etc.)

genericsPlaceholderAndTypeMap:Map<GenericsType, GenericsType>

the result of makeDeclaringAndActualGenericsTypeMap(ClassNode, ClassNode)

findParameterizedTypeback to summary
public static ClassNode findParameterizedType(final ClassNode genericsClass, final ClassNode actualType)

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

findParameterizedTypeback to summary
public static ClassNode findParameterizedType(final ClassNode genericsClass, final ClassNode actualType, final 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.

findParameterizedTypeFromCacheback to summary
public static ClassNode findParameterizedTypeFromCache(final ClassNode genericsClass, final ClassNode actualType)

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

findParameterizedTypeFromCacheback to summary
public static ClassNode findParameterizedTypeFromCache(final ClassNode genericsClass, final 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)

getSuperClassback to summary
public static ClassNode getSuperClass(final ClassNode type, final ClassNode target)
hasNonPlaceHoldersback to summary
public static boolean hasNonPlaceHolders(final ClassNode type)

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

Since
3.0.0
hasPlaceHoldersback to summary
public static boolean hasPlaceHolders(final ClassNode type)

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

Since
3.0.0
hasUnresolvedGenericsback to summary
public static boolean hasUnresolvedGenerics(final ClassNode type)

Checks for any placeholder (aka unresolved) generics.

makeClassSafeback to summary
public static ClassNode makeClassSafe(Class<T> klass)
makeClassSafe0back to summary
public static ClassNode makeClassSafe0(ClassNode type, GenericsType... genericTypes)
makeClassSafeWithGenericsback to summary
public static ClassNode makeClassSafeWithGenerics(Class<T> klass, ClassNode genericsType)
makeClassSafeWithGenericsback to summary
public static ClassNode makeClassSafeWithGenerics(ClassNode type, GenericsType... genericTypes)
makeDeclaringAndActualGenericsTypeMapback to summary
public static Map<GenericsType, GenericsType> makeDeclaringAndActualGenericsTypeMap(final ClassNode declaringClass, final 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

makeDeclaringAndActualGenericsTypeMapOfExactTypeback to summary
public static Map<GenericsType, GenericsType> makeDeclaringAndActualGenericsTypeMapOfExactType(final ClassNode declaringClass, final ClassNode actualReceiver)

The method is similar with GenericsUtils#makeDeclaringAndActualGenericsTypeMap(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:ClassNode

the generics class node declaring the generics types

actualReceiver:ClassNode

the subclass class node

Returns:Map<GenericsType, GenericsType>

the placeholder-to-actualtype mapping

Since
3.0.0
newClassback to summary
public static ClassNode newClass(ClassNode type)
nonGenericback to summary
public static ClassNode nonGeneric(final ClassNode type)
parameterizeInterfaceGenericsback to summary
public static ClassNode parameterizeInterfaceGenerics(final ClassNode hint, final ClassNode target)

Deprecated

Use #parameterizeType instead

Interface class nodes retrieved from org.codehaus.groovy.ast.ClassNode#getInterfaces() or org.codehaus.groovy.ast.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:ClassNode

the class node where generics types are parameterized

target:ClassNode

the interface we want to parameterize generics types

Returns:ClassNode

a parameterized interface class node

Annotations
@Deprecated
parameterizeSAMback to summary
public static Tuple2<ClassNode[], ClassNode> parameterizeSAM(final 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 java.util.function.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:ClassNode

the class node which contains only one abstract method

Since
3.0.0
parameterizeTypeback to summary
public static ClassNode parameterizeType(ClassNode hint, final ClassNode target)

Interface class nodes retrieved from org.codehaus.groovy.ast.ClassNode#getInterfaces() or org.codehaus.groovy.ast.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:ClassNode

the ClassNode where generics types are parameterized

target:ClassNode

the interface we want to parameterize generics types

Returns:ClassNode

a parameterized interface ClassNode

parseClassNodesFromStringback to summary
public static ClassNode[] parseClassNodesFromString(final String option, final SourceUnit sourceUnit, final CompilationUnit compilationUnit, final MethodNode mn, final ASTNode usage)
resolveClassNodeback to summary
private static ClassNode resolveClassNode(final SourceUnit sourceUnit, final CompilationUnit compilationUnit, final MethodNode mn, final ASTNode usage, final ClassNode parsedNode)
toGenericTypesStringback to summary
public static String toGenericTypesString(GenericsType[] genericsTypes)
org.codehaus.groovy.ast.tools back to summary

private Class GenericsUtils.ParameterizedTypeCacheKey

extends Object
Class Inheritance
  • java.lang.Object
  • org.codehaus.groovy.ast.tools.GenericsUtils.ParameterizedTypeCacheKey

Field Summary

Modifier and TypeField and Description
private ClassNode
private ClassNode

Constructor Summary

AccessConstructor and Description
public
ParameterizedTypeCacheKey(ClassNode genericsClass, ClassNode actualType)

Method Summary

Modifier and TypeMethod and Description
public boolean
equals(Object
the reference object with which to compare.
o
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public ClassNode
public ClassNode
public int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
public void
public void
setGenericsClass(ClassNode genericsClass)

Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAlltoStringwaitwaitwait

Field Detail

actualTypeback to summary
private ClassNode actualType
genericsClassback to summary
private ClassNode genericsClass

Constructor Detail

ParameterizedTypeCacheKeyback to summary
public ParameterizedTypeCacheKey(ClassNode genericsClass, ClassNode actualType)

Method Detail

equalsback to summary
public boolean equals(Object o)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
o:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
getActualTypeback to summary
public ClassNode getActualType()
getGenericsClassback to summary
public ClassNode getGenericsClass()
hashCodeback to summary
public int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object.

Annotations
@Override
setActualTypeback to summary
public void setActualType(ClassNode actualType)
Annotations
@SuppressWarnings:unused
setGenericsClassback to summary
public void setGenericsClass(ClassNode genericsClass)
Annotations
@SuppressWarnings:unused