|
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
|
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)
|
static java.lang.String
|
extractPropertyNameFromMethodName(java.lang.String prefix, java.lang.String methodName)
Stores the inferred return type of a closure or a method. |
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)
Given a method name and a prefix, returns the name of the property that should be looked up, following the java beans rules. |
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
|
getInferredReturnType(ASTNode exp)
|
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 StaticTypeCheckingVisitor.SetterInfo
|
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 boolean
|
isSkippedInnerClass(AnnotatedNode node)
Test if a node is an inner class node, and if it is, then checks if the enclosing method is skipped. |
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 ClassNode
|
storeInferredReturnType(ASTNode node, ClassNode type)
If a method call returns a parameterized type, then we can perform additional inference on the return type, so that the type gets actual type parameters. |
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
|
visitCaseStatement(CaseStatement statement)
|
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)
Given a loop collection type, returns the inferred type of the loop element. |
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
|
visitSwitch(SwitchStatement statement)
|
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)
|
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
public static final Statement GENERATED_EMPTY_STATEMENT
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 LINKEDHASHMAP_CLASSNODE
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)
@java.lang.Override 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)
public static java.lang.String extractPropertyNameFromMethodName(java.lang.String prefix, java.lang.String methodName)
node
- a ClosureExpression or a MethodNodetype
- the inferred return type of the code
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)
prefix
- the method name prefix ("get", "is", "set", ...)methodName
- the method 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 getInferredReturnType(ASTNode exp)
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 StaticTypeCheckingVisitor.SetterInfo 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 boolean isSkippedInnerClass(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 ClassNode storeInferredReturnType(ASTNode node, ClassNode type)
method
- the method nodearguments
- the method call arguments
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)
@java.lang.OverridestoreType(expression, ClassHelper.make(IntRange.class)); public void visitAttributeExpression(AttributeExpression expression)
@java.lang.OverrideMethodCallExpression call = new MethodCallExpression( public void visitBinaryExpression(BinaryExpression expression)
@java.lang.Overridereturn; public void visitBitwiseNegationExpression(BitwiseNegationExpression expression)
@java.lang.Override// in that case, the INFERRED_TYPE corresponds to the current inferred type, while public void visitCaseStatement(CaseStatement statement)
@java.lang.Overrideif (expr.getRightExpression() instanceof ListExpression) { public void visitCastExpression(CastExpression expression)
@java.lang.Overrideextension.afterVisitClass(node); public void visitClass(ClassNode node)
@java.lang.Override public void visitClassExpression(ClassExpression expression)
@java.lang.OverridetypesBeforeVisit.put(ve, metadata); public void visitClosureExpression(ClosureExpression expression)
@java.lang.OverridetypeCheckingContext.isInStaticContext = false; public void visitConstructorCallExpression(ConstructorCallExpression call)
@java.lang.Overrideif (node != null) { protected void visitConstructorOrMethod(MethodNode node, boolean isConstructor)
@java.lang.OverridecomponentType = genericsTypes[0].getType(); public void visitField(FieldNode node)
collectionType
- the type of the collection
@java.lang.Override public void visitForLoop(ForStatement forLoop)
@java.lang.Overridefinal ClassNode typeOfTrue = getType(trueExpression); public void visitIfElse(IfStatement ifElse)
@java.lang.Override public void visitMethod(MethodNode node)
protected void visitMethodCallArguments(ArgumentListExpression arguments, boolean visitClosures, MethodNode selectedMethod)
@java.lang.Override// we can get actual parameters directly public void visitMethodCallExpression(MethodCallExpression call)
@java.lang.Override} public void visitPostfixExpression(PostfixExpression expression)
@java.lang.Overrideif (!isSkipMode(node) && !shouldSkipMethodNode(node)) { public void visitPrefixExpression(PrefixExpression expression)
@java.lang.Override public void visitPropertyExpression(PropertyExpression pexp)
@java.lang.Overridetry { public void visitRangeExpression(RangeExpression expression)
@java.lang.Override} public void visitReturnStatement(ReturnStatement statement)
@java.lang.Override} public void visitStaticMethodCallExpression(StaticMethodCallExpression call)
@java.lang.OverridestoreType(exp, getOriginalDeclarationType(exp)); public void visitSwitch(SwitchStatement statement)
@java.lang.Override} public void visitTernaryExpression(TernaryExpression expression)
@java.lang.Override} public void visitTryCatchFinally(TryCatchStatement statement)
@java.lang.Override} else { public void visitUnaryMinusExpression(UnaryMinusExpression expression)
@java.lang.OverrideClassNode typeRe = type.redirect(); public void visitUnaryPlusExpression(UnaryPlusExpression expression)
@java.lang.OverrideClassNode clazzType = CLASS_Type.getPlainNodeReference(); public void visitVariableExpression(VariableExpression vexp)
@java.lang.OverridestoreTargetMethod(origin, node); public void visitWhileLoop(WhileStatement loop)
protected static ClassNode wrapTypeIfNecessary(ClassNode type)
Groovy Documentation