Class StaticTypeCheckingSupport
java.lang.Object
org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport
Support methods for
StaticTypeCheckingVisitor.-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classDGM-like helpers forboolean[]access.static classDGM-like helpers forbyte[]access.static classDGM-like helpers forchar[]access.static classDGM-like helpers fordouble[]access.static classDGM-like helpers forfloat[]access.static classDGM-like helpers forint[]access.static classDGM-like helpers forlong[]access.static classDGM-like helpers for object arrays.static classDGM-like helpers forshort[]access. -
Field Summary
FieldsModifier and TypeFieldDescriptionprotected static final ClassNodeCachedArrayListtype.protected static final ClassNodeCachedBaseStreamtype.protected static final ClassNodeprotected static final ClassNodeprotected static final Comparator<MethodNode>This comparator is used when we return the list of methods from DGM which name correspond to a given name.protected static final ExtensionMethodCacheShared cache of extension methods visible to the static type checker.protected static final ClassNodeprotected static final ClassNodeCachedLinkedHashMaptype.protected static final ClassNodeCachedLinkedHashSettype.protected static final ClassNodeCachedMatchertype.protected static final ClassNodeThis is for internal use only. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic intallParametersAndArgumentsMatch(Parameter[] parameters, ClassNode[] argumentTypes) Determines if the specific argument types fit the parameters.static booleancheckCompatibleAssignmentTypes(ClassNode left, ClassNode right) Returns true or false depending on whether the right classnode can be assigned to the left classnode.static booleancheckCompatibleAssignmentTypes(ClassNode left, ClassNode right, Expression rightExpression) Checks assignment compatibility using the supplied right-hand expression for additional context.static booleancheckCompatibleAssignmentTypes(ClassNode left, ClassNode right, Expression rightExpression, boolean allowConstructorCoercion) Everything that can be done bycastToTypeshould be allowed for assignment.static List<MethodNode>chooseBestMethod(ClassNode receiver, Collection<MethodNode> methods, ClassNode... argumentTypes) Returns the method(s) which best fit the argument types.static voidClears all cached extension methods.static voidclearExtensionMethodCache(ClassLoader loader) Clears cached extension methods for the supplied class loader.Deprecated, for removal: This API element is subject to removal in a future version.static ObjectevaluateExpression(Expression expr, CompilerConfiguration config) Deprecated, for removal: This API element is subject to removal in a future version.static ObjectevaluateExpression(Expression expr, CompilerConfiguration config, GroovyClassLoader loader) Evaluates expressions as found in annotation parameters.static List<MethodNode>filterMethodsByVisibility(List<MethodNode> methodNodeList, ClassNode enclosingClassNode) Filter methods according to visibilitystatic List<MethodNode>findDGMMethodsByNameAndArguments(ClassLoader loader, ClassNode receiver, String name, ClassNode[] args) Returns matching extension methods for the supplied receiver and argument types.static List<MethodNode>findDGMMethodsByNameAndArguments(ClassLoader loader, ClassNode receiver, String name, ClassNode[] args, List<MethodNode> methods) Adds matching extension methods for the supplied receiver and argument types.static List<MethodNode>findDGMMethodsByNameAndArguments(ClassNode receiver, String name, ClassNode[] args) Deprecated, for removal: This API element is subject to removal in a future version.static List<MethodNode>findDGMMethodsByNameAndArguments(ClassNode receiver, String name, ClassNode[] args, List<MethodNode> methods) Deprecated, for removal: This API element is subject to removal in a future version.static Set<MethodNode>findDGMMethodsForClassNode(ClassLoader loader, ClassNode clazz, String name) Returns extension methods with the supplied name for the receiver hierarchy.protected static voidfindDGMMethodsForClassNode(ClassLoader loader, ClassNode clazz, String name, TreeSet<MethodNode> accumulator) Collects extension methods with the supplied name for the receiver hierarchy.protected static Set<MethodNode>findDGMMethodsForClassNode(ClassNode clazz, String name) Deprecated, for removal: This API element is subject to removal in a future version.protected static voidfindDGMMethodsForClassNode(ClassNode clazz, String name, TreeSet<MethodNode> accumulator) Deprecated, for removal: This API element is subject to removal in a future version.static List<MethodNode>findSetters(ClassNode cn, String setterName, boolean voidOnly) Deprecated.protected static VariableGiven a variable expression, returns the ultimately accessed variable.protected static GenericsTypefullyResolve(GenericsType gt, Map<GenericsType.GenericsTypeName, GenericsType> placeholders) Given a generics type representing SomeClass<T,V> and a resolved placeholder map, returns a new generics type for which placeholders are resolved recursively.protected static ClassNodefullyResolveType(ClassNode type, Map<GenericsType.GenericsTypeName, GenericsType> placeholders) Resolves placeholders and wildcard bounds within the supplied type.static StringgetAssignOperationName(int op) GEP-15: returns the dedicated compound-assignment method name for a compound-assign operator token (e.g.static ClassNodegetCorrectedClassNode(ClassNode cn, ClassNode sc, boolean completed) Deprecated, for removal: This API element is subject to removal in a future version.static booleanimplementsInterfaceOrIsSubclassOf(ClassNode type, ClassNode superOrInterface) Indicates whethertypeimplements or inherits fromsuperOrInterface.protected static booleanisArrayAccessExpression(Expression expression) Returns true for expressions of the form x[...]static booleanisAssignableTo(ClassNode type, ClassNode toBeAssignedTo) Checks if a class node is assignable to another.static booleanisAssignment(int op) Indicates whether the token represents any assignment form.static booleanisBeingCompiled(ClassNode node) Indicates whether the class node is still attached to a compile unit.static booleanisClassClassNodeWrappingConcreteType(ClassNode classNode) Returns true if the class node represents a class node for the Class class and if the parametrized type is a neither a placeholder nor a wildcard.static booleanisCompareToBoolean(int op) Indicates whether the operation maps to a booleancompareToresult.static booleanstatic booleanstatic booleanstatic ClassNodeReturns the trait self type referenced by the supplied variable, if any.static booleanIndicates whether the wildcard has no explicit bounds.static booleanReturns true if a class makes use of generic types.static booleanReturns true if the provided class node, when considered as a receiver of a message or as a parameter, is using a placeholder in its generics type.static booleanTells if a class is one of the "accept all" classes as the left hand side of an assignment.static booleanisWithCall(String name, Expression arguments) Called on method call checks in order to determine if a method call corresponds to the idiomatic o.with { ...static booleanDetermines if node is a raw type or references any generics placeholders.static Parameter[]parameterizeArguments(ClassNode receiver, MethodNode m) Given a receiver and a method node, parameterize the method arguments using available generic type information.static ClassNoderesolveClassNodeGenerics(Map<GenericsType.GenericsTypeName, GenericsType> resolvedPlaceholders, Map<GenericsType.GenericsTypeName, GenericsType> placeholdersFromContext, ClassNode currentType) Applies resolved placeholders and the surrounding context to the supplied type.protected static booleantypeCheckMethodArgumentWithGenerics(ClassNode parameterType, ClassNode argumentType, boolean lastArg) Checks that the parameterized generics of an argument are compatible with the generics of the parameter.protected static booleantypeCheckMethodsWithGenerics(ClassNode receiver, ClassNode[] argumentTypes, MethodNode candidateMethod) Checks whether the candidate method remains compatible after applying generics.
-
Field Details
-
Matcher_TYPE
CachedMatchertype. -
ArrayList_TYPE
CachedArrayListtype. -
BaseStream_TYPE
CachedBaseStreamtype. -
Collection_TYPE
-
Deprecated_TYPE
-
LinkedHashMap_TYPE
CachedLinkedHashMaptype. -
LinkedHashSet_TYPE
CachedLinkedHashSettype. -
NUMBER_TYPES
-
NUMBER_OPS
-
GSTRING_STRING_CLASSNODE
-
UNKNOWN_PARAMETER_TYPE
This is for internal use only. When an argument method is null, we cannot determine its type, so we use this one as a wildcard. -
DGM_METHOD_NODE_COMPARATOR
This comparator is used when we return the list of methods from DGM which name correspond to a given name. As we also look up for DGM methods of superclasses or interfaces, it may be possible to find two methods which have the same name and the same arguments. In that case, we should not add the method from superclass or interface otherwise the system won't be able to select the correct method, resulting in an ambiguous method selection for similar methods. -
EXTENSION_METHOD_CACHE
Shared cache of extension methods visible to the static type checker.
-
-
Constructor Details
-
StaticTypeCheckingSupport
public StaticTypeCheckingSupport()
-
-
Method Details
-
clearExtensionMethodCache
Clears cached extension methods for the supplied class loader. -
clearExtensionMethodCache
public static void clearExtensionMethodCache()Clears all cached extension methods. -
isArrayAccessExpression
Returns true for expressions of the form x[...]- Parameters:
expression- an expression- Returns:
- true for array access expressions
-
isWithCall
Called on method call checks in order to determine if a method call corresponds to the idiomatic o.with { ... } structure- Parameters:
name- name of the method calledarguments- method call arguments- Returns:
- true if the name is "with" and arguments consist of a single closure
-
findTargetVariable
Given a variable expression, returns the ultimately accessed variable.- Parameters:
ve- a variable expression- Returns:
- the target variable
-
findDGMMethodsForClassNode
@Deprecated(forRemoval=true, since="2.2.0") protected static Set<MethodNode> findDGMMethodsForClassNode(ClassNode clazz, String name) Deprecated, for removal: This API element is subject to removal in a future version. -
findDGMMethodsForClassNode
public static Set<MethodNode> findDGMMethodsForClassNode(ClassLoader loader, ClassNode clazz, String name) Returns extension methods with the supplied name for the receiver hierarchy. -
findDGMMethodsForClassNode
@Deprecated(forRemoval=true, since="2.2.0") protected static void findDGMMethodsForClassNode(ClassNode clazz, String name, TreeSet<MethodNode> accumulator) Deprecated, for removal: This API element is subject to removal in a future version. -
findDGMMethodsForClassNode
protected static void findDGMMethodsForClassNode(ClassLoader loader, ClassNode clazz, String name, TreeSet<MethodNode> accumulator) Collects extension methods with the supplied name for the receiver hierarchy. -
allParametersAndArgumentsMatch
Determines if the specific argument types fit the parameters.- Returns:
- -1 if arguments do not match, 0 if arguments are of the exact type, and > 0 when one or more argument is not of the exact type but still match
-
isAssignableTo
Checks if a class node is assignable to another. This is used for example in assignment checks where you want to verify that the assignment is valid.- Returns:
- true if the class node is assignable to the other class node, false otherwise
-
isCompareToBoolean
public static boolean isCompareToBoolean(int op) Indicates whether the operation maps to a booleancompareToresult. -
getAssignOperationName
GEP-15: returns the dedicated compound-assignment method name for a compound-assign operator token (e.g.PLUS_EQUAL->"plusAssign"), ornullif the token is not one of the twelve operators in scope.INTDIV_EQUAL,MOD_EQUAL,ELVIS_EQUAL,LOGICAL_OR_EQUALandLOGICAL_AND_EQUALare intentionally excluded.- Since:
- 6.0.0
-
isAssignment
public static boolean isAssignment(int op) Indicates whether the token represents any assignment form. -
checkCompatibleAssignmentTypes
Returns true or false depending on whether the right classnode can be assigned to the left classnode. This method should not add errors by itself: we let the caller decide what to do if an incompatible assignment is found.- Parameters:
left- the class to be assigned toright- the assignee class- Returns:
- false if types are incompatible
-
checkCompatibleAssignmentTypes
public static boolean checkCompatibleAssignmentTypes(ClassNode left, ClassNode right, Expression rightExpression) Checks assignment compatibility using the supplied right-hand expression for additional context. -
checkCompatibleAssignmentTypes
public static boolean checkCompatibleAssignmentTypes(ClassNode left, ClassNode right, Expression rightExpression, boolean allowConstructorCoercion) Everything that can be done bycastToTypeshould be allowed for assignment. -
isWildcardLeftHandSide
Tells if a class is one of the "accept all" classes as the left hand side of an assignment.- Parameters:
node- the classnode to test- Returns:
- true if it's an Object, String, boolean, Boolean or Class.
-
isBeingCompiled
Indicates whether the class node is still attached to a compile unit. -
implementsInterfaceOrIsSubclassOf
Indicates whethertypeimplements or inherits fromsuperOrInterface. -
findDGMMethodsByNameAndArguments
@Deprecated(forRemoval=true, since="2.2.0") public static List<MethodNode> findDGMMethodsByNameAndArguments(ClassNode receiver, String name, ClassNode[] args) Deprecated, for removal: This API element is subject to removal in a future version. -
findDGMMethodsByNameAndArguments
public static List<MethodNode> findDGMMethodsByNameAndArguments(ClassLoader loader, ClassNode receiver, String name, ClassNode[] args) Returns matching extension methods for the supplied receiver and argument types. -
findDGMMethodsByNameAndArguments
@Deprecated(forRemoval=true, since="2.2.0") public static List<MethodNode> findDGMMethodsByNameAndArguments(ClassNode receiver, String name, ClassNode[] args, List<MethodNode> methods) Deprecated, for removal: This API element is subject to removal in a future version. -
findDGMMethodsByNameAndArguments
public static List<MethodNode> findDGMMethodsByNameAndArguments(ClassLoader loader, ClassNode receiver, String name, ClassNode[] args, List<MethodNode> methods) Adds matching extension methods for the supplied receiver and argument types. -
isUsingUncheckedGenerics
Returns true if the provided class node, when considered as a receiver of a message or as a parameter, is using a placeholder in its generics type. In this case, we're facing unchecked generics and type checking is limited (ex: void foo(Set s) { s.keySet() }- Parameters:
node- the node to test- Returns:
- true if it is using any placeholder in generics types
-
chooseBestMethod
public static List<MethodNode> chooseBestMethod(ClassNode receiver, Collection<MethodNode> methods, ClassNode... argumentTypes) Returns the method(s) which best fit the argument types.- Returns:
- zero or more results
-
parameterizeArguments
Given a receiver and a method node, parameterize the method arguments using available generic type information.- Parameters:
receiver- the classm- the method- Returns:
- the parameterized arguments
-
isUsingGenericsOrIsArrayUsingGenerics
Returns true if a class makes use of generic types. If node represents an array type, then checks if the component type is using generics.- Parameters:
cn- a class node for which to check if it is using generics- Returns:
- true if the type (or component type) is using generics
-
fullyResolve
protected static GenericsType fullyResolve(GenericsType gt, Map<GenericsType.GenericsTypeName, GenericsType> placeholders) Given a generics type representing SomeClass<T,V> and a resolved placeholder map, returns a new generics type for which placeholders are resolved recursively. -
fullyResolveType
protected static ClassNode fullyResolveType(ClassNode type, Map<GenericsType.GenericsTypeName, GenericsType> placeholders) Resolves placeholders and wildcard bounds within the supplied type. -
typeCheckMethodArgumentWithGenerics
protected static boolean typeCheckMethodArgumentWithGenerics(ClassNode parameterType, ClassNode argumentType, boolean lastArg) Checks that the parameterized generics of an argument are compatible with the generics of the parameter.- Parameters:
parameterType- the parameter type of a methodargumentType- the type of the argument passed to the method
-
typeCheckMethodsWithGenerics
protected static boolean typeCheckMethodsWithGenerics(ClassNode receiver, ClassNode[] argumentTypes, MethodNode candidateMethod) Checks whether the candidate method remains compatible after applying generics. -
resolveClassNodeGenerics
public static ClassNode resolveClassNodeGenerics(Map<GenericsType.GenericsTypeName, GenericsType> resolvedPlaceholders, Map<GenericsType.GenericsTypeName, GenericsType> placeholdersFromContext, ClassNode currentType) Applies resolved placeholders and the surrounding context to the supplied type. -
isUnboundedWildcard
Indicates whether the wildcard has no explicit bounds. -
filterMethodsByVisibility
public static List<MethodNode> filterMethodsByVisibility(List<MethodNode> methodNodeList, ClassNode enclosingClassNode) Filter methods according to visibility- Parameters:
methodNodeList- method nodes to filterenclosingClassNode- the enclosing class- Returns:
- filtered method nodes
- Since:
- 3.0.0
-
isGStringOrGStringStringLUB
- Returns:
- true if the class node is either a GString or the LUB of String and GString.
-
isParameterizedWithGStringOrGStringString
- Parameters:
node- the node to be tested- Returns:
- true if the node is using generics types and one of those types is a gstring or string/gstring lub
-
isParameterizedWithString
- Parameters:
node- the node to be tested- Returns:
- true if the node is using generics types and one of those types is a string
-
missesGenericsTypes
Determines if node is a raw type or references any generics placeholders. -
evaluateExpression
@Deprecated(forRemoval=true, since="4.0.7") public static Object evaluateExpression(Expression expr, CompilerConfiguration config) Deprecated, for removal: This API element is subject to removal in a future version. -
evaluateExpression
public static Object evaluateExpression(Expression expr, CompilerConfiguration config, GroovyClassLoader loader) Evaluates expressions as found in annotation parameters. For example, it will evaluate a constant, be it referenced directly as an integer or as a reference to a field.If the expression cannot be evaluated on its own, an exception is thrown.
- Parameters:
expr- the expression to be evaluatedconfig- the compiler configurationloader- the compiler class loader- Returns:
- the result of the expression
- Throws:
GroovyBugError
-
collectAllInterfaces
@Deprecated(forRemoval=true, since="4.0.0") public static Set<ClassNode> collectAllInterfaces(ClassNode node) Deprecated, for removal: This API element is subject to removal in a future version.Collects all interfaces of a class node, including those defined by the super class.- Parameters:
node- a class for which we want to retrieve all interfaces- Returns:
- a set of interfaces implemented by this class node
-
getCorrectedClassNode
@Deprecated(forRemoval=true, since="4.0.0") public static ClassNode getCorrectedClassNode(ClassNode cn, ClassNode sc, boolean completed) Deprecated, for removal: This API element is subject to removal in a future version.Corrects the supplied class node against a generics specification. -
isClassClassNodeWrappingConcreteType
Returns true if the class node represents a class node for the Class class and if the parametrized type is a neither a placeholder nor a wildcard. For example, the class node Class<Foo> where Foo is a class would return true, but the class node for Class<?> would return false.- Parameters:
classNode- a class node to be tested- Returns:
- true if it is the class node for Class and its generic type is a real class
-
findSetters
@Deprecated(since="5.0.0") public static List<MethodNode> findSetters(ClassNode cn, String setterName, boolean voidOnly) Deprecated.Returns matching setter methods from the class and its interfaces. -
isTraitSelf
Returns the trait self type referenced by the supplied variable, if any.
-
evaluateExpression(Expression, CompilerConfiguration, GroovyClassLoader)instead