This interface defines a high-level API for handling type checking errors. As a dynamic language and a platform for developing DSLs, the Groovy language provides a lot of means to supply custom bindings or methods that are not possible to find at compile time. However, it is still possible to help the compiler, for example by telling it what is the type of an unresolved property. For basic DSL type checking, implementing those methods would help the type checker and make it silent where it normally throws errors.
Modifiers | Name | Description |
---|---|---|
protected StaticTypeCheckingVisitor |
typeCheckingVisitor |
Constructor and description |
---|
TypeCheckingExtension
(StaticTypeCheckingVisitor typeCheckingVisitor) |
Type Params | Return Type | Name and description |
---|---|---|
|
void |
addStaticTypeError(String msg, ASTNode expr) Adds a type checking error, which will be displayed to the user during compilation. |
|
void |
afterMethodCall(MethodCall call) Allows the extension to perform additional tasks after the type checker actually visits a method call. |
|
void |
afterVisitClass(ClassNode node) Allows the extension to perform additional tasks after the type checker actually visited a class node. |
|
void |
afterVisitMethod(MethodNode node) Allows the extension to perform additional tasks after the type checker actually visited a method node. |
|
boolean |
beforeMethodCall(MethodCall call) Allows the extension to perform additional tasks before the type checker actually visits a method call. |
|
boolean |
beforeVisitClass(ClassNode node) Allows the extension to perform additional tasks before the type checker actually visits a class node. |
|
boolean |
beforeVisitMethod(MethodNode node) Allows the extension to perform additional tasks before the type checker actually visits a method node. |
|
ClassNode |
buildListType(ClassNode componentType) Builds a parametrized class node for List, to represent List<X> |
|
ClassNode |
buildMapType(ClassNode keyType, ClassNode valueType) Builds a parametrized class node representing the Map<keyType,valueType> type. |
|
ClassNode |
classNodeFor(Class type) |
|
ClassNode |
classNodeFor(String type) |
|
boolean |
existsProperty(PropertyExpression pexp, boolean checkForReadOnly) |
|
boolean |
existsProperty(PropertyExpression pexp, boolean checkForReadOnly, ClassCodeVisitorSupport visitor) |
|
ClassNode |
extractStaticReceiver(MethodCall call) Given a method call, first checks that it's a static method call, and if it is, returns the class node for the receiver. |
|
void |
finish() Subclasses should implement this method if they need to perform additional checks after the type checker has finished its work. |
|
ClassNode[] |
getArgumentTypes(ArgumentListExpression args) |
|
MethodNode |
getTargetMethod(Expression expression) |
|
ClassNode |
getType(ASTNode exp) Returns the inferred type of an expression. |
|
List<MethodNode> |
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. |
|
boolean |
handleIncompatibleAssignment(ClassNode lhsType, ClassNode rhsType, Expression assignmentExpression) This method is called by the type checker when an assignment is not allowed by the type checker. |
|
boolean |
handleIncompatibleReturnType(ReturnStatement returnStatement, ClassNode inferredReturnType) Allows the extension to catch incompatible return types. |
|
List<MethodNode> |
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. |
|
boolean |
handleUnresolvedAttribute(AttributeExpression aexp) This method is called by the type checker when an attribute expression cannot be resolved (for example, when an attribute doesn't exist). |
|
boolean |
handleUnresolvedProperty(PropertyExpression pexp) This method is called by the type checker when a property expression cannot be resolved (for example, when a property doesn't exist). |
|
boolean |
handleUnresolvedVariableExpression(VariableExpression vexp) This method is called by the type checker when a variable expression cannot be resolved. |
|
boolean |
isStaticMethodCallOnClass(MethodCall call, ClassNode receiver) Given a method call, checks if it's a static method call and if it is, tells if the receiver matches the one supplied as an argument. |
|
ClassNode |
lookupClassNodeFor(String type) Lookup a ClassNode by its name from the source unit |
|
void |
onMethodSelection(Expression expression, MethodNode target) Allows the extension to listen to method selection events. |
|
ClassNode |
parameterizedType(ClassNode baseType, ClassNode... genericsTypeArguments) |
|
void |
setup() Subclasses should implement this method whenever they need to perform special checks before the type checker starts working. |
|
void |
storeType(Expression exp, ClassNode cn) Stores an inferred type for an expression. |
Adds a type checking error, which will be displayed to the user during compilation.
msg
- the message for the errorexpr
- the expression which is the root cause of the errorAllows 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 with TypeCheckingMode.SKIP.
call
- a method call, either a MethodCallExpression, StaticMethodCallExpression, or ConstructorCallExpressionAllows 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 with TypeCheckingMode.SKIP.
node
- a class nodeAllows 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 with TypeCheckingMode.SKIP.
node
- a method nodeAllows 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 with TypeCheckingMode.SKIP.
call
- a method call, either a MethodCallExpression, StaticMethodCallExpression, or ConstructorCallExpressionAllows 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 with TypeCheckingMode.SKIP.
node
- a class nodeAllows 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 with TypeCheckingMode.SKIP.
node
- a method nodeBuilds a parametrized class node for List, to represent List<X>
componentType
- the classnode for the component type of the listBuilds a parametrized class node representing the Map<keyType,valueType> type.
keyType
- the classnode type of the keyvalueType
- the classnode type of the value Given a method call, first checks that it's a static method call, and if it is, returns the
class node for the receiver. For example, with the following code:
Person.findAll { ... }, it would return the class node for Person.
If it's not a static method call, returns null.
call
- a method callSubclasses 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.
Returns the inferred type of an expression. Delegates to the type checker implementation.
exp
- the expression for which we want to find the inferred typeThis 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.
nodes
- the list of ambiguous methodsorigin
- the expression which originated the method selection processThis 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.
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 callboolean
false if the extension does not handle this assignment, true otherwiseAllows 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.
returnStatement
- the statement that triggered the errorinferredReturnType
- the inferred return type for this statementThis 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.
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 neededThis 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.
aexp
- the unresolved attributeboolean
false if this extension doesn't resolve the attribute, true
if it resolves the attribute.This 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.
pexp
- the unresolved propertyboolean
false if this extension doesn't resolve the property, true
if it resolves the property.This 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.
vexp
- the unresolved variable extensionboolean
false if the extension doesn't handle it,
true if the extension handles this variable.Given a method call, checks if it's a static method call and if it is, tells if the receiver matches the one supplied as an argument.
call
- a method callreceiver
- a class nodeLookup a ClassNode by its name from the source unit
type
- the name of the class whose ClassNode we want to lookupAllows 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.
expression
- the expression for which a corresponding method has been foundtarget
- the method which has been chosen by the type checkerSubclasses should implement this method whenever they need to perform special checks before the type checker starts working.
Stores an inferred type for an expression. Delegates to the type checker.
exp
- the expression for which we want to store an inferred typecn
- the type of the expressionCopyright © 2003-2021 The Apache Software Foundation. All rights reserved.