Class FunctionType
- java.lang.Object
-
- com.google.javascript.rhino.jstype.JSType
-
- com.google.javascript.rhino.jstype.ObjectType
-
- com.google.javascript.rhino.jstype.PrototypeObjectType
-
- com.google.javascript.rhino.jstype.FunctionType
-
- All Implemented Interfaces:
JSType.WithSourceRef
- Direct Known Subclasses:
NoObjectType
public class FunctionType extends PrototypeObjectType implements JSType.WithSourceRef
This derived type provides extended information about a function, including its return type and argument types.Note: the parameters list is the PARAM_LIST 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
FunctionType.Builder
A builder class for function and arrow types.static class
FunctionType.Parameter
Models a single JavaScript parameter.-
Nested classes/interfaces inherited from class com.google.javascript.rhino.jstype.JSType
JSType.HasPropertyKind, JSType.Nullability, JSType.SubtypingMode, JSType.TypePair, JSType.WithSourceRef
-
-
Field Summary
-
Fields inherited from class com.google.javascript.rhino.jstype.JSType
templateTypeMap
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
acceptsArguments(java.util.List<? extends JSType> argumentTypes)
static FunctionType.Builder
builder(JSTypeRegistry registry)
boolean
canBeCalled()
This predicate is used to test whether a given type can be used as the 'function' in a function call.java.util.List<FunctionType>
checkExtendsLoop()
check if there is a loop in the type extends chainvoid
clearCachedValues()
Clear cached values.FunctionType
forgetParameterAndReturnTypes()
Create a new constructor with the parameters and return type stripped.java.lang.Iterable<ObjectType>
getAllImplementedInterfaces()
Returns all interfaces implemented by a class or its superclass and any superclasses for any of those interfaces.java.util.Collection<ObjectType>
getAncestorInterfaces()
FunctionType
getBindReturnType(int argsToBind)
Get the return value of calling "bind" on this function with the specified number of arguments.FunctionType
getCanonicalRepresentation()
ClosurePrimitive
getClosurePrimitive()
Returns the@closurePrimitive
identifier associated with this functionFunctionType
getConstructor()
Gets this object's constructor.com.google.common.collect.ImmutableList<TemplateType>
getConstructorOnlyTemplateParameters()
Returns a list of template types present on the constructor but not on the instance.com.google.common.collect.ImmutableList<ObjectType>
getExtendedInterfaces()
Returns interfaces directly extended by an interfaceint
getExtendedInterfacesCount()
Returns the number of interfaces directly extended by an interfacejava.lang.String
getGoogModuleId()
The ID of the goog.module in which this type was declared.com.google.common.collect.ImmutableList<ObjectType>
getImplementedInterfaces()
Returns interfaces implemented directly by a class or its superclass.ObjectType
getInstanceType()
Gets the type of instance of this function.int
getMaxArity()
Gets the maximum number of arguments that this function requires, or Integer.MAX_VALUE if this is a variable argument function.int
getMinArity()
Gets the minimum number of arguments that this function requires.com.google.common.collect.ImmutableList<ObjectType>
getOwnImplementedInterfaces()
Returns interfaces directly implemented by the class.java.util.Set<java.lang.String>
getOwnPropertyNames()
Includes the prototype iff someone has created it.com.google.common.collect.ImmutableList<FunctionType.Parameter>
getParameters()
JSType
getPropertyType(java.lang.String name)
Gets the property type of the property whose name is given.java.util.Map<java.lang.String,JSType>
getPropertyTypeMap()
get the map of properties to types covered in a function typeObjectType
getPrototype()
Gets theprototype
property of this function type.ObjectType
getPrototypeProperty()
JSType
getReturnType()
Property
getSlot(java.lang.String name)
Default getSlot implementation.Node
getSource()
Gets the source node or null if this is an unknown function.FunctionType
getSuperClassConstructor()
Given a constructor or an interface type, get its superclass constructor ornull
if none exists.JSType
getTypeOfThis()
Gets the type ofthis
in this function.boolean
hasCachedValues()
Returns true if any cached values have been set for this type.boolean
hasEqualCallType(FunctionType that)
boolean
hasImplementedInterfaces()
boolean
hasInstanceType()
Returns whether this function type has an instance type.boolean
hasProperties()
boolean
isAbstract()
boolean
isAmbiguousConstructor()
Returns true if the constructor does not come from a literal class or function in the AST, or if it extends such an ambiguous constructorboolean
isConstructor()
Whether this type is aFunctionType
that is a constructor or a named type that points to such a type.boolean
isInstanceType()
Whether this type is an Instance object of some constructor.boolean
isInterface()
Whether this type is aFunctionType
that is an interface or a named type that points to such a type.boolean
isOrdinaryFunction()
Whether this type is aFunctionType
that is an ordinary function (i.e.boolean
isReturnTypeInferred()
boolean
isStructuralInterface()
boolean
makesDicts()
When a class B inherits from A and A is annotated as a dict, then B automatically gets the annotation, if B's constructor is not explicitly annotated.boolean
makesStructs()
When a class B inherits from A and A is annotated as a struct, then B automatically gets the annotation, if B's constructor is not explicitly annotated.void
setDict()
void
setExplicitUnrestricted()
void
setExtendedInterfaces(java.util.List<ObjectType> extendedInterfaces)
void
setImplementedInterfaces(java.util.List<ObjectType> implementedInterfaces)
void
setImplicitMatch(boolean flag)
sets the current interface type to support structural interface matching (abbr.void
setPrototypeBasedOn(ObjectType baseType)
Sets the prototype, creating the prototype object from the given base type.void
setSource(Node source)
Sets the source node.void
setStruct()
FunctionType.Builder
toBuilder()
Copies all the information from another function type.FunctionType
toMaybeFunctionType()
Downcasts this to a FunctionType, or returns null if this is not a function.<T> T
visit(Visitor<T> visitor)
Visit this type with the given visitor.-
Methods inherited from class com.google.javascript.rhino.jstype.PrototypeObjectType
getCtorExtendedInterfaces, getCtorImplementedInterfaces, getImplicitPrototype, getOwnerFunction, getReferenceName, getTemplateParamCount, isAnonymous, isNativeObjectType, matchConstraint, matchesNumberContext, matchesObjectContext, matchesStringContext, matchesSymbolContext, matchRecordTypeConstraint, removeProperty, setPropertyJSDocInfo, setPropertyNode
-
Methods inherited from class com.google.javascript.rhino.jstype.ObjectType
cast, createDelegateSuffix, defineDeclaredProperty, defineInferredProperty, defineSynthesizedProperty, findClosestDefinition, findPropertyTypeWithoutConsideringTemplateTypes, getClosestDefiningType, getDisplayName, getEnumeratedTypeOfEnumObject, getImplicitPrototypeChain, getJSDocInfo, getNormalizedReferenceName, getOwnPropertyDefSite, getOwnPropertyJSDocInfo, getOwnPropertyKind, getOwnSlot, getPossibleToBooleanOutcomes, getPropertiesCount, getPropertyDefSite, getPropertyJSDocInfo, getPropertyKind, getPropertyNames, getPropertyNode, getRawType, getTemplateTypes, hasOwnDeclaredProperty, hasOwnProperty, hasReferenceName, isFunctionPrototypeType, isObject, isPropertyInExterns, isPropertyTypeDeclared, isPropertyTypeInferred, isStructuralType, isUnknownType, setJSDocInfo, testForEquality
-
Methods inherited from class com.google.javascript.rhino.jstype.JSType
areSimilar, assertFunctionType, assertObjectType, autobox, autoboxesTo, canCastTo, canTestForEqualityWith, canTestForShallowEqualityWith, collapseUnion, containsReferenceAncestor, dereference, differsFrom, equals, findPropertyType, getEnumeratedTypeOfEnumElement, getGreatestSubtype, getLeastSupertype, getPropertyKind, getRestrictedTypeGivenOutcome, getTemplateTypeMap, getTypeParameters, getTypesUnderEquality, getTypesUnderInequality, getTypesUnderShallowEquality, getTypesUnderShallowInequality, getUnionMembers, hasAnyTemplateTypes, hasDisplayName, hashCode, hasProperty, isAllType, isArrayType, isBigIntObjectType, isBigIntOrNumber, isBigIntValueType, isBooleanObjectType, isBooleanValueType, isBoxableScalar, isCheckedUnknownType, isDateType, isDict, isEmptyType, isEnumElementType, isEnumType, isExplicitlyVoidable, isFunctionType, isGlobalThisType, isLiteralObject, isNamedType, isNominalConstructorOrInterface, isNominalType, isNoObjectType, isNoResolvedType, isNoType, isNullable, isNullType, isNumber, isNumberObjectType, isNumberValueType, isObjectType, isOnlyBigInt, isRawTypeOfTemplatizedType, isRecordType, isRegexpType, isResolved, isSomeUnknownType, isString, isStringObjectType, isStringValueType, isStruct, isSubtype, isSubtype, isSubtypeOf, isSubtypeOf, isSubtypeWithoutStructuralTyping, isSuccessfullyResolved, isSymbol, isSymbolObjectType, isSymbolValueType, isTemplateType, isTemplatizedType, isUnionType, isUnsuccessfullyResolved, isVoidable, isVoidType, loosenTypecheckingDueToForwardReferencedSupertype, mergeSupertypeTemplateTypes, resolve, restrictByNotNull, restrictByNotNullOrUndefined, restrictByNotUndefined, setValidator, toAnnotationString, toMaybeEnumElementType, toMaybeEnumType, toMaybeFunctionType, toMaybeNamedType, toMaybeObjectType, toMaybeRecordType, toMaybeTemplateType, toMaybeTemplatizedType, toMaybeUnionType, toObjectType, toString
-
-
-
-
Method Detail
-
getConstructor
public FunctionType getConstructor()
Description copied from class:ObjectType
Gets this object's constructor.- Overrides:
getConstructor
in classPrototypeObjectType
- Returns:
- this object's constructor or
null
if it is a native object (constructed natively v.s. by instantiation of a function)
-
isInstanceType
public final boolean isInstanceType()
Description copied from class:JSType
Whether this type is an Instance object of some constructor. Does not necessarily mean this is anInstanceObjectType
.- Overrides:
isInstanceType
in classJSType
-
isConstructor
public final boolean isConstructor()
Description copied from class:JSType
Whether this type is aFunctionType
that is a constructor or a named type that points to such a type.- Overrides:
isConstructor
in classJSType
-
isInterface
public final boolean isInterface()
Description copied from class:JSType
Whether this type is aFunctionType
that is an interface or a named type that points to such a type.- Overrides:
isInterface
in classJSType
-
isOrdinaryFunction
public final boolean isOrdinaryFunction()
Description copied from class:JSType
Whether this type is aFunctionType
that is an ordinary function (i.e. not a constructor, nominal interface, or record interface), or a named type that points to such a type.- Overrides:
isOrdinaryFunction
in classJSType
-
makesStructs
public final boolean makesStructs()
When a class B inherits from A and A is annotated as a struct, then B automatically gets the annotation, if B's constructor is not explicitly annotated.
-
makesDicts
public final boolean makesDicts()
When a class B inherits from A and A is annotated as a dict, then B automatically gets the annotation, if B's constructor is not explicitly annotated.
-
setStruct
public final void setStruct()
-
setDict
public final void setDict()
-
setExplicitUnrestricted
public final void setExplicitUnrestricted()
-
toMaybeFunctionType
public FunctionType toMaybeFunctionType()
Description copied from class:JSType
Downcasts 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:
toMaybeFunctionType
in classJSType
-
canBeCalled
public final boolean canBeCalled()
Description copied from class:JSType
This predicate is used to test whether a given type can be used as the 'function' in a function call.- Overrides:
canBeCalled
in classJSType
- Returns:
true
if this type might be callable.
-
hasImplementedInterfaces
public final boolean hasImplementedInterfaces()
-
getParameters
public final com.google.common.collect.ImmutableList<FunctionType.Parameter> getParameters()
-
getMinArity
public final int getMinArity()
Gets the minimum number of arguments that this function requires.
-
getMaxArity
public final int getMaxArity()
Gets the maximum number of arguments that this function requires, or Integer.MAX_VALUE if this is a variable argument function.
-
getReturnType
public final JSType getReturnType()
-
isReturnTypeInferred
public final boolean isReturnTypeInferred()
-
getSlot
public final Property getSlot(java.lang.String name)
Description copied from class:ObjectType
Default getSlot implementation. This gets overridden by FunctionType for lazily-resolved prototypes.- Overrides:
getSlot
in classObjectType
-
getOwnPropertyNames
public final 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:
getOwnPropertyNames
in classObjectType
-
getPrototypeProperty
public final ObjectType getPrototypeProperty()
-
getPrototype
public final ObjectType getPrototype()
Gets theprototype
property of this function type. This is equivalent to(ObjectType) getPropertyType("prototype")
.
-
setPrototypeBasedOn
public final void setPrototypeBasedOn(ObjectType baseType)
Sets the prototype, creating the prototype object from the given base type.- Parameters:
baseType
- The base type.
-
getAllImplementedInterfaces
public final 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.
-
getAncestorInterfaces
public final java.util.Collection<ObjectType> getAncestorInterfaces()
-
getImplementedInterfaces
public final com.google.common.collect.ImmutableList<ObjectType> getImplementedInterfaces()
Returns interfaces implemented directly by a class or its superclass.
-
getOwnImplementedInterfaces
public final com.google.common.collect.ImmutableList<ObjectType> getOwnImplementedInterfaces()
Returns interfaces directly implemented by the class.
-
setImplementedInterfaces
public final void setImplementedInterfaces(java.util.List<ObjectType> implementedInterfaces)
-
getExtendedInterfaces
public final com.google.common.collect.ImmutableList<ObjectType> getExtendedInterfaces()
Returns interfaces directly extended by an interface
-
getExtendedInterfacesCount
public final int getExtendedInterfacesCount()
Returns the number of interfaces directly extended by an interface
-
setExtendedInterfaces
public final void setExtendedInterfaces(java.util.List<ObjectType> extendedInterfaces)
-
getPropertyType
public final JSType getPropertyType(java.lang.String name)
Description copied from class:ObjectType
Gets 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:
getPropertyType
in classObjectType
- Returns:
- the property's type or
UnknownType
. This method never returnsnull
.
-
getBindReturnType
public final 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 final FunctionType getSuperClassConstructor()
Given a constructor or an interface type, get its superclass constructor ornull
if none exists.- Overrides:
getSuperClassConstructor
in classObjectType
-
hasEqualCallType
public final boolean hasEqualCallType(FunctionType that)
-
visit
public <T> T visit(Visitor<T> visitor)
Description copied from class:JSType
Visit this type with the given visitor.- Overrides:
visit
in classObjectType
- Returns:
- the value returned by the visitor
- See Also:
Visitor
-
getInstanceType
public final ObjectType getInstanceType()
Gets the type of instance of this function. May return null if the `this` type can not be converted to "ObjectType" (see JSType#toObjectType).- Throws:
java.lang.IllegalStateException
- if this function is not a constructor (seeisConstructor()
).
-
hasInstanceType
public final boolean hasInstanceType()
Returns whether this function type has an instance type.
-
getTypeOfThis
public final JSType getTypeOfThis()
Gets the type ofthis
in this function.- Overrides:
getTypeOfThis
in classObjectType
-
getSource
public final Node getSource()
Gets the source node or null if this is an unknown function.- Specified by:
getSource
in interfaceJSType.WithSourceRef
-
getGoogModuleId
@Nullable public java.lang.String getGoogModuleId()
Description copied from interface:JSType.WithSourceRef
The ID of the goog.module in which this type was declared.- Specified by:
getGoogModuleId
in interfaceJSType.WithSourceRef
-
setSource
public final void setSource(Node source)
Sets the source node.
-
clearCachedValues
public final void clearCachedValues()
Description copied from class:ObjectType
Clear cached values. Should be called before making changes to a prototype that may have been changed since creation.- Overrides:
clearCachedValues
in classObjectType
-
hasCachedValues
public final boolean hasCachedValues()
Description copied from class:ObjectType
Returns 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.- Overrides:
hasCachedValues
in classObjectType
-
hasProperties
public final boolean hasProperties()
-
setImplicitMatch
public final void setImplicitMatch(boolean flag)
sets the current interface type to support structural interface matching (abbr. SMI)- Parameters:
flag
- indicates whether or not it should support SMI
-
isStructuralInterface
public final boolean isStructuralInterface()
- Overrides:
isStructuralInterface
in classJSType
-
isAbstract
public final boolean isAbstract()
-
getPropertyTypeMap
public final java.util.Map<java.lang.String,JSType> getPropertyTypeMap()
get the map of properties to types covered in a function type- Overrides:
getPropertyTypeMap
in classObjectType
- Returns:
- a Map that maps the property's name to the property's type
-
checkExtendsLoop
public final java.util.List<FunctionType> checkExtendsLoop()
check if there is a loop in the type extends chain- Returns:
- an array of all functions in the loop chain if a loop exists, otherwise returns null
-
acceptsArguments
public final boolean acceptsArguments(java.util.List<? extends JSType> argumentTypes)
-
forgetParameterAndReturnTypes
public final FunctionType forgetParameterAndReturnTypes()
Create a new constructor with the parameters and return type stripped.
-
getConstructorOnlyTemplateParameters
public final com.google.common.collect.ImmutableList<TemplateType> getConstructorOnlyTemplateParameters()
Returns a list of template types present on the constructor but not on the instance.
-
isAmbiguousConstructor
public final boolean isAmbiguousConstructor()
Returns true if the constructor does not come from a literal class or function in the AST, or if it extends such an ambiguous constructor
-
getClosurePrimitive
public final ClosurePrimitive getClosurePrimitive()
Returns the@closurePrimitive
identifier associated with this function
-
builder
public static FunctionType.Builder builder(JSTypeRegistry registry)
-
toBuilder
public FunctionType.Builder toBuilder()
Copies all the information from another function type.
-
getCanonicalRepresentation
public final FunctionType getCanonicalRepresentation()
-
-