|
Groovy Documentation | |||||||
FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.codehaus.groovy.ast.CodeVisitorSupport
org.codehaus.groovy.ast.ClassCodeVisitorSupport
org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor
public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport
The main class code visitor responsible for static type checking. It will perform various inspections like checking assignment types, type inference, ... Eventually, class nodes may be annotated with inferred type information.
Nested Class Summary | |
---|---|
static class |
StaticTypeCheckingVisitor.SignatureCodecFactory
|
protected class |
StaticTypeCheckingVisitor.VariableExpressionTypeMemoizer
|
Field Summary | |
---|---|
static MethodNode |
CLOSURE_CALL_NO_ARG
|
static MethodNode |
CLOSURE_CALL_ONE_ARG
|
static MethodNode |
CLOSURE_CALL_VARGS
|
protected static Expression |
CURRENT_SIGNATURE_PROTOCOL
|
protected static int |
CURRENT_SIGNATURE_PROTOCOL_VERSION
|
protected static ClassNode |
DELEGATES_TO
|
protected static ClassNode |
DELEGATES_TO_TARGET
|
protected static ClassNode |
DGM_CLASSNODE
|
protected static java.util.List |
EMPTY_METHODNODE_LIST
|
protected static java.lang.Object |
ERROR_COLLECTOR
|
protected static MethodNode |
GET_DELEGATE
|
protected static MethodNode |
GET_OWNER
|
protected static MethodNode |
GET_THISOBJECT
|
protected static ClassNode |
ITERABLE_TYPE
|
protected static ClassNode |
TYPECHECKED_CLASSNODE
|
protected static ClassNode[] |
TYPECHECKING_ANNOTATIONS
|
protected static ClassNode |
TYPECHECKING_INFO_NODE
|
protected DefaultTypeCheckingExtension |
extension
|
protected ReturnAdder |
returnAdder
|
protected ReturnStatementListener |
returnListener
|
protected TypeCheckingContext |
typeCheckingContext
|
Constructor Summary | |
StaticTypeCheckingVisitor(SourceUnit source, ClassNode cn)
|
Method Summary | |
---|---|
protected void
|
addAmbiguousErrorMessage(java.util.List foundMethods, java.lang.String name, ClassNode[] args, Expression expr)
|
protected void
|
addAssignmentError(ClassNode leftType, ClassNode rightType, Expression assignmentExpression)
|
protected void
|
addCategoryMethodCallError(Expression call)
|
protected void
|
addClosureReturnType(ClassNode returnType)
|
protected void
|
addError(java.lang.String msg, ASTNode expr)
|
protected void
|
addNoMatchingMethodError(ClassNode receiver, java.lang.String name, ClassNode[] args, Expression call)
|
protected void
|
addReceivers(java.util.List receivers, java.util.Collection owners, boolean implicitThis)
|
protected void
|
addStaticTypeError(java.lang.String msg, ASTNode expr)
|
void
|
addTypeCheckingExtension(TypeCheckingExtension extension)
|
protected void
|
addTypeCheckingInfoAnnotation(MethodNode node)
|
protected void
|
addUnsupportedPreOrPostfixExpressionError(Expression expression)
|
protected boolean
|
areCategoryMethodCalls(java.util.List foundMethods, java.lang.String name, ClassNode[] args)
|
protected boolean
|
checkCast(ClassNode targetType, Expression source)
|
protected void
|
checkClosureParameters(Expression callArguments, ClassNode receiver)
visit a method call target, to infer the type. |
protected void
|
checkForbiddenSpreadArgument(ArgumentListExpression argumentList)
|
protected void
|
checkGroovyConstructorMap(Expression receiver, ClassNode receiverType, MapExpression mapExpression)
Checks that a constructor style expression is valid regarding the number of arguments and the argument types. |
protected void
|
checkGroovyStyleConstructor(ClassNode node, ClassNode[] arguments)
Checks whether a property exists on the receiver, or on any of the possible receiver classes (found in the temporary type information table) |
protected ClassNode
|
checkReturnType(ReturnStatement statement)
|
protected void
|
collectAllInterfaceMethodsByName(ClassNode receiver, java.lang.String name, java.util.List methods)
|
protected boolean
|
existsProperty(PropertyExpression pexp, boolean checkForReadOnly)
|
protected boolean
|
existsProperty(PropertyExpression pexp, boolean checkForReadOnly, ClassCodeVisitorSupport visitor)
|
protected java.lang.Object
|
extractTemporaryTypeInfoKey(Expression expression)
|
protected static ClassNode[]
|
extractTypesFromParameters(Parameter[] parameters)
|
protected ClassNode
|
findCurrentInstanceOfClass(Expression expr, ClassNode type)
|
protected java.util.List
|
findMethod(ClassNode receiver, java.lang.String name, ClassNode... args)
|
protected MethodNode
|
findMethodOrFail(Expression expr, ClassNode receiver, java.lang.String name, ClassNode... args)
|
protected java.util.List
|
findMethodsWithGenerated(ClassNode receiver, java.lang.String name)
|
protected static java.lang.String
|
formatArgumentList(ClassNode[] nodes)
Returns a wrapped type if, and only if, the provided class node is a primitive type. |
protected static GenericsType
|
fullyResolve(GenericsType gt, java.util.Map placeholders)
|
protected static ClassNode
|
fullyResolveType(ClassNode type, java.util.Map placeholders)
|
protected ClassNode[]
|
getArgumentTypes(ArgumentListExpression args)
|
protected DelegationMetadata
|
getDelegationMetadata(ClosureExpression expression)
|
protected static ClassNode
|
getGroupOperationResultType(ClassNode a, ClassNode b)
This method returns the list of methods named against the supplied parameter that are defined on the specified receiver, but it will also add "non existing" methods that will be generated afterwards by the compiler, for example if a method is using default values and that the specified class node isn't compiled yet. |
protected ClassNode
|
getInferredReturnTypeFromWithClosureArgument(Expression callArguments)
In the case of a Object.with { ... } call, this method is supposed to retrieve the inferred closure return type. |
protected ClassNode
|
getOriginalDeclarationType(Expression lhs)
|
protected ClassNode
|
getResultType(ClassNode left, int op, ClassNode right, BinaryExpression expr)
|
protected SourceUnit
|
getSourceUnit()
|
protected java.util.List
|
getTemporaryTypesForExpression(Expression objectExpression)
|
protected ClassNode
|
getType(ASTNode exp)
|
protected ClassNode[]
|
getTypeCheckingAnnotations()
|
TypeCheckingContext
|
getTypeCheckingContext()
Returns the current type checking context. |
protected static boolean
|
hasRHSIncompleteGenericTypeInfo(ClassNode inferredRightExpressionType)
A helper method which determines which receiver class should be used in error messages when a field or attribute is not found. |
protected boolean
|
hasSetter(PropertyExpression pexp)
|
protected ClassNode
|
inferComponentType(ClassNode containerType, ClassNode indexType)
|
protected void
|
inferDiamondType(ConstructorCallExpression cce, ClassNode lType)
|
protected ClassNode
|
inferListExpressionType(ListExpression list)
|
static ClassNode
|
inferLoopElementType(ClassNode collectionType)
|
protected ClassNode
|
inferMapExpressionType(MapExpression map)
|
protected ClassNode
|
inferReturnTypeGenerics(ClassNode receiver, MethodNode method, Expression arguments)
Checks that the parameterized generics of an argument are compatible with the generics of the parameter. |
void
|
initialize()
|
protected static boolean
|
isClassInnerClassOrEqualTo(ClassNode toBeChecked, ClassNode start)
|
protected boolean
|
isClosureCall(java.lang.String name, Expression objectExpression, Expression arguments)
|
protected static boolean
|
isNullConstant(Expression expression)
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 boolean
|
isSecondPassNeededForControlStructure(java.util.Map varOrigType, java.util.Map oldTracker)
|
boolean
|
isSkipMode(AnnotatedNode node)
|
protected java.util.List
|
makeOwnerList(Expression objectExpression)
|
void
|
performSecondPass()
|
protected void
|
pickInferredTypeFromMethodAnnotation(MethodNode node)
|
protected java.util.Map
|
popAssignmentTracking(java.util.Map oldTracker)
|
protected static java.lang.String
|
prettyPrintMethodList(java.util.List nodes)
|
protected java.util.Map
|
pushAssignmentTracking()
|
protected void
|
pushInstanceOfTypeInfo(Expression objectOfInstanceOf, Expression typeExpression)
Stores information about types when [objectOfInstanceof instanceof typeExpression] is visited |
protected void
|
restoreVariableExpressionMetadata(java.util.Map typesBeforeVisit)
|
protected void
|
saveVariableExpressionMetadata(java.util.Set closureSharedExpressions, java.util.Map typesBeforeVisit)
|
void
|
setMethodsToBeVisited(java.util.Set methodsToBeVisited)
|
protected boolean
|
shouldSkipClassNode(ClassNode node)
|
protected boolean
|
shouldSkipMethodNode(MethodNode node)
|
protected void
|
silentlyVisitMethodNode(MethodNode directMethodCallCandidate)
|
protected void
|
startMethodInference(MethodNode node, ErrorCollector collector)
|
protected void
|
storeInferredTypeForPropertyExpression(PropertyExpression pexp, ClassNode flatInferredType)
|
protected void
|
storeTargetMethod(Expression call, MethodNode directMethodCallCandidate)
|
protected void
|
storeType(Expression exp, ClassNode cn)
|
protected void
|
typeCheckAssignment(BinaryExpression assignmentExpression, Expression leftExpression, ClassNode leftExpressionType, Expression rightExpression, ClassNode inferredRightExpressionType)
|
protected void
|
typeCheckClosureCall(Expression callArguments, ClassNode[] args, Parameter[] parameters)
|
protected MethodNode
|
typeCheckMapConstructor(ConstructorCallExpression call, ClassNode receiver, Expression arguments)
|
protected boolean
|
typeCheckMethodArgumentWithGenerics(ClassNode parameterType, ClassNode argumentType, boolean lastArg)
|
protected void
|
typeCheckMethodsWithGenerics(ClassNode receiver, ClassNode[] arguments, MethodNode candidateMethod, Expression location)
|
void
|
visitAttributeExpression(AttributeExpression expression)
|
void
|
visitBinaryExpression(BinaryExpression expression)
|
void
|
visitBitwiseNegationExpression(BitwiseNegationExpression expression)
|
void
|
visitCastExpression(CastExpression expression)
|
void
|
visitClass(ClassNode node)
|
void
|
visitClassExpression(ClassExpression expression)
|
void
|
visitClosureExpression(ClosureExpression expression)
|
void
|
visitConstructorCallExpression(ConstructorCallExpression call)
|
protected void
|
visitConstructorOrMethod(MethodNode node, boolean isConstructor)
|
void
|
visitField(FieldNode node)
|
void
|
visitForLoop(ForStatement forLoop)
|
void
|
visitIfElse(IfStatement ifElse)
|
void
|
visitMethod(MethodNode node)
|
protected void
|
visitMethodCallArguments(ArgumentListExpression arguments, boolean visitClosures, MethodNode selectedMethod)
|
void
|
visitMethodCallExpression(MethodCallExpression call)
|
void
|
visitPostfixExpression(PostfixExpression expression)
|
void
|
visitPrefixExpression(PrefixExpression expression)
|
void
|
visitPropertyExpression(PropertyExpression pexp)
|
void
|
visitRangeExpression(RangeExpression expression)
|
void
|
visitReturnStatement(ReturnStatement statement)
|
void
|
visitStaticMethodCallExpression(StaticMethodCallExpression call)
|
void
|
visitTernaryExpression(TernaryExpression expression)
|
void
|
visitTryCatchFinally(TryCatchStatement statement)
|
void
|
visitUnaryMinusExpression(UnaryMinusExpression expression)
|
void
|
visitUnaryPlusExpression(UnaryPlusExpression expression)
|
void
|
visitVariableExpression(VariableExpression vexp)
|
void
|
visitWhileLoop(WhileStatement loop)
|
protected static ClassNode
|
wrapTypeIfNecessary(ClassNode type)
|
Methods inherited from class java.lang.Object | |
---|---|
java.lang.Object#wait(long, int), java.lang.Object#wait(long), java.lang.Object#wait(), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll() |
Field Detail |
---|
public static final MethodNode CLOSURE_CALL_NO_ARG
public static final MethodNode CLOSURE_CALL_ONE_ARG
public static final MethodNode CLOSURE_CALL_VARGS
protected static final Expression CURRENT_SIGNATURE_PROTOCOL
protected static final int CURRENT_SIGNATURE_PROTOCOL_VERSION
protected static final ClassNode DELEGATES_TO
protected static final ClassNode DELEGATES_TO_TARGET
protected static final ClassNode DGM_CLASSNODE
protected static final java.util.List EMPTY_METHODNODE_LIST
protected static final java.lang.Object ERROR_COLLECTOR
protected static final MethodNode GET_DELEGATE
protected static final MethodNode GET_OWNER
protected static final MethodNode GET_THISOBJECT
protected static final ClassNode ITERABLE_TYPE
protected static final ClassNode TYPECHECKED_CLASSNODE
protected static final ClassNode[] TYPECHECKING_ANNOTATIONS
protected static final ClassNode TYPECHECKING_INFO_NODE
protected DefaultTypeCheckingExtension extension
protected final ReturnAdder returnAdder
protected final ReturnStatementListener returnListener
protected TypeCheckingContext typeCheckingContext
Constructor Detail |
---|
public StaticTypeCheckingVisitor(SourceUnit source, ClassNode cn)
Method Detail |
---|
protected void addAmbiguousErrorMessage(java.util.List foundMethods, java.lang.String name, ClassNode[] args, Expression expr)
protected void addAssignmentError(ClassNode leftType, ClassNode rightType, Expression assignmentExpression)
protected void addCategoryMethodCallError(Expression call)
protected void addClosureReturnType(ClassNode returnType)
protected void addError(java.lang.String msg, ASTNode expr)
protected void addNoMatchingMethodError(ClassNode receiver, java.lang.String name, ClassNode[] args, Expression call)
protected void addReceivers(java.util.List receivers, java.util.Collection owners, boolean implicitThis)
protected void addStaticTypeError(java.lang.String msg, ASTNode expr)
public void addTypeCheckingExtension(TypeCheckingExtension extension)
protected void addTypeCheckingInfoAnnotation(MethodNode node)
protected void addUnsupportedPreOrPostfixExpressionError(Expression expression)
protected boolean areCategoryMethodCalls(java.util.List foundMethods, java.lang.String name, ClassNode[] args)
protected boolean checkCast(ClassNode targetType, Expression source)
protected void checkClosureParameters(Expression callArguments, ClassNode receiver)
protected void checkForbiddenSpreadArgument(ArgumentListExpression argumentList)
protected void checkGroovyConstructorMap(Expression receiver, ClassNode receiverType, MapExpression mapExpression)
node
- the class node for which we will try to find a matching constructorarguments
- the constructor arguments
protected void checkGroovyStyleConstructor(ClassNode node, ClassNode[] arguments)
pexp
- a property expressioncheckForReadOnly
- also lookup for read only propertiesvisitor
- if not null, when the property node is found, visit it with the provided visitor
protected ClassNode checkReturnType(ReturnStatement statement)
protected void collectAllInterfaceMethodsByName(ClassNode receiver, java.lang.String name, java.util.List methods)
protected boolean existsProperty(PropertyExpression pexp, boolean checkForReadOnly)
protected boolean existsProperty(PropertyExpression pexp, boolean checkForReadOnly, ClassCodeVisitorSupport visitor)
protected java.lang.Object extractTemporaryTypeInfoKey(Expression expression)
protected static ClassNode[] extractTypesFromParameters(Parameter[] parameters)
protected ClassNode findCurrentInstanceOfClass(Expression expr, ClassNode type)
protected java.util.List findMethod(ClassNode receiver, java.lang.String name, ClassNode... args)
protected MethodNode findMethodOrFail(Expression expr, ClassNode receiver, java.lang.String name, ClassNode... args)
protected java.util.List findMethodsWithGenerated(ClassNode receiver, java.lang.String name)
protected static java.lang.String formatArgumentList(ClassNode[] nodes)
protected static GenericsType fullyResolve(GenericsType gt, java.util.Map placeholders)
protected static ClassNode fullyResolveType(ClassNode type, java.util.Map placeholders)
protected ClassNode[] getArgumentTypes(ArgumentListExpression args)
protected DelegationMetadata getDelegationMetadata(ClosureExpression expression)
protected static ClassNode getGroupOperationResultType(ClassNode a, ClassNode b)
receiver
- the receiver where to find methodsname
- the name of the methods to return
protected ClassNode getInferredReturnTypeFromWithClosureArgument(Expression callArguments)
callArguments
- the argument list from the Object#with(Closure) call, ie. a single closure expression
protected ClassNode getOriginalDeclarationType(Expression lhs)
protected ClassNode getResultType(ClassNode left, int op, ClassNode right, BinaryExpression expr)
protected SourceUnit getSourceUnit()
protected java.util.List getTemporaryTypesForExpression(Expression objectExpression)
protected ClassNode getType(ASTNode exp)
protected ClassNode[] getTypeCheckingAnnotations()
public TypeCheckingContext getTypeCheckingContext()
protected static boolean hasRHSIncompleteGenericTypeInfo(ClassNode inferredRightExpressionType)
expr
- the expression for which an unknown field has been foundtype
- the type of the expression (used as fallback type)
protected boolean hasSetter(PropertyExpression pexp)
protected ClassNode inferComponentType(ClassNode containerType, ClassNode indexType)
protected void inferDiamondType(ConstructorCallExpression cce, ClassNode lType)
protected ClassNode inferListExpressionType(ListExpression list)
public static ClassNode inferLoopElementType(ClassNode collectionType)
protected ClassNode inferMapExpressionType(MapExpression map)
protected ClassNode inferReturnTypeGenerics(ClassNode receiver, MethodNode method, Expression arguments)
parameterType
- the parameter type of a methodargumentType
- the type of the argument passed to the method
public void initialize()
protected static boolean isClassInnerClassOrEqualTo(ClassNode toBeChecked, ClassNode start)
protected boolean isClosureCall(java.lang.String name, Expression objectExpression, Expression arguments)
protected static boolean isNullConstant(Expression expression)
protected boolean isSecondPassNeededForControlStructure(java.util.Map varOrigType, java.util.Map oldTracker)
public boolean isSkipMode(AnnotatedNode node)
protected java.util.List makeOwnerList(Expression objectExpression)
public void performSecondPass()
protected void pickInferredTypeFromMethodAnnotation(MethodNode node)
protected java.util.Map popAssignmentTracking(java.util.Map oldTracker)
protected static java.lang.String prettyPrintMethodList(java.util.List nodes)
protected java.util.Map pushAssignmentTracking()
protected void pushInstanceOfTypeInfo(Expression objectOfInstanceOf, Expression typeExpression)
objectOfInstanceOf
- the expression which must be checked against instanceoftypeExpression
- the expression which represents the target type
protected void restoreVariableExpressionMetadata(java.util.Map typesBeforeVisit)
protected void saveVariableExpressionMetadata(java.util.Set closureSharedExpressions, java.util.Map typesBeforeVisit)
public void setMethodsToBeVisited(java.util.Set methodsToBeVisited)
protected boolean shouldSkipClassNode(ClassNode node)
protected boolean shouldSkipMethodNode(MethodNode node)
protected void silentlyVisitMethodNode(MethodNode directMethodCallCandidate)
protected void startMethodInference(MethodNode node, ErrorCollector collector)
protected void storeInferredTypeForPropertyExpression(PropertyExpression pexp, ClassNode flatInferredType)
protected void storeTargetMethod(Expression call, MethodNode directMethodCallCandidate)
protected void storeType(Expression exp, ClassNode cn)
protected void typeCheckAssignment(BinaryExpression assignmentExpression, Expression leftExpression, ClassNode leftExpressionType, Expression rightExpression, ClassNode inferredRightExpressionType)
protected void typeCheckClosureCall(Expression callArguments, ClassNode[] args, Parameter[] parameters)
protected MethodNode typeCheckMapConstructor(ConstructorCallExpression call, ClassNode receiver, Expression arguments)
protected boolean typeCheckMethodArgumentWithGenerics(ClassNode parameterType, ClassNode argumentType, boolean lastArg)
protected void typeCheckMethodsWithGenerics(ClassNode receiver, ClassNode[] arguments, MethodNode candidateMethod, Expression location)
ClassNode toType = getWrapper(getType(expression.getTo())); public void visitAttributeExpression(AttributeExpression expression)
int op = expression.getOperation().getType(); public void visitBinaryExpression(BinaryExpression expression)
String name = type == PLUS_PLUS ? "next" : type == MINUS_MINUS ? "previous" : null; public void visitBitwiseNegationExpression(BitwiseNegationExpression expression)
ClassNode leftRedirect = left.redirect(); public void visitCastExpression(CastExpression expression)
extension.afterVisitClass(node); public void visitClass(ClassNode node)
public void visitClassExpression(ClassExpression expression)
Variable accessedVariable = ve.getAccessedVariable(); public void visitClosureExpression(ClosureExpression expression)
public void visitConstructorCallExpression(ConstructorCallExpression call)
// bean-style constructor protected void visitConstructorOrMethod(MethodNode node, boolean isConstructor)
*/ public void visitField(FieldNode node)
return true; public void visitForLoop(ForStatement forLoop)
protected void storeType(Expression exp, ClassNode cn) { public void visitIfElse(IfStatement ifElse)
if (extension.beforeMethodCall(call)) { public void visitMethod(MethodNode node)
protected void visitMethodCallArguments(ArgumentListExpression arguments, boolean visitClosures, MethodNode selectedMethod)
} public void visitMethodCallExpression(MethodCallExpression call)
} public void visitPostfixExpression(PostfixExpression expression)
&& !type.equals(void_WRAPPER_TYPE) public void visitPrefixExpression(PrefixExpression expression)
Expression objectExpression = pexp.getObjectExpression(); public void visitPropertyExpression(PropertyExpression pexp)
BinaryExpression enclosingBinaryExpression = typeCheckingContext.getEnclosingBinaryExpression(); public void visitRangeExpression(RangeExpression expression)
} public void visitReturnStatement(ReturnStatement statement)
} public void visitStaticMethodCallExpression(StaticMethodCallExpression call)
else if (isNumberType(leftRedirect) && isNumberType(rightRedirect)) { public void visitTernaryExpression(TernaryExpression expression)
if (isNumberCategory(getWrapper(leftRedirect)) && isNumberCategory(getWrapper(rightRedirect))) { public void visitTryCatchFinally(TryCatchStatement statement)
} else { public void visitUnaryMinusExpression(UnaryMinusExpression expression)
ClassNode typeRe = type.redirect(); public void visitUnaryPlusExpression(UnaryPlusExpression expression)
if (vexp.getName().equals("owner") public void visitVariableExpression(VariableExpression vexp)
public void visitWhileLoop(WhileStatement loop)
protected static ClassNode wrapTypeIfNecessary(ClassNode type)
Groovy Documentation