com.google.javascript.rhino.jstype
Class FunctionType

java.lang.Object
  extended by com.google.javascript.rhino.jstype.JSType
      extended by com.google.javascript.rhino.jstype.ObjectType
          extended by com.google.javascript.rhino.jstype.FunctionType
All Implemented Interfaces:
StaticScope<JSType>, 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.ObjectType
ObjectType.Property
 
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, UNKNOWN_NAME
 
Method Summary
 boolean canBeCalled()
          This predicate is used to test whether a given type can be used as the 'function' in a function call.
 void clearCachedValues()
          Clear cached values.
 Iterable<ObjectType> getAllExtendedInterfaces()
          Returns all extended interfaces declared by an interfaces or its super- interfaces.
 Iterable<ObjectType> getAllImplementedInterfaces()
          Returns all interfaces implemented by a class or its superclass and any superclasses for any of those interfaces.
 FunctionType getBindReturnType(int argsToBind)
          Get the return value of calling "bind" on this function with the specified number of arguments.
 FunctionType getConstructor()
          Gets this object's constructor.
 Iterable<ObjectType> getCtorExtendedInterfaces()
          Gets the interfaces extended by the interface associated with this type.
 Iterable<ObjectType> getCtorImplementedInterfaces()
          Gets the interfaces implemented by the ctor associated with this type.
 Iterable<ObjectType> getExtendedInterfaces()
          Returns interfaces directly extended by an interface
 int getExtendedInterfacesCount()
          Returns the number of interfaces directly extended by an interface
 Iterable<ObjectType> getImplementedInterfaces()
          Returns interfaces implemented directly by a class or its superclass.
 ObjectType getImplicitPrototype()
          Gets the implicit prototype (a.k.a.
 ObjectType getInstanceType()
          Gets the type of instance of this function.
 int getMaxArguments()
          Gets the maximum number of arguments that this function requires, or Integer.MAX_VALUE if this is a variable argument function.
 int getMinArguments()
          Gets the minimum number of arguments that this function requires.
 FunctionType getOwnerFunction()
          Gets the owner of this if it's a function prototype.
 Iterable<ObjectType> getOwnImplementedInterfaces()
          Returns interfaces directly implemented by the class.
 JSDocInfo getOwnPropertyJSDocInfo(String propertyName)
          Gets the docInfo on the specified property on this type.
 Set<String> getOwnPropertyNames()
          Includes the prototype iff someone has created it.
 Iterable<Node> getParameters()
           
 Node getParametersNode()
          Gets an LP node that contains all params.
 int getPropertiesCount()
          Gets the number of properties of this object.
 Node getPropertyNode(String propertyName)
          Gets the node corresponding to the definition of the specified property.
 JSType getPropertyType(String name)
          Gets the property type of the property whose name is given.
 ObjectType getPrototype()
          Gets the prototype property of this function type.
 String getReferenceName()
          Gets the reference name for this object.
 JSType getReturnType()
           
 ObjectType.Property getSlot(String name)
          Returns any defined slot within this scope for this name.
 Node getSource()
          Gets the source node or null if this is an unknown function.
 List<FunctionType> getSubTypes()
          Returns a list of types that are subtypes of this type.
 FunctionType getSuperClassConstructor()
          Given a constructor or an interface type, get its superclass constructor or null if none exists.
 String getTemplateTypeName()
          Gets the template type name.
static ObjectType getTopDefiningInterface(ObjectType type, String propertyName)
          Given an interface and a property, finds the top-most super interface that has the property defined (including this interface).
 ObjectType getTopMostDefiningType(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).
 ObjectType getTypeOfThis()
          Gets the type of this in this function.
 boolean hasCachedValues()
          Returns true if any cached values have been set for this type.
 boolean hasEqualCallType(FunctionType otherType)
           
 int hashCode()
           
 boolean hasImplementedInterfaces()
           
 boolean hasInstanceType()
          Returns whether this function type has an instance type.
 boolean hasOwnProperty(String propertyName)
          Checks whether the property whose name is given is present directly on the object.
 boolean hasProperty(String propertyName)
          Checks whether the property whose name is given is present on the object.
 boolean hasReferenceName()
          Returns true if the object is named.
 boolean isConstructor()
          Whether this type is a FunctionType that is a constructor or a named type that points to such a type.
 boolean isEquivalentTo(JSType otherType)
          Two function types are equal if their signatures match.
 boolean isInstanceType()
          Whether this type is an Instance object of some constructor.
 boolean isInterface()
          Whether this type is a FunctionType that is an interface or a named type that points to such a type.
 boolean isNativeObjectType()
          Whether this is a built-in object.
 boolean isOrdinaryFunction()
          Whether this type is a FunctionType that is an ordinary function or a named type that points to such a type.
 boolean isPropertyInExterns(String propertyName)
          Checks whether the property was defined in the externs.
 boolean isPropertyTypeDeclared(String property)
          Checks whether the property's type is declared.
 boolean isPropertyTypeInferred(String property)
          Checks whether the property's type is inferred.
 boolean isReturnTypeInferred()
           
 boolean isSubtype(JSType that)
          A function is a subtype of another if their call methods are related via subtyping and this is a subtype of that with regard to the prototype chain.
 void matchConstraint(ObjectType constraintObj)
          Modify this type so that it matches the specified type.
 boolean matchesNumberContext()
          This predicate is used to test whether a given type can appear in a numeric context, such as an operand of a multiply operator.
 boolean matchesObjectContext()
          This predicate is used to test whether a given type can appear in an Object context, such as the expression in a with statement.
 boolean matchesStringContext()
          This predicate is used to test whether a given type can appear in a String context, such as an operand of a string concat (+) operator.
 boolean removeProperty(String name)
          Removes the declared or inferred property from this ObjectType.
 void setExtendedInterfaces(List<ObjectType> extendedInterfaces)
           
 void setImplementedInterfaces(List<ObjectType> implementedInterfaces)
           
 void setPropertyJSDocInfo(String propertyName, JSDocInfo info)
          Sets the docInfo for the specified property from the JSDocInfo on its definition.
 void setPrototypeBasedOn(ObjectType baseType)
          Sets the prototype, creating the prototype object from the given base type.
 void setSource(Node source)
          Sets the source node.
 String toDebugHashCodeString()
          A hash code function for diagnosing complicated issues around type-identity.
 FunctionType toMaybeFunctionType()
          Downcasts this to a FunctionType, or returns null if this is not a function.
 JSType unboxesTo()
          Gets the type to which this type unboxes.
<T> T
visit(Visitor<T> visitor)
          Visit this type with the given visitor.
 
Methods inherited from class com.google.javascript.rhino.jstype.ObjectType
cast, createDelegateSuffix, defineDeclaredProperty, defineInferredProperty, findPropertyType, getDisplayName, getIndexType, getJSDocInfo, getNormalizedReferenceName, getOwnSlot, getParameterType, getParentScope, getPossibleToBooleanOutcomes, getPropertyNames, getRootNode, isFunctionPrototypeType, isObject, isUnknownType, setJSDocInfo, testForEquality
 
Methods inherited from class com.google.javascript.rhino.jstype.JSType
autobox, autoboxesTo, canAssignTo, canTestForEqualityWith, canTestForShallowEqualityWith, clearResolved, collapseUnion, dereference, differsFrom, equals, forceResolve, getGreatestSubtype, getLeastSupertype, getRestrictedTypeGivenToBooleanOutcome, getTypesUnderEquality, getTypesUnderInequality, getTypesUnderShallowEquality, getTypesUnderShallowInequality, hasDisplayName, isAllType, isArrayType, isBooleanObjectType, isBooleanValueType, isCheckedUnknownType, isDateType, isEmptyType, isEnumElementType, isEnumType, isEquivalent, isFunctionType, isGlobalThisType, isNominalConstructor, isNominalType, isNoObjectType, isNoResolvedType, isNoType, isNullable, isNullType, isNumber, isNumberObjectType, isNumberValueType, isRecordType, isRegexpType, isResolved, isString, isStringObjectType, isStringValueType, isTemplateType, isUnionType, isVoidType, matchesInt32Context, matchesUint32Context, resolve, restrictByNotNullOrUndefined, setValidator, toAnnotationString, toMaybeEnumElementType, toMaybeEnumType, toMaybeFunctionType, toMaybeUnionType, toObjectType, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Method Detail

isInstanceType

public boolean isInstanceType()
Description copied from class: JSType
Whether this type is an Instance object of some constructor. Does not necessarily mean this is an InstanceObjectType.

Overrides:
isInstanceType in class JSType

isConstructor

public boolean isConstructor()
Description copied from class: JSType
Whether this type is a FunctionType that is a constructor or a named type that points to such a type.

Overrides:
isConstructor in class JSType

isInterface

public boolean isInterface()
Description copied from class: JSType
Whether this type is a FunctionType that is an interface or a named type that points to such a type.

Overrides:
isInterface in class JSType

isOrdinaryFunction

public boolean isOrdinaryFunction()
Description copied from class: JSType
Whether this type is a FunctionType that is an ordinary function or a named type that points to such a type.

Overrides:
isOrdinaryFunction in class JSType

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 class JSType

canBeCalled

public 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.

Returns:
true if this type might be callable.

hasImplementedInterfaces

public boolean hasImplementedInterfaces()

getParameters

public 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 ObjectType.Property getSlot(String name)
Description copied from interface: StaticScope
Returns any defined slot within this scope for this name. This call continues searching through parent scopes if a slot with this name is not found in the current scope.

Specified by:
getSlot in interface StaticScope<JSType>
Parameters:
name - The name of the variable slot to look up.
Returns:
The defined slot for the variable, or null if no definition exists.

getOwnPropertyNames

public Set<String> getOwnPropertyNames()
Includes the prototype iff someone has created it. We do not want to expose the prototype for ordinary functions.


getPrototype

public ObjectType getPrototype()
Gets the prototype property 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 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 Iterable<ObjectType> getImplementedInterfaces()
Returns interfaces implemented directly by a class or its superclass.


getOwnImplementedInterfaces

public Iterable<ObjectType> getOwnImplementedInterfaces()
Returns interfaces directly implemented by the class.


setImplementedInterfaces

public void setImplementedInterfaces(List<ObjectType> implementedInterfaces)

getAllExtendedInterfaces

public 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 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(List<ObjectType> extendedInterfaces)
                           throws UnsupportedOperationException
Throws:
UnsupportedOperationException

getPropertyType

public JSType getPropertyType(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.

Returns:
the property's type or UnknownType. This method never returns null.

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 or null if none exists.


getTopDefiningInterface

public static ObjectType getTopDefiningInterface(ObjectType type,
                                                 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(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).


isEquivalentTo

public boolean isEquivalentTo(JSType otherType)
Two function types are equal if their signatures match. Since they don't have signatures, two interfaces are equal if their names match.

Overrides:
isEquivalentTo in class JSType

hashCode

public int hashCode()
Overrides:
hashCode in class JSType

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 and this is a subtype of that with regard to the prototype chain.

Returns:
this &lt;: that

visit

public <T> T visit(Visitor<T> visitor)
Description copied from class: JSType
Visit this type with the given visitor.

Overrides:
visit in class ObjectType
Returns:
the value returned by the visitor
See Also:
Visitor

getInstanceType

public ObjectType getInstanceType()
Gets the type of instance of this function.

Throws:
IllegalStateException - if this function is not a constructor (see isConstructor()).

hasInstanceType

public boolean hasInstanceType()
Returns whether this function type has an instance type.


getTypeOfThis

public ObjectType getTypeOfThis()
Gets the type of this in this function.

Specified by:
getTypeOfThis in interface StaticScope<JSType>
Overrides:
getTypeOfThis in class ObjectType

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: ObjectType
Clear cached values. Should be called before making changes to a prototype that may have been changed since creation.

Overrides:
clearCachedValues in class ObjectType

getSubTypes

public 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: 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.


getTemplateTypeName

public String getTemplateTypeName()
Gets the template type name.


toDebugHashCodeString

public String toDebugHashCodeString()
Description copied from class: JSType
A hash code function for diagnosing complicated issues around type-identity.

Overrides:
toDebugHashCodeString in class JSType

getPropertiesCount

public int getPropertiesCount()
Gets the number of properties of this object.

Specified by:
getPropertiesCount in class ObjectType

hasProperty

public boolean hasProperty(String propertyName)
Description copied from class: ObjectType
Checks whether the property whose name is given is present on the object.

Specified by:
hasProperty in class ObjectType

hasOwnProperty

public boolean hasOwnProperty(String propertyName)
Description copied from class: ObjectType
Checks whether the property whose name is given is present directly on the object. Returns false even if it is declared on a supertype.

Overrides:
hasOwnProperty in class ObjectType

isPropertyTypeDeclared

public boolean isPropertyTypeDeclared(String property)
Description copied from class: ObjectType
Checks whether the property's type is declared.

Specified by:
isPropertyTypeDeclared in class ObjectType

isPropertyTypeInferred

public boolean isPropertyTypeInferred(String property)
Description copied from class: ObjectType
Checks whether the property's type is inferred.

Specified by:
isPropertyTypeInferred in class ObjectType

isPropertyInExterns

public boolean isPropertyInExterns(String propertyName)
Description copied from class: ObjectType
Checks whether the property was defined in the externs.

Overrides:
isPropertyInExterns in class ObjectType

removeProperty

public boolean removeProperty(String name)
Description copied from class: ObjectType
Removes the declared or inferred property from this ObjectType.

Overrides:
removeProperty in class ObjectType
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.

getPropertyNode

public Node getPropertyNode(String propertyName)
Description copied from class: ObjectType
Gets the node corresponding to the definition of the specified property. This could be the node corresponding to declaration of the property or the node corresponding to the first reference to this property, e.g., "this.propertyName" in a constructor. Note this is mainly intended to be an estimate of where in the source code a property is defined. Sometime the returned node is not even part of the global AST but in the AST of the JsDoc that defines a type.

Overrides:
getPropertyNode in class ObjectType
Parameters:
propertyName - the name of the property
Returns:
the Node corresponding to the property or null.

getOwnPropertyJSDocInfo

public JSDocInfo getOwnPropertyJSDocInfo(String propertyName)
Description copied from class: ObjectType
Gets the docInfo on the specified property on this type. This should not be done implemented recursively, as you generally need to know exactly on which type in the prototype chain the JSDocInfo exists.

Overrides:
getOwnPropertyJSDocInfo in class ObjectType

setPropertyJSDocInfo

public void setPropertyJSDocInfo(String propertyName,
                                 JSDocInfo info)
Description copied from class: ObjectType
Sets the docInfo for the specified property from the JSDocInfo on its definition.

Overrides:
setPropertyJSDocInfo in class ObjectType
info - JSDocInfo for the property definition. May be null.

matchesNumberContext

public boolean matchesNumberContext()
Description copied from class: JSType
This predicate is used to test whether a given type can appear in a numeric context, such as an operand of a multiply operator.

Overrides:
matchesNumberContext in class JSType

matchesStringContext

public boolean matchesStringContext()
Description copied from class: JSType
This predicate is used to test whether a given type can appear in a String context, such as an operand of a string concat (+) operator. All types have at least the potential for converting to String. When we add externally defined types, such as a browser OM, we may choose to add types that do not automatically convert to String.

Overrides:
matchesStringContext in class JSType

unboxesTo

public JSType unboxesTo()
Description copied from class: JSType
Gets the type to which this type unboxes.

Overrides:
unboxesTo in class JSType
Returns:
the unboxed type or null if this type does not unbox.

matchesObjectContext

public boolean matchesObjectContext()
Description copied from class: JSType
This predicate is used to test whether a given type can appear in an Object context, such as the expression in a with statement. Most types we will encounter, except notably null, have at least the potential for converting to Object. Host defined objects can get peculiar.

Overrides:
matchesObjectContext in class JSType

getConstructor

public FunctionType getConstructor()
Description copied from class: ObjectType
Gets this object's constructor.

Specified by:
getConstructor in class ObjectType
Returns:
this object's constructor or null if it is a native object (constructed natively v.s. by instantiation of a function)

getImplicitPrototype

public ObjectType getImplicitPrototype()
Description copied from class: ObjectType
Gets the implicit prototype (a.k.a. the [[Prototype]] property).

Specified by:
getImplicitPrototype in class ObjectType

getReferenceName

public String getReferenceName()
Description copied from class: ObjectType
Gets 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:
getReferenceName in class ObjectType
Returns:
the object's name or null if this is an anonymous object

hasReferenceName

public boolean hasReferenceName()
Description copied from class: ObjectType
Returns true if the object is named.

Overrides:
hasReferenceName in class ObjectType
Returns:
true if the object is named, false if it is anonymous

isNativeObjectType

public boolean isNativeObjectType()
Whether this is a built-in object.

Overrides:
isNativeObjectType in class ObjectType

getOwnerFunction

public FunctionType getOwnerFunction()
Description copied from class: ObjectType
Gets the owner of this if it's a function prototype.

Overrides:
getOwnerFunction in class ObjectType

getCtorImplementedInterfaces

public Iterable<ObjectType> getCtorImplementedInterfaces()
Description copied from class: ObjectType
Gets the interfaces implemented by the ctor associated with this type. Intended to be overridden by subclasses.

Overrides:
getCtorImplementedInterfaces in class ObjectType

getCtorExtendedInterfaces

public Iterable<ObjectType> getCtorExtendedInterfaces()
Description copied from class: ObjectType
Gets the interfaces extended by the interface associated with this type. Intended to be overriden by subclasses.

Overrides:
getCtorExtendedInterfaces in class ObjectType

matchConstraint

public void matchConstraint(ObjectType constraintObj)
Description copied from class: JSType
Modify 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 contraint (much like how the java compiler does reverse-inference to figure out generics).

Overrides:
matchConstraint in class JSType