Class GroovyTypeCheckingExtensionSupport
java.lang.Object
org.codehaus.groovy.transform.stc.TypeCheckingExtension
org.codehaus.groovy.transform.stc.AbstractTypeCheckingExtension
org.codehaus.groovy.transform.stc.GroovyTypeCheckingExtensionSupport
Base class for type checking extensions written in Groovy. Compared to its superclass,
TypeCheckingExtension,
this class adds a number of utility methods aimed at leveraging the syntax of the Groovy language to improve
expressiveness and conciseness.- Since:
- 2.1.0
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classEvent handler registration: setup Registers closure that runs after the type checker finishes initialization finish Registers closure that runs after the type checker completes type checking beforeVisitClass Registers closure that runs before type checking a class afterVisitClass Registers closure that runs after having finished the visit of a type checked class beforeVisitMethod Registers closure that runs before type checking a method body afterVisitMethod Registers closure that runs after type checking a method body beforeMethodCall Registers closure that runs before the type checker starts type checking a method call afterMethodCall Registers closure that runs once the type checker has finished type checking a method call methodNotFound Registers closure that runs when it fails to find an appropriate method for a method call ambiguousMethods Registers closure that runs when the type checker cannot choose between several candidate methods onMethodSelection Registers closure that runs when it finds a method appropriate for a method call unresolvedVariable Registers closure that runs when the type checker finds an unresolved variable unresolvedProperty Registers closure that runs when the type checker cannot find a property on the receiver unresolvedAttribute Registers closure that runs when the type checker cannot find an attribute on the receiver incompatibleAssignment Registers closure that runs when the type checker thinks that the right-hand side of an assignment is incompatible with the left-hand side incompatibleReturnType Registers closure that runs when the type checker thinks that a return value is incompatibe with the return type Expression categorization: isAnnotationConstantExpression Determines if argument is anAnnotationConstantExpressionisArgumentListExpression Determines if argument is anArgumentListExpressionisArrayExpression Determines if argument is anArrayExpressionisAttributeExpression Determines if argument is anAttributeExpressionisBinaryExpression Determines if argument is aBinaryExpressionisBitwiseNegationExpression Determines if argument is aBitwiseNegationExpressionisBooleanExpression Determines if argument is aBooleanExpressionisCastExpression Determines if argument is aCastExpressionisClassExpression Determines if argument is aClassExpressionisClosureExpression Determines if argument is aClosureExpressionisConstantExpression Determines if argument is aConstantExpressionisConstructorCallExpression Determines if argument is aConstructorCallExpressionisDeclarationExpression Determines if argument is aDeclarationExpressionisElvisOperatorExpression Determines if argument is anElvisOperatorExpressionisEmptyExpression Determines if argument is anEmptyExpressionisFieldExpression Determines if argument is aFieldExpressionisGStringExpression Determines if argument is aGStringExpressionisLambdaExpression Determines if argument is aLambdaExpressionisListExpression Determines if argument is aListExpressionisMapExpression Determines if argument is aMapExpressionisMapEntryExpression Determines if argument is aMapEntryExpressionisMethodCallExpression Determines if argument is aMethodCallExpressionisMethodPointerExpression Determines if argument is aMethodPointerExpressionisMethodReferenceExpression Determines if argument is aMethodReferenceExpressionisNamedArgumentListExpression Determines if argument is aNamedArgumentListExpressionisNotExpression Determines if argument is aNotExpressionisPostfixExpression Determines if argument is aPostfixExpressionisPrefixExpression Determines if argument is aPrefixExpressionisPropertyExpression Determines if argument is aPropertyExpressionisRangeExpression Determines if argument is aRangeExpressionisSpreadExpression Determines if argument is aSpreadExpressionisSpreadMapExpression Determines if argument is aSpreadMapExpressionisStaticMethodCallExpression Determines if argument is aStaticMethodCallExpressionisTernaryExpression Determines if argument is aTernaryExpressionisTupleExpression Determines if argument is aTupleExpressionisUnaryMinusExpression Determines if argument is aUnaryMinusExpressionisUnaryPlusExpression Determines if argument is aUnaryPlusExpressionisVariableExpression Determines if argument is aVariableExpressionGeneral utility: Delegates toAbstractTypeCheckingExtensionImports static members ofClassHelperImports static members ofStaticTypeCheckingSupportNested classes/interfaces inherited from class org.codehaus.groovy.transform.stc.AbstractTypeCheckingExtension
AbstractTypeCheckingExtension.TypeCheckingScope -
Field Summary
Fields inherited from class org.codehaus.groovy.transform.stc.AbstractTypeCheckingExtension
context, debug, handledFields inherited from class org.codehaus.groovy.transform.stc.TypeCheckingExtension
typeCheckingVisitor -
Constructor Summary
ConstructorsConstructorDescriptionGroovyTypeCheckingExtensionSupport(StaticTypeCheckingVisitor typeCheckingVisitor, String scriptPath, CompilationUnit compilationUnit) Builds a type checking extension relying on a Groovy script (type checking DSL). -
Method Summary
Modifier and TypeMethodDescriptionvoidafterMethodCall(MethodCall call) Allows the extension to perform additional tasks after the type checker actually visits a method call.voidafterVisitClass(ClassNode node) Allows the extension to perform additional tasks after the type checker actually visited a class node.voidafterVisitMethod(MethodNode node) Allows the extension to perform additional tasks after the type checker actually visited a method node.booleanbeforeMethodCall(MethodCall call) Allows the extension to perform additional tasks before the type checker actually visits a method call.booleanbeforeVisitClass(ClassNode node) Allows the extension to perform additional tasks before the type checker actually visits a class node.booleanbeforeVisitMethod(MethodNode node) Allows the extension to perform additional tasks before the type checker actually visits a method node.booleanvoidfinish()Subclasses should implement this method if they need to perform additional checks after the type checker has finished its work.handleAmbiguousMethods(List<MethodNode> nodes, Expression origin) This method is called by the type checker before throwing an "ambiguous method" error, giving the chance to the extension to select the method properly.booleanhandleIncompatibleAssignment(ClassNode lhsType, ClassNode rhsType, Expression assignmentExpression) This method is called by the type checker when an assignment is not allowed by the type checker.booleanhandleIncompatibleReturnType(ReturnStatement returnStatement, ClassNode inferredReturnType) Allows the extension to catch incompatible return types.handleMissingMethod(ClassNode receiver, String name, ArgumentListExpression argumentList, ClassNode[] argumentTypes, MethodCall call) This method is called by the type checker when a method call cannot be resolved.booleanThis method is called by the type checker when an attribute expression cannot be resolved (for example, when an attribute doesn't exist).booleanThis method is called by the type checker when a property expression cannot be resolved (for example, when a property doesn't exist).booleanThis method is called by the type checker when a variable expression cannot be resolved.inthashCode()voidonMethodSelection(Expression expression, MethodNode target) Allows the extension to listen to method selection events.voidsetDebug(boolean debug) voidsetup()Subclasses should implement this method whenever they need to perform special checks before the type checker starts working.Methods inherited from class org.codehaus.groovy.transform.stc.AbstractTypeCheckingExtension
argTypeMatches, argTypeMatches, argTypesMatches, argTypesMatches, delegatesTo, delegatesTo, delegatesTo, firstArgTypesMatches, firstArgTypesMatches, getArguments, getCurrentScope, getEnclosingBinaryExpression, getEnclosingBinaryExpressionStack, getEnclosingClassNode, getEnclosingClassNodes, getEnclosingClosure, getEnclosingClosureStack, getEnclosingMethod, getEnclosingMethodCall, getEnclosingMethodCalls, getEnclosingMethods, getGeneratedMethods, isAnnotatedBy, isAnnotatedBy, isDynamic, isExtensionMethod, isGenerated, isMethodCall, log, makeDynamic, makeDynamic, makeDynamic, makeDynamic, makeDynamic, makeDynamic, newMethod, newMethod, newMethod, newScope, newScope, popEnclosingBinaryExpression, popEnclosingClassNode, popEnclosingClosure, popEnclosingMethod, popEnclosingMethodCall, popTemporaryTypeInfo, pushEnclosingBinaryExpression, pushEnclosingClassNode, pushEnclosingClosureExpression, pushEnclosingMethod, pushEnclosingMethodCall, pushTemporaryTypeInfo, safeCall, scopeExit, scopeExit, setHandled, unique, withTypeCheckerMethods inherited from class org.codehaus.groovy.transform.stc.TypeCheckingExtension
addStaticTypeError, buildListType, buildMapType, classNodeFor, classNodeFor, existsProperty, existsProperty, extractStaticReceiver, getArgumentTypes, getTargetMethod, getType, isStaticMethodCallOnClass, lookupClassNodeFor, parameterizedType, storeType
-
Constructor Details
-
GroovyTypeCheckingExtensionSupport
public GroovyTypeCheckingExtensionSupport(StaticTypeCheckingVisitor typeCheckingVisitor, String scriptPath, CompilationUnit compilationUnit) Builds a type checking extension relying on a Groovy script (type checking DSL).- Parameters:
typeCheckingVisitor- the type checking visitorscriptPath- the path to the type checking script (in classpath)compilationUnit-
-
-
Method Details
-
equals
-
hashCode
public int hashCode() -
setDebug
public void setDebug(boolean debug) -
setup
public void setup()Description copied from class:TypeCheckingExtensionSubclasses should implement this method whenever they need to perform special checks before the type checker starts working.- Overrides:
setupin classTypeCheckingExtension
-
finish
public void finish()Description copied from class:TypeCheckingExtensionSubclasses should implement this method if they need to perform additional checks after the type checker has finished its work. This is particularly useful for situations where you need multiple passes. Some checks in that case may be deferred to the end, using this method.- Overrides:
finishin classTypeCheckingExtension
-
onMethodSelection
Description copied from class:TypeCheckingExtensionAllows the extension to listen to method selection events. Given an expression, which may be a method call expression, a static method call expression, a pre/postfix expression, ..., if a corresponding method is found, this method is called.- Overrides:
onMethodSelectionin classTypeCheckingExtension- Parameters:
expression- the expression for which a corresponding method has been foundtarget- the method which has been chosen by the type checker
-
afterMethodCall
Description copied from class:TypeCheckingExtensionAllows the extension to perform additional tasks after the type checker actually visits a method call. Compared to a custom visitor, this method ensures that the node being visited is a node which would have been visited by the type checker. This is in particular important for nodes which are marked withTypeCheckingMode.SKIP.- Overrides:
afterMethodCallin classTypeCheckingExtension- Parameters:
call- a method call, either aMethodCallExpression,StaticMethodCallExpression, orConstructorCallExpression
-
beforeMethodCall
Description copied from class:TypeCheckingExtensionAllows the extension to perform additional tasks before the type checker actually visits a method call. Compared to a custom visitor, this method ensures that the node being visited is a node which would have been visited by the type checker. This is in particular important for nodes which are marked withTypeCheckingMode.SKIP.- Overrides:
beforeMethodCallin classTypeCheckingExtension- Parameters:
call- a method call, either aMethodCallExpression,StaticMethodCallExpression, orConstructorCallExpression- Returns:
- false if the type checker should visit the node, or true if this extension replaces what the type checker would do with the method call.
-
handleUnresolvedVariableExpression
Description copied from class:TypeCheckingExtensionThis method is called by the type checker when a variable expression cannot be resolved. It gives the extension a chance to resolve it for the type checker.- Overrides:
handleUnresolvedVariableExpressionin classTypeCheckingExtension- Parameters:
vexp- the unresolved variable extension- Returns:
booleanfalse if the extension doesn't handle it, true if the extension handles this variable.
-
handleUnresolvedProperty
Description copied from class:TypeCheckingExtensionThis method is called by the type checker when a property expression cannot be resolved (for example, when a property doesn't exist). It gives the extension a chance to resolve it.- Overrides:
handleUnresolvedPropertyin classTypeCheckingExtension- Parameters:
pexp- the unresolved property- Returns:
booleanfalse if this extension doesn't resolve the property, true if it resolves the property.
-
handleUnresolvedAttribute
Description copied from class:TypeCheckingExtensionThis method is called by the type checker when an attribute expression cannot be resolved (for example, when an attribute doesn't exist). It gives the extension a chance to resolve it.- Overrides:
handleUnresolvedAttributein classTypeCheckingExtension- Parameters:
aexp- the unresolved attribute- Returns:
booleanfalse if this extension doesn't resolve the attribute, true if it resolves the attribute.
-
afterVisitMethod
Description copied from class:TypeCheckingExtensionAllows the extension to perform additional tasks after the type checker actually visited a method node. Compared to a custom visitor, this method ensures that the node being visited is a node which would have been visited by the type checker. This is in particular important for nodes which are marked withTypeCheckingMode.SKIP.- Overrides:
afterVisitMethodin classTypeCheckingExtension- Parameters:
node- a method node
-
beforeVisitClass
Description copied from class:TypeCheckingExtensionAllows the extension to perform additional tasks before the type checker actually visits a class node. Compared to a custom visitor, this method ensures that the node being visited is a node which would have been visited by the type checker. This is in particular important for nodes which are marked withTypeCheckingMode.SKIP.- Overrides:
beforeVisitClassin classTypeCheckingExtension- Parameters:
node- a class node- Returns:
- false if the type checker should visit the node, or true if this extension replaces what the type checker would do with the class.
-
afterVisitClass
Description copied from class:TypeCheckingExtensionAllows the extension to perform additional tasks after the type checker actually visited a class node. Compared to a custom visitor, this method ensures that the node being visited is a node which would have been visited by the type checker. This is in particular important for nodes which are marked withTypeCheckingMode.SKIP.- Overrides:
afterVisitClassin classTypeCheckingExtension- Parameters:
node- a class node
-
beforeVisitMethod
Description copied from class:TypeCheckingExtensionAllows the extension to perform additional tasks before the type checker actually visits a method node. Compared to a custom visitor, this method ensures that the node being visited is a node which would have been visited by the type checker. This is in particular important for nodes which are marked withTypeCheckingMode.SKIP.- Overrides:
beforeVisitMethodin classTypeCheckingExtension- Parameters:
node- a method node- Returns:
- false if the type checker should visit the node, or true if this extension replaces what the type checker would do with the method.
-
handleIncompatibleAssignment
public boolean handleIncompatibleAssignment(ClassNode lhsType, ClassNode rhsType, Expression assignmentExpression) Description copied from class:TypeCheckingExtensionThis method is called by the type checker when an assignment is not allowed by the type checker. Extensions may override this method to allow such assignments where the type checker normally disallows them.- Overrides:
handleIncompatibleAssignmentin classTypeCheckingExtension- Parameters:
lhsType- the type of the left hand side of the assignment, as found by the type checkerrhsType- the type of the right hand side of the assignment, as found by the type checkerassignmentExpression- the assignment expression which triggered this call- Returns:
booleanfalse if the extension does not handle this assignment, true otherwise
-
handleIncompatibleReturnType
public boolean handleIncompatibleReturnType(ReturnStatement returnStatement, ClassNode inferredReturnType) Description copied from class:TypeCheckingExtensionAllows the extension to catch incompatible return types. This event is called whenever the type checker finds that an inferred return type is incompatible with the declared return type of a method.- Overrides:
handleIncompatibleReturnTypein classTypeCheckingExtension- Parameters:
returnStatement- the statement that triggered the errorinferredReturnType- the inferred return type for this statement- Returns:
- false if the extension doesn't handle the error, true otherwise
-
handleMissingMethod
public List<MethodNode> handleMissingMethod(ClassNode receiver, String name, ArgumentListExpression argumentList, ClassNode[] argumentTypes, MethodCall call) Description copied from class:TypeCheckingExtensionThis method is called by the type checker when a method call cannot be resolved. Extensions may override this method to handle missing methods and prevent the type checker from throwing an error.- Overrides:
handleMissingMethodin classTypeCheckingExtension- Parameters:
receiver- the type of the receivername- the name of the called methodargumentList- the list of arguments of the callargumentTypes- the types of the arguments of the callcall- the method call itself, if needed- Returns:
- an empty list if the extension cannot resolve the method, or a list of potential methods if the extension finds candidates. This method must not return null.
-
handleAmbiguousMethods
Description copied from class:TypeCheckingExtensionThis method is called by the type checker before throwing an "ambiguous method" error, giving the chance to the extension to select the method properly. This means that when this method is called, the "nodes" parameter contains at least two methods. If the returned list still contains at least two methods, then the type checker will throw an ambiguous method call error. If the returned method contains 1 element, then the type checker will not throw any error. It is invalid to return an empty list.- Overrides:
handleAmbiguousMethodsin classTypeCheckingExtension- Parameters:
nodes- the list of ambiguous methodsorigin- the expression which originated the method selection process- Returns:
- a single element list of disambiguated selection, or more elements if still ambiguous. It is not allowed to return an empty list.
-