Class FunctionType
- java.lang.Object
-
- com.google.javascript.rhino.jstype.JSType
-
- com.google.javascript.rhino.jstype.ObjectType
-
- com.google.javascript.rhino.jstype.FunctionType
-
- All Implemented Interfaces:
StaticScope<JSType>,java.io.Serializable
- Direct Known Subclasses:
NoObjectType
public class FunctionType extends ObjectType
This derived type provides extended information about a function, including its return type and argument types.Note: the parameters list is the LP node that is the parent of the actual NAME node containing the parsed argument list (annotated with JSDOC_TYPE_PROP's for the compile-time type of each argument.
- See Also:
- Serialized Form
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.google.javascript.rhino.jstype.JSType
JSType.TypePair
-
-
Field Summary
-
Fields inherited from class com.google.javascript.rhino.jstype.JSType
EMPTY_TYPE_COMPONENT, ENUMDECL, NOT_A_CLASS, NOT_A_TYPE, NOT_ENUMDECL, templateTypeMap, UNKNOWN_NAME
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description booleancanBeCalled()This predicate is used to test whether a given type can be used as the 'function' in a function call.voidclearCachedValues()Clear cached values.FunctionTypecloneWithoutArrowType()Create a new constructor with the parameters and return type stripped.java.lang.Iterable<ObjectType>getAllExtendedInterfaces()Returns all extended interfaces declared by an interfaces or its super- interfaces.java.lang.Iterable<ObjectType>getAllImplementedInterfaces()Returns all interfaces implemented by a class or its superclass and any superclasses for any of those interfaces.FunctionTypegetBindReturnType(int argsToBind)Get the return value of calling "bind" on this function with the specified number of arguments.FunctionTypegetConstructor()Gets this object's constructor.java.lang.Iterable<ObjectType>getCtorExtendedInterfaces()Gets the interfaces extended by the interface associated with this type.java.lang.Iterable<ObjectType>getCtorImplementedInterfaces()Gets the interfaces implemented by the ctor associated with this type.java.lang.Iterable<ObjectType>getExtendedInterfaces()Returns interfaces directly extended by an interfaceintgetExtendedInterfacesCount()Returns the number of interfaces directly extended by an interfacejava.lang.Iterable<ObjectType>getImplementedInterfaces()Returns interfaces implemented directly by a class or its superclass.ObjectTypegetImplicitPrototype()Gets the implicit prototype (a.k.a.ObjectTypegetInstanceType()Gets the type of instance of this function.intgetMaxArguments()Gets the maximum number of arguments that this function requires, or Integer.MAX_VALUE if this is a variable argument function.intgetMinArguments()Gets the minimum number of arguments that this function requires.FunctionTypegetOwnerFunction()Gets the owner of this if it's a function prototype.java.lang.Iterable<ObjectType>getOwnImplementedInterfaces()Returns interfaces directly implemented by the class.java.util.Set<java.lang.String>getOwnPropertyNames()Includes the prototype iff someone has created it.java.lang.Iterable<Node>getParameters()NodegetParametersNode()Gets an LP node that contains all params.JSTypegetPropertyType(java.lang.String name)Gets the property type of the property whose name is given.ObjectTypegetPrototype()Gets theprototypeproperty of this function type.java.lang.StringgetReferenceName()Gets the reference name for this object.JSTypegetReturnType()PropertygetSlot(java.lang.String name)Default getSlot implementation.NodegetSource()Gets the source node or null if this is an unknown function.java.util.List<FunctionType>getSubTypes()Returns a list of types that are subtypes of this type.FunctionTypegetSuperClassConstructor()Given a constructor or an interface type, get its superclass constructor ornullif none exists.static ObjectTypegetTopDefiningInterface(ObjectType type, java.lang.String propertyName)Given an interface and a property, finds the top-most super interface that has the property defined (including this interface).ObjectTypegetTopMostDefiningType(java.lang.String propertyName)Given a constructor or an interface type and a property, finds the top-most superclass that has the property defined (including this constructor).JSTypegetTypeOfThis()Gets the type ofthisin this function.booleanhasAnyTemplateTypesInternal()booleanhasCachedValues()Returns true if any cached values have been set for this type.booleanhasEqualCallType(FunctionType otherType)inthashCode()booleanhasImplementedInterfaces()booleanhasInstanceType()Returns whether this function type has an instance type.booleanhasReferenceName()Returns true if the object is named.booleanisConstructor()Whether this type is aFunctionTypethat is a constructor or a named type that points to such a type.booleanisInstanceType()Whether this type is an Instance object of some constructor.booleanisInterface()Whether this type is aFunctionTypethat is an interface or a named type that points to such a type.booleanisNativeObjectType()Whether this is a built-in object.booleanisOrdinaryFunction()Whether this type is aFunctionTypethat is an ordinary function or a named type that points to such a type.booleanisReturnTypeInferred()booleanisSubtype(JSType that)A function is a subtype of another if their call methods are related via subtyping andthisis a subtype ofthatwith regard to the prototype chain.booleanmakesDicts()When a class B inherits from A and A is annotated as a dict, then B automatically gets the annotation, even if B's constructor is not explicitly annotated.booleanmakesStructs()When a class B inherits from A and A is annotated as a struct, then B automatically gets the annotation, even if B's constructor is not explicitly annotated.voidmatchConstraint(JSType constraint)Modify this type so that it matches the specified type.booleanmatchesNumberContext()This predicate is used to test whether a given type can appear in a numeric context, such as an operand of a multiply operator.booleanmatchesObjectContext()This predicate is used to test whether a given type can appear in anObjectcontext, such as the expression in a with statement.booleanmatchesStringContext()This predicate is used to test whether a given type can appear in aStringcontext, such as an operand of a string concat (+) operator.voidmatchRecordTypeConstraint(ObjectType constraintObj)booleanremoveProperty(java.lang.String name)Removes the declared or inferred property from this ObjectType.voidsetDict()voidsetExtendedInterfaces(java.util.List<ObjectType> extendedInterfaces)voidsetImplementedInterfaces(java.util.List<ObjectType> implementedInterfaces)voidsetPropertyJSDocInfo(java.lang.String propertyName, JSDocInfo info)Sets the docInfo for the specified property from theJSDocInfoon its definition.voidsetPrototypeBasedOn(ObjectType baseType)Sets the prototype, creating the prototype object from the given base type.voidsetSource(Node source)Sets the source node.voidsetStruct()java.lang.StringtoDebugHashCodeString()A hash code function for diagnosing complicated issues around type-identity.FunctionTypetoMaybeFunctionType()Downcasts this to a FunctionType, or returns null if this is not a function.JSTypeunboxesTo()Turn an object type to its corresponding scalar type.<T> Tvisit(Visitor<T> visitor)Visit this type with the given visitor.-
Methods inherited from class com.google.javascript.rhino.jstype.ObjectType
cast, createDelegateSuffix, defineDeclaredProperty, defineInferredProperty, defineSynthesizedProperty, findPropertyType, getDisplayName, getJSDocInfo, getNormalizedReferenceName, getOwnPropertyJSDocInfo, getOwnSlot, getParentScope, getPossibleToBooleanOutcomes, getPropertiesCount, getPropertyNames, getPropertyNode, getRootNode, getTemplateTypes, hasOwnProperty, hasProperty, isFunctionPrototypeType, isObject, isPropertyInExterns, isPropertyTypeDeclared, isPropertyTypeInferred, isUnknownType, setJSDocInfo, testForEquality
-
Methods inherited from class com.google.javascript.rhino.jstype.JSType
autobox, autoboxesTo, canCastTo, canTestForEqualityWith, canTestForShallowEqualityWith, clearResolved, collapseUnion, dereference, differsFrom, equals, forceResolve, getGreatestSubtype, getLeastSupertype, getRestrictedTypeGivenToBooleanOutcome, getTemplateTypeMap, getTypesUnderEquality, getTypesUnderInequality, getTypesUnderShallowEquality, getTypesUnderShallowInequality, hasAnyTemplateTypes, hasDisplayName, isAllType, isArrayType, isBooleanObjectType, isBooleanValueType, isCheckedUnknownType, isDateType, isDict, isEmptyType, isEnumElementType, isEnumType, isEquivalent, isEquivalentTo, isFunctionType, isGlobalThisType, isInvariant, isNominalConstructor, isNominalType, isNoObjectType, isNoResolvedType, isNoType, isNullable, isNullType, isNumber, isNumberObjectType, isNumberValueType, isRecordType, isRegexpType, isResolved, isString, isStringObjectType, isStringValueType, isStruct, isTemplateType, isTemplatizedType, isUnionType, isVoidType, matchesInt32Context, matchesUint32Context, resolve, restrictByNotNullOrUndefined, setValidator, toAnnotationString, toMaybeEnumElementType, toMaybeEnumType, toMaybeFunctionType, toMaybeTemplateType, toMaybeTemplateType, toMaybeTemplatizedType, toMaybeTemplatizedType, toMaybeUnionType, toObjectType, toString
-
-
-
-
Method Detail
-
isInstanceType
public boolean isInstanceType()
Description copied from class:JSTypeWhether this type is an Instance object of some constructor. Does not necessarily mean this is anInstanceObjectType.- Overrides:
isInstanceTypein classJSType
-
isConstructor
public boolean isConstructor()
Description copied from class:JSTypeWhether this type is aFunctionTypethat is a constructor or a named type that points to such a type.- Overrides:
isConstructorin classJSType
-
isInterface
public boolean isInterface()
Description copied from class:JSTypeWhether this type is aFunctionTypethat is an interface or a named type that points to such a type.- Overrides:
isInterfacein classJSType
-
isOrdinaryFunction
public boolean isOrdinaryFunction()
Description copied from class:JSTypeWhether this type is aFunctionTypethat is an ordinary function or a named type that points to such a type.- Overrides:
isOrdinaryFunctionin classJSType
-
makesStructs
public boolean makesStructs()
When a class B inherits from A and A is annotated as a struct, then B automatically gets the annotation, even if B's constructor is not explicitly annotated.
-
makesDicts
public boolean makesDicts()
When a class B inherits from A and A is annotated as a dict, then B automatically gets the annotation, even if B's constructor is not explicitly annotated.
-
setStruct
public void setStruct()
-
setDict
public void setDict()
-
toMaybeFunctionType
public FunctionType toMaybeFunctionType()
Description copied from class:JSTypeDowncasts this to a FunctionType, or returns null if this is not a function. For the purposes of this function, we define a MaybeFunctionType as any type in the sub-lattice { x | LEAST_FUNCTION_TYPE <= x <= GREATEST_FUNCTION_TYPE } This definition excludes bottom types like NoType and NoObjectType. This definition is somewhat arbitrary and axiomatic, but this is the definition that makes the most sense for the most callers.- Overrides:
toMaybeFunctionTypein classJSType
-
canBeCalled
public boolean canBeCalled()
Description copied from class:JSTypeThis predicate is used to test whether a given type can be used as the 'function' in a function call.- Returns:
trueif this type might be callable.
-
hasImplementedInterfaces
public boolean hasImplementedInterfaces()
-
getParameters
public java.lang.Iterable<Node> getParameters()
-
getParametersNode
public Node getParametersNode()
Gets an LP node that contains all params. May be null.
-
getMinArguments
public int getMinArguments()
Gets the minimum number of arguments that this function requires.
-
getMaxArguments
public int getMaxArguments()
Gets the maximum number of arguments that this function requires, or Integer.MAX_VALUE if this is a variable argument function.
-
getReturnType
public JSType getReturnType()
-
isReturnTypeInferred
public boolean isReturnTypeInferred()
-
getSlot
public Property getSlot(java.lang.String name)
Description copied from class:ObjectTypeDefault getSlot implementation. This gets overridden by FunctionType for lazily-resolved prototypes.- Specified by:
getSlotin interfaceStaticScope<JSType>- Overrides:
getSlotin classObjectType- Parameters:
name- The name of the variable slot to look up.- Returns:
- The defined slot for the variable, or
nullif no definition exists.
-
getOwnPropertyNames
public java.util.Set<java.lang.String> getOwnPropertyNames()
Includes the prototype iff someone has created it. We do not want to expose the prototype for ordinary functions.- Overrides:
getOwnPropertyNamesin classObjectType
-
getPrototype
public ObjectType getPrototype()
Gets theprototypeproperty of this function type. This is equivalent to(ObjectType) getPropertyType("prototype").
-
setPrototypeBasedOn
public void setPrototypeBasedOn(ObjectType baseType)
Sets the prototype, creating the prototype object from the given base type.- Parameters:
baseType- The base type.
-
getAllImplementedInterfaces
public java.lang.Iterable<ObjectType> getAllImplementedInterfaces()
Returns all interfaces implemented by a class or its superclass and any superclasses for any of those interfaces. If this is called before all types are resolved, it may return an incomplete set.
-
getImplementedInterfaces
public java.lang.Iterable<ObjectType> getImplementedInterfaces()
Returns interfaces implemented directly by a class or its superclass.
-
getOwnImplementedInterfaces
public java.lang.Iterable<ObjectType> getOwnImplementedInterfaces()
Returns interfaces directly implemented by the class.
-
setImplementedInterfaces
public void setImplementedInterfaces(java.util.List<ObjectType> implementedInterfaces)
-
getAllExtendedInterfaces
public java.lang.Iterable<ObjectType> getAllExtendedInterfaces()
Returns all extended interfaces declared by an interfaces or its super- interfaces. If this is called before all types are resolved, it may return an incomplete set.
-
getExtendedInterfaces
public java.lang.Iterable<ObjectType> getExtendedInterfaces()
Returns interfaces directly extended by an interface
-
getExtendedInterfacesCount
public int getExtendedInterfacesCount()
Returns the number of interfaces directly extended by an interface
-
setExtendedInterfaces
public void setExtendedInterfaces(java.util.List<ObjectType> extendedInterfaces) throws java.lang.UnsupportedOperationException
- Throws:
java.lang.UnsupportedOperationException
-
getPropertyType
public JSType getPropertyType(java.lang.String name)
Description copied from class:ObjectTypeGets the property type of the property whose name is given. If the underlying object does not have this property, the Unknown type is returned to indicate that no information is available on this property. This gets overridden by FunctionType for lazily-resolved call() and bind() functions.- Overrides:
getPropertyTypein classObjectType- Returns:
- the property's type or
UnknownType. This method never returnsnull.
-
getBindReturnType
public FunctionType getBindReturnType(int argsToBind)
Get the return value of calling "bind" on this function with the specified number of arguments. If -1 is passed, then we will return a result that accepts any parameters.
-
getSuperClassConstructor
public FunctionType getSuperClassConstructor()
Given a constructor or an interface type, get its superclass constructor ornullif none exists.
-
getTopDefiningInterface
public static ObjectType getTopDefiningInterface(ObjectType type, java.lang.String propertyName)
Given an interface and a property, finds the top-most super interface that has the property defined (including this interface).
-
getTopMostDefiningType
public ObjectType getTopMostDefiningType(java.lang.String propertyName)
Given a constructor or an interface type and a property, finds the top-most superclass that has the property defined (including this constructor).
-
hasEqualCallType
public boolean hasEqualCallType(FunctionType otherType)
-
isSubtype
public boolean isSubtype(JSType that)
A function is a subtype of another if their call methods are related via subtyping andthisis a subtype ofthatwith regard to the prototype chain.- Returns:
this <: that
-
visit
public <T> T visit(Visitor<T> visitor)
Description copied from class:JSTypeVisit this type with the given visitor.- Overrides:
visitin classObjectType- Returns:
- the value returned by the visitor
- See Also:
Visitor
-
getInstanceType
public ObjectType getInstanceType()
Gets the type of instance of this function.- Throws:
java.lang.IllegalStateException- if this function is not a constructor (seeisConstructor()).
-
hasInstanceType
public boolean hasInstanceType()
Returns whether this function type has an instance type.
-
getTypeOfThis
public JSType getTypeOfThis()
Gets the type ofthisin this function.- Specified by:
getTypeOfThisin interfaceStaticScope<JSType>- Overrides:
getTypeOfThisin classObjectType
-
getSource
public Node getSource()
Gets the source node or null if this is an unknown function.
-
setSource
public void setSource(Node source)
Sets the source node.
-
clearCachedValues
public void clearCachedValues()
Description copied from class:ObjectTypeClear cached values. Should be called before making changes to a prototype that may have been changed since creation.- Overrides:
clearCachedValuesin classObjectType
-
getSubTypes
public java.util.List<FunctionType> getSubTypes()
Returns a list of types that are subtypes of this type. This is only valid for constructor functions, and may be null. This allows a downward traversal of the subtype graph.
-
hasCachedValues
public boolean hasCachedValues()
Description copied from class:ObjectTypeReturns true if any cached values have been set for this type. If true, then the prototype chain should not be changed, as it might invalidate the cached values.
-
toDebugHashCodeString
public java.lang.String toDebugHashCodeString()
Description copied from class:JSTypeA hash code function for diagnosing complicated issues around type-identity.- Overrides:
toDebugHashCodeStringin classJSType
-
cloneWithoutArrowType
public FunctionType cloneWithoutArrowType()
Create a new constructor with the parameters and return type stripped.
-
hasAnyTemplateTypesInternal
public boolean hasAnyTemplateTypesInternal()
-
removeProperty
public boolean removeProperty(java.lang.String name)
Description copied from class:ObjectTypeRemoves the declared or inferred property from this ObjectType.- Overrides:
removePropertyin classObjectType- Parameters:
name- the property's name- Returns:
- true if the property was removed successfully. False if the property did not exist, or could not be removed.
-
setPropertyJSDocInfo
public void setPropertyJSDocInfo(java.lang.String propertyName, JSDocInfo info)Description copied from class:ObjectTypeSets the docInfo for the specified property from theJSDocInfoon its definition.- Overrides:
setPropertyJSDocInfoin classObjectTypeinfo-JSDocInfofor the property definition. May benull.
-
matchesNumberContext
public boolean matchesNumberContext()
Description copied from class:JSTypeThis predicate is used to test whether a given type can appear in a numeric context, such as an operand of a multiply operator.- Overrides:
matchesNumberContextin classJSType
-
matchesStringContext
public boolean matchesStringContext()
Description copied from class:JSTypeThis predicate is used to test whether a given type can appear in aStringcontext, such as an operand of a string concat (+) operator. All types have at least the potential for converting toString. When we add externally defined types, such as a browser OM, we may choose to add types that do not automatically convert toString.- Overrides:
matchesStringContextin classJSType
-
unboxesTo
public JSType unboxesTo()
Description copied from class:JSTypeTurn an object type to its corresponding scalar type.
-
matchesObjectContext
public boolean matchesObjectContext()
Description copied from class:JSTypeThis predicate is used to test whether a given type can appear in anObjectcontext, such as the expression in a with statement. Most types we will encounter, except notablynull, have at least the potential for converting toObject. Host defined objects can get peculiar.- Overrides:
matchesObjectContextin classJSType
-
getConstructor
public FunctionType getConstructor()
Description copied from class:ObjectTypeGets this object's constructor.- Specified by:
getConstructorin classObjectType- Returns:
- this object's constructor or
nullif it is a native object (constructed natively v.s. by instantiation of a function)
-
getImplicitPrototype
public ObjectType getImplicitPrototype()
Description copied from class:ObjectTypeGets the implicit prototype (a.k.a. the[[Prototype]]property).- Specified by:
getImplicitPrototypein classObjectType
-
getReferenceName
public java.lang.String getReferenceName()
Description copied from class:ObjectTypeGets the reference name for this object. This includes named types like constructors, prototypes, and enums. It notably does not include literal types like strings and booleans and structural types.- Specified by:
getReferenceNamein classObjectType- Returns:
- the object's name or
nullif this is an anonymous object
-
hasReferenceName
public boolean hasReferenceName()
Description copied from class:ObjectTypeReturns true if the object is named.- Overrides:
hasReferenceNamein classObjectType- Returns:
- true if the object is named, false if it is anonymous
-
isNativeObjectType
public boolean isNativeObjectType()
Whether this is a built-in object.- Overrides:
isNativeObjectTypein classObjectType
-
getOwnerFunction
public FunctionType getOwnerFunction()
Description copied from class:ObjectTypeGets the owner of this if it's a function prototype.- Overrides:
getOwnerFunctionin classObjectType
-
getCtorImplementedInterfaces
public java.lang.Iterable<ObjectType> getCtorImplementedInterfaces()
Description copied from class:ObjectTypeGets the interfaces implemented by the ctor associated with this type. Intended to be overridden by subclasses.- Overrides:
getCtorImplementedInterfacesin classObjectType
-
getCtorExtendedInterfaces
public java.lang.Iterable<ObjectType> getCtorExtendedInterfaces()
Description copied from class:ObjectTypeGets the interfaces extended by the interface associated with this type. Intended to be overridden by subclasses.- Overrides:
getCtorExtendedInterfacesin classObjectType
-
matchConstraint
public void matchConstraint(JSType constraint)
Description copied from class:JSTypeModify this type so that it matches the specified type. This is useful for reverse type-inference, where we want to infer that an object literal matches its constraint (much like how the java compiler does reverse-inference to figure out generics).- Overrides:
matchConstraintin classJSType
-
matchRecordTypeConstraint
public void matchRecordTypeConstraint(ObjectType constraintObj)
-
-