com.google.javascript.rhino.jstype
Class ObjectType

java.lang.Object
  extended by com.google.javascript.rhino.jstype.JSType
      extended by com.google.javascript.rhino.jstype.ObjectType
All Implemented Interfaces:
StaticScope<JSType>, Serializable
Direct Known Subclasses:
EnumElementType, EnumType, FunctionType, TemplateType, UnknownType

public abstract class ObjectType
extends JSType
implements StaticScope<JSType>

Object type. In JavaScript, all object types have properties, and each of those properties has a type. Property types may be DECLARED, INFERRED, or UNKNOWN. DECLARED properties have an explicit type annotation, as in: /xx @type {number} x/ Foo.prototype.bar = 1; This property may only hold number values, and an assignment to any other type of value is an error. INFERRED properties do not have an explicit type annotation. Rather, we try to find all the possible types that this property can hold. Foo.prototype.bar = 1; If the programmer assigns other types of values to this property, the property will take on the union of all these types. UNKNOWN properties are properties on the UNKNOWN type. The UNKNOWN type has all properties, but we do not know whether they are declared or inferred.

See Also:
Serialized Form

Nested Class Summary
static class 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
static ObjectType cast(JSType type)
          A null-safe version of JSType#toObjectType.
 void clearCachedValues()
          Clear cached values.
static String createDelegateSuffix(String suffix)
          Creates a suffix for a proxy delegate.
 boolean defineDeclaredProperty(String propertyName, JSType type, Node propertyNode)
          Defines a property whose type is synthesized (i.e.
 boolean defineInferredProperty(String propertyName, JSType type, Node propertyNode)
          Defines a property whose type is inferred.
 JSType findPropertyType(String propertyName)
          Coerces this type to an Object type, then gets the type of the property whose name is given.
abstract  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.
 String getDisplayName()
          Returns a user meaningful label for the JSType instance.
abstract  ObjectType getImplicitPrototype()
          Gets the implicit prototype (a.k.a.
 JSType getIndexType()
          Gets the declared default index type.
 JSDocInfo getJSDocInfo()
          Gets the docInfo for this type.
 String getNormalizedReferenceName()
          Due to the complexity of some of our internal type systems, sometimes we have different types constructed by the same constructor.
 FunctionType getOwnerFunction()
          Gets the owner of this if it's a function prototype.
 JSDocInfo getOwnPropertyJSDocInfo(String propertyName)
          Gets the docInfo on the specified property on this type.
 Set<String> getOwnPropertyNames()
          Returns the names of all the properties directly on this type.
 ObjectType.Property getOwnSlot(String name)
          Like getSlot but does not recurse into parent scopes.
 JSType getParameterType()
          Gets the declared default element type.
 ObjectType getParentScope()
          Returns the scope enclosing this one or null if none.
 BooleanLiteralSet getPossibleToBooleanOutcomes()
          Computes the set of possible outcomes of the ToBoolean predicate for this type.
abstract  int getPropertiesCount()
          Gets the number of properties of this object.
 Set<String> getPropertyNames()
          Returns a list of properties defined or inferred on this type and any of its supertypes.
 Node getPropertyNode(String propertyName)
          Gets the node corresponding to the definition of the specified property.
abstract  JSType getPropertyType(String propertyName)
          Gets the property type of the property whose name is given.
abstract  String getReferenceName()
          Gets the reference name for this object.
 Node getRootNode()
          Returns the root node associated with this scope.
abstract  ObjectType.Property getSlot(String name)
          Returns any defined slot within this scope for this name.
 ObjectType getTypeOfThis()
          Returns the expected type of this in the current scope.
 boolean hasCachedValues()
          Returns true if any cached values have been set for this type.
 boolean hasOwnProperty(String propertyName)
          Checks whether the property whose name is given is present directly on the object.
abstract  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 isFunctionPrototypeType()
          Whether this is the prototype of a function.
 boolean isNativeObjectType()
          Whether this is a built-in object.
 boolean isObject()
          Tests whether this type is an Object, or any subtype thereof.
 boolean isPropertyInExterns(String propertyName)
          Checks whether the property was defined in the externs.
abstract  boolean isPropertyTypeDeclared(String propertyName)
          Checks whether the property's type is declared.
abstract  boolean isPropertyTypeInferred(String propertyName)
          Checks whether the property's type is inferred.
 boolean isUnknownType()
          We treat this as the unknown type if any of its implicit prototype properties is unknown.
 boolean removeProperty(String propertyName)
          Removes the declared or inferred property from this ObjectType.
 void setJSDocInfo(JSDocInfo info)
          Sets the docInfo for this type from the given JSDocInfo.
 void setPropertyJSDocInfo(String propertyName, JSDocInfo info)
          Sets the docInfo for the specified property from the JSDocInfo on its definition.
 TernaryValue testForEquality(JSType that)
          Compares this and that.
<T> T
visit(Visitor<T> visitor)
          Visit this type with the given visitor.
 
Methods inherited from class com.google.javascript.rhino.jstype.JSType
autobox, autoboxesTo, canAssignTo, canBeCalled, canTestForEqualityWith, canTestForShallowEqualityWith, clearResolved, collapseUnion, dereference, differsFrom, equals, forceResolve, getGreatestSubtype, getLeastSupertype, getRestrictedTypeGivenToBooleanOutcome, getTypesUnderEquality, getTypesUnderInequality, getTypesUnderShallowEquality, getTypesUnderShallowInequality, hasDisplayName, hashCode, isAllType, isArrayType, isBooleanObjectType, isBooleanValueType, isCheckedUnknownType, isConstructor, isDateType, isEmptyType, isEnumElementType, isEnumType, isEquivalent, isEquivalentTo, isFunctionType, isGlobalThisType, isInstanceType, isInterface, isNominalConstructor, isNominalType, isNoObjectType, isNoResolvedType, isNoType, isNullable, isNullType, isNumber, isNumberObjectType, isNumberValueType, isOrdinaryFunction, isRecordType, isRegexpType, isResolved, isString, isStringObjectType, isStringValueType, isSubtype, isTemplateType, isUnionType, isVoidType, matchConstraint, matchesInt32Context, matchesNumberContext, matchesObjectContext, matchesStringContext, matchesUint32Context, resolve, restrictByNotNullOrUndefined, setValidator, toAnnotationString, toDebugHashCodeString, toMaybeEnumElementType, toMaybeEnumType, toMaybeFunctionType, toMaybeFunctionType, toMaybeUnionType, toObjectType, toString, unboxesTo
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Method Detail

getRootNode

public Node getRootNode()
Description copied from interface: StaticScope
Returns the root node associated with this scope. May be null.

Specified by:
getRootNode in interface StaticScope<JSType>

getParentScope

public ObjectType getParentScope()
Description copied from interface: StaticScope
Returns the scope enclosing this one or null if none.

Specified by:
getParentScope in interface StaticScope<JSType>

getSlot

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

getOwnSlot

public ObjectType.Property getOwnSlot(String name)
Description copied from interface: StaticScope
Like getSlot but does not recurse into parent scopes.

Specified by:
getOwnSlot in interface StaticScope<JSType>

getTypeOfThis

public ObjectType getTypeOfThis()
Description copied from interface: StaticScope
Returns the expected type of this in the current scope.

Specified by:
getTypeOfThis in interface StaticScope<JSType>

getParameterType

public JSType getParameterType()
Gets the declared default element type.

See Also:
ParameterizedType

getIndexType

public JSType getIndexType()
Gets the declared default index type.

See Also:
IndexedType

getJSDocInfo

public JSDocInfo getJSDocInfo()
Gets the docInfo for this type.

Overrides:
getJSDocInfo in class JSType

setJSDocInfo

public void setJSDocInfo(JSDocInfo info)
Sets the docInfo for this type from the given JSDocInfo. The JSDocInfo may be null.


getReferenceName

public abstract String getReferenceName()
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.

Returns:
the object's name or null if this is an anonymous object

getNormalizedReferenceName

public String getNormalizedReferenceName()
Due to the complexity of some of our internal type systems, sometimes we have different types constructed by the same constructor. In other parts of the type system, these are called delegates. We construct these types by appending suffixes to the constructor name. The normalized reference name does not have these suffixes, and as such, recollapses these implicit types back to their real type.


getDisplayName

public String getDisplayName()
Description copied from class: JSType
Returns a user meaningful label for the JSType instance. For example, Functions and Enums will return their declaration name (if they have one). Some types will not have a meaningful display name. Calls to hasDisplayName() will return true IFF getDisplayName() will return null or a zero length string.

Overrides:
getDisplayName in class JSType
Returns:
the display name of the type, or null if one is not available

createDelegateSuffix

public static String createDelegateSuffix(String suffix)
Creates a suffix for a proxy delegate.

See Also:
getNormalizedReferenceName()

hasReferenceName

public boolean hasReferenceName()
Returns true if the object is named.

Returns:
true if the object is named, false if it is anonymous

testForEquality

public TernaryValue testForEquality(JSType that)
Description copied from class: JSType
Compares this and that.

Overrides:
testForEquality in class JSType
Returns:
  • TernaryValue.TRUE if the comparison of values of this type and that always succeed (such as undefined compared to null)
  • TernaryValue.FALSE if the comparison of values of this type and that always fails (such as undefined compared to number)
  • TernaryValue.UNKNOWN if the comparison can succeed or fail depending on the concrete values

getConstructor

public abstract FunctionType getConstructor()
Gets this object's constructor.

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

getImplicitPrototype

public abstract ObjectType getImplicitPrototype()
Gets the implicit prototype (a.k.a. the [[Prototype]] property).


defineDeclaredProperty

public final boolean defineDeclaredProperty(String propertyName,
                                            JSType type,
                                            Node propertyNode)
Defines a property whose type is synthesized (i.e. not inferred).

Parameters:
propertyName - the property's name
type - the type
propertyNode - the node corresponding to the declaration of property which might later be accessed using getPropertyNode.

defineInferredProperty

public final boolean defineInferredProperty(String propertyName,
                                            JSType type,
                                            Node propertyNode)
Defines a property whose type is inferred.

Parameters:
propertyName - the property's name
type - the type
propertyNode - the node corresponding to the inferred definition of property that might later be accessed using getPropertyNode.

removeProperty

public boolean removeProperty(String propertyName)
Removes the declared or inferred property from this ObjectType.

Parameters:
propertyName - 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)
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.

Parameters:
propertyName - the name of the property
Returns:
the Node corresponding to the property or null.

getOwnPropertyJSDocInfo

public JSDocInfo getOwnPropertyJSDocInfo(String propertyName)
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.


setPropertyJSDocInfo

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

Parameters:
info - JSDocInfo for the property definition. May be null.

findPropertyType

public JSType findPropertyType(String propertyName)
Description copied from class: JSType
Coerces this type to an Object type, then gets the type of the property whose name is given. Unlike getPropertyType(java.lang.String), returns null if the property is not found.

Overrides:
findPropertyType in class JSType
Returns:
The property's type. null if the current type cannot have properties, or if the type is not found.

getPropertyType

public abstract JSType getPropertyType(String propertyName)
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.

hasProperty

public abstract boolean hasProperty(String propertyName)
Checks whether the property whose name is given is present on the object.


hasOwnProperty

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


getOwnPropertyNames

public Set<String> getOwnPropertyNames()
Returns the names of all the properties directly on this type.


isPropertyTypeInferred

public abstract boolean isPropertyTypeInferred(String propertyName)
Checks whether the property's type is inferred.


isPropertyTypeDeclared

public abstract boolean isPropertyTypeDeclared(String propertyName)
Checks whether the property's type is declared.


isPropertyInExterns

public boolean isPropertyInExterns(String propertyName)
Checks whether the property was defined in the externs.


getPropertiesCount

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


getPropertyNames

public Set<String> getPropertyNames()
Returns a list of properties defined or inferred on this type and any of its supertypes.


visit

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

Specified by:
visit in class JSType
Returns:
the value returned by the visitor
See Also:
Visitor

getPossibleToBooleanOutcomes

public BooleanLiteralSet getPossibleToBooleanOutcomes()
Description copied from class: JSType
Computes the set of possible outcomes of the ToBoolean predicate for this type. The ToBoolean predicate is defined by the ECMA-262 standard, 3rd edition. Its behavior for simple types can be summarized by the following table:
typeresult
undefined{false}
null{false}
boolean{true, false}
number{true, false}
string{true, false}
Object{true}

Specified by:
getPossibleToBooleanOutcomes in class JSType
Returns:
the set of boolean literals for this type

isUnknownType

public boolean isUnknownType()
We treat this as the unknown type if any of its implicit prototype properties is unknown.

Overrides:
isUnknownType in class JSType

isObject

public boolean isObject()
Description copied from class: JSType
Tests whether this type is an Object, or any subtype thereof.

Overrides:
isObject in class JSType
Returns:
this &lt;: Object

hasCachedValues

public boolean hasCachedValues()
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.


clearCachedValues

public void clearCachedValues()
Clear cached values. Should be called before making changes to a prototype that may have been changed since creation.


isNativeObjectType

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


cast

public static ObjectType cast(JSType type)
A null-safe version of JSType#toObjectType.


isFunctionPrototypeType

public final boolean isFunctionPrototypeType()
Description copied from class: JSType
Whether this is the prototype of a function.

Overrides:
isFunctionPrototypeType in class JSType

getOwnerFunction

public FunctionType getOwnerFunction()
Gets the owner of this if it's a function prototype.


getCtorImplementedInterfaces

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


getCtorExtendedInterfaces

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