Class ObjectType
- java.lang.Object
-
- com.google.javascript.rhino.jstype.JSType
-
- com.google.javascript.rhino.jstype.ObjectType
-
- All Implemented Interfaces:
java.io.Serializable
- Direct Known Subclasses:
EnumElementType
,PrototypeObjectType
,ProxyObjectType
,UnknownType
public abstract class ObjectType extends JSType implements java.io.Serializable
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
-
Nested classes/interfaces inherited from class com.google.javascript.rhino.jstype.JSType
JSType.HasPropertyKind, JSType.Nullability, JSType.SubtypingMode, JSType.TypePair
-
-
Field Summary
-
Fields inherited from class com.google.javascript.rhino.jstype.JSType
templateTypeMap
-
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description static ObjectType
cast(JSType type)
A null-safe version of JSType#toObjectType.void
clearCachedValues()
Clear cached values.static java.lang.String
createDelegateSuffix(java.lang.String suffix)
Creates a suffix for a proxy delegate.boolean
defineDeclaredProperty(java.lang.String propertyName, JSType type, Node propertyNode)
Defines a property whose type is explicitly declared by the programmer.boolean
defineInferredProperty(java.lang.String propertyName, JSType type, Node propertyNode)
Defines a property whose type is inferred.boolean
defineSynthesizedProperty(java.lang.String propertyName, JSType type, Node propertyNode)
Defines a property whose type is on a synthesized object.Property.OwnedProperty
findClosestDefinition(java.lang.String propertyName)
Returns the closest definition of the property including this type itself.protected JSType
findPropertyTypeWithoutConsideringTemplateTypes(java.lang.String propertyName)
Looks up a property on this type, but without properly replacing any templates in the result.ObjectType
getClosestDefiningType(java.lang.String propertyName)
Returns the closest ancestor that defines the property including this type itself.abstract FunctionType
getConstructor()
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.String
getDisplayName()
Returns a user meaningful label for the JSType instance.JSType
getEnumeratedTypeOfEnumObject()
abstract ObjectType
getImplicitPrototype()
Gets the implicit prototype (a.k.a.java.lang.Iterable<ObjectType>
getImplicitPrototypeChain()
Returns a lazy, dynamicIterable
for the types forming the implicit prototype chain of this type.JSDocInfo
getJSDocInfo()
Gets the docInfo for this type.java.lang.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()
Node
getOwnPropertyDefSite(java.lang.String propertyName)
JSDocInfo
getOwnPropertyJSDocInfo(java.lang.String propertyName)
Gets the docInfo on the specified property on this type.JSType.HasPropertyKind
getOwnPropertyKind(java.lang.String propertyName)
Checks whether the property whose name is given is present directly on the object.java.util.Set<java.lang.String>
getOwnPropertyNames()
Returns the names of all the properties directly on this type.Property
getOwnSlot(java.lang.String name)
BooleanLiteralSet
getPossibleToBooleanOutcomes()
Computes the set of possible outcomes of theToBoolean
predicate for this type.int
getPropertiesCount()
Gets the number of properties of this object.Node
getPropertyDefSite(java.lang.String propertyName)
JSDocInfo
getPropertyJSDocInfo(java.lang.String propertyName)
JSType.HasPropertyKind
getPropertyKind(java.lang.String propertyName, boolean autobox)
Checks whether the property is present on the object.com.google.common.collect.ImmutableSortedSet<java.lang.String>
getPropertyNames()
Returns a list of properties defined or inferred on this type and any of its supertypes.Node
getPropertyNode(java.lang.String propertyName)
Gets the node corresponding to the definition of the specified property.JSType
getPropertyType(java.lang.String propertyName)
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 an object typeObjectType
getRawType()
abstract java.lang.String
getReferenceName()
Gets the reference name for this object.Property
getSlot(java.lang.String name)
Default getSlot implementation.FunctionType
getSuperClassConstructor()
com.google.common.collect.ImmutableList<JSType>
getTemplateTypes()
Gets the declared default element type.ObjectType
getTopMostDefiningType(java.lang.String propertyName)
Returns the top most type that defines the property.JSType
getTypeOfThis()
boolean
hasCachedValues()
Returns true if any cached values have been set for this type.boolean
hasOwnProperty(java.lang.String propertyName)
Checks whether the property whose name is given is present directly on the object.boolean
hasReferenceName()
INVARIANT:hasReferenceName()
is true if and only ifgetReferenceName()
returns a non-null string.boolean
isAmbiguousObject()
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 anObject
, or any subtype thereof.boolean
isPropertyInExterns(java.lang.String propertyName)
Checks whether the property was defined in the externs.boolean
isPropertyTypeDeclared(java.lang.String propertyName)
Checks whether the property's type is declared.boolean
isPropertyTypeInferred(java.lang.String propertyName)
Checks whether the property's type is inferred.boolean
isStructuralType()
boolean
isUnknownType()
We treat this as the unknown type if any of its implicit prototype properties is unknown.boolean
removeProperty(java.lang.String propertyName)
Removes the declared or inferred property from this ObjectType.void
setJSDocInfo(JSDocInfo info)
Sets the docInfo for this type from the givenJSDocInfo
.void
setPropertyJSDocInfo(java.lang.String propertyName, JSDocInfo info)
Sets the docInfo for the specified property from theJSDocInfo
on its definition.void
setPropertyNode(java.lang.String propertyName, Node defSite)
Sets the node where the property was defined.TernaryValue
testForEquality(JSType that)
Comparesthis
andthat
.<T> T
visit(Visitor<T> visitor)
Visit this type with the given visitor.-
Methods inherited from class com.google.javascript.rhino.jstype.JSType
areIdentical, assertFunctionType, assertObjectType, autobox, autoboxesTo, canBeCalled, canCastTo, canTestForEqualityWith, canTestForShallowEqualityWith, collapseUnion, containsReferenceAncestor, dereference, differsFrom, equals, findPropertyType, getEnumeratedTypeOfEnumElement, getGreatestSubtype, getGreatestSubtypeWithProperty, getLeastSupertype, getPropertyKind, getRestrictedTypeGivenOutcome, getTemplateParamCount, getTemplateTypeMap, getTypeParameters, getTypesUnderEquality, getTypesUnderInequality, getTypesUnderShallowEquality, getTypesUnderShallowInequality, getUnionMembers, hasAnyTemplateTypes, hasDisplayName, hashCode, hasProperty, isAllType, isArrayType, isBigIntObjectType, isBigIntOrNumber, isBigIntValueType, isBooleanObjectType, isBooleanValueType, isBoxableScalar, isCheckedUnknownType, isConstructor, isDateType, isDict, isEmptyType, isEnumElementType, isEnumType, isExplicitlyVoidable, isFunctionType, isGlobalThisType, isInstanceType, isInterface, isLiteralObject, isNamedType, isNominalConstructor, isNominalType, isNoObjectType, isNoResolvedType, isNoType, isNullable, isNullType, isNumber, isNumberObjectType, isNumberValueType, isObjectType, isOnlyBigInt, isOrdinaryFunction, isRawTypeOfTemplatizedType, isRecordType, isRegexpType, isResolved, isSomeUnknownType, isString, isStringObjectType, isStringValueType, isStruct, isStructuralInterface, isSubtype, isSubtype, isSubtypeOf, isSubtypeOf, isSubtypeWithoutStructuralTyping, isSuccessfullyResolved, isSymbol, isSymbolObjectType, isSymbolValueType, isTemplateType, isTemplatizedType, isUnionType, isUnresolved, isUnresolvedOrResolvedUnknown, isUnsuccessfullyResolved, isVoidable, isVoidType, loosenTypecheckingDueToForwardReferencedSupertype, matchConstraint, matchesNumberContext, matchesObjectContext, matchesStringContext, matchesSymbolContext, mergeSupertypeTemplateTypes, resolve, restrictByNotNull, restrictByNotNullOrUndefined, restrictByNotUndefined, setValidator, toAnnotationString, toMaybeEnumElementType, toMaybeEnumType, toMaybeFunctionType, toMaybeFunctionType, toMaybeNamedType, toMaybeObjectType, toMaybeRecordType, toMaybeTemplateType, toMaybeTemplatizedType, toMaybeUnionType, toObjectType, toString, unboxesTo
-
-
-
-
Method Detail
-
getSlot
public Property getSlot(java.lang.String name)
Default getSlot implementation. This gets overridden by FunctionType for lazily-resolved prototypes.
-
getOwnSlot
public final Property getOwnSlot(java.lang.String name)
-
getTypeOfThis
public JSType getTypeOfThis()
-
getTemplateTypes
public com.google.common.collect.ImmutableList<JSType> getTemplateTypes()
Gets the declared default element type.- See Also:
TemplatizedType
-
getJSDocInfo
public JSDocInfo getJSDocInfo()
Gets the docInfo for this type.- Overrides:
getJSDocInfo
in classJSType
-
setJSDocInfo
public void setJSDocInfo(JSDocInfo info)
-
getReferenceName
@Nullable public abstract java.lang.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.Returning an empty string means something different than returning null. An empty string may indicate an anonymous constructor, which we treat differently than a literal type without a reference name. e.g. in
InstanceObjectType.appendTo(TypeStringBuilder)
- Returns:
- the object's name or
null
if this is an anonymous object
-
hasReferenceName
public final boolean hasReferenceName()
INVARIANT:hasReferenceName()
is true if and only ifgetReferenceName()
returns a non-null string.- Returns:
- true if the object is named, false if it is anonymous
-
getNormalizedReferenceName
@Nullable public final java.lang.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. Note that suffixes such as ".prototype" can be added after the delegate suffix, so anything after the parentheses must still be retained.
-
getDisplayName
public java.lang.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 classJSType
- Returns:
- the display name of the type, or null if one is not available
-
createDelegateSuffix
public static java.lang.String createDelegateSuffix(java.lang.String suffix)
Creates a suffix for a proxy delegate.- See Also:
getNormalizedReferenceName()
-
isAmbiguousObject
public boolean isAmbiguousObject()
-
getRawType
public final ObjectType getRawType()
-
testForEquality
public TernaryValue testForEquality(JSType that)
Description copied from class:JSType
Comparesthis
andthat
.- Overrides:
testForEquality
in classJSType
- Returns:
TernaryValue.TRUE
if the comparison of values ofthis
type andthat
always succeed (such asundefined
compared tonull
)TernaryValue.FALSE
if the comparison of values ofthis
type andthat
always fails (such asundefined
compared tonumber
)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)
-
getSuperClassConstructor
public FunctionType getSuperClassConstructor()
-
getTopMostDefiningType
public final ObjectType getTopMostDefiningType(java.lang.String propertyName)
Returns the top most type that defines the property.Note: if you are doing type validation, you are probably looking for the closest definition of the property which could be resolved by
getClosestDefiningType(java.lang.String)
.
-
getClosestDefiningType
public final ObjectType getClosestDefiningType(java.lang.String propertyName)
Returns the closest ancestor that defines the property including this type itself.
-
findClosestDefinition
public final Property.OwnedProperty findClosestDefinition(java.lang.String propertyName)
Returns the closest definition of the property including this type itself.
-
getImplicitPrototype
public abstract ObjectType getImplicitPrototype()
Gets the implicit prototype (a.k.a. the[[Prototype]]
property).
-
getImplicitPrototypeChain
public final java.lang.Iterable<ObjectType> getImplicitPrototypeChain()
Returns a lazy, dynamicIterable
for the types forming the implicit prototype chain of this type.The chain is iterated bottom to top; from the nearest ancestor to the most distant. Iteration stops when the next ancestor would be a
null
reference.The created
Iterator
s will not reflect changes to the prototype chain of elements it has already iterated past, but will reflect those of upcoming elements. Neither theIterable
nor itsIterator
support mutation.
-
defineDeclaredProperty
public final boolean defineDeclaredProperty(java.lang.String propertyName, JSType type, Node propertyNode)
Defines a property whose type is explicitly declared by the programmer.- Parameters:
propertyName
- the property's nametype
- the typepropertyNode
- the node corresponding to the declaration of property which might later be accessed usinggetPropertyNode
.
-
defineSynthesizedProperty
public final boolean defineSynthesizedProperty(java.lang.String propertyName, JSType type, Node propertyNode)
Defines a property whose type is on a synthesized object. These objects don't actually exist in the user's program. They're just used for bookkeeping in the type system.
-
defineInferredProperty
public final boolean defineInferredProperty(java.lang.String propertyName, JSType type, Node propertyNode)
Defines a property whose type is inferred.- Parameters:
propertyName
- the property's nametype
- the typepropertyNode
- the node corresponding to the inferred definition of property that might later be accessed usinggetPropertyNode
.
-
removeProperty
public boolean removeProperty(java.lang.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 final Node getPropertyNode(java.lang.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.
-
getPropertyDefSite
public final Node getPropertyDefSite(java.lang.String propertyName)
-
getPropertyJSDocInfo
public final JSDocInfo getPropertyJSDocInfo(java.lang.String propertyName)
-
getOwnPropertyJSDocInfo
public final JSDocInfo getOwnPropertyJSDocInfo(java.lang.String propertyName)
Gets the docInfo on the specified property on this type. This should not be implemented recursively, as you generally need to know exactly on which type in the prototype chain the JSDocInfo exists.
-
getOwnPropertyDefSite
public final Node getOwnPropertyDefSite(java.lang.String propertyName)
-
setPropertyJSDocInfo
public void setPropertyJSDocInfo(java.lang.String propertyName, JSDocInfo info)
Sets the docInfo for the specified property from theJSDocInfo
on its definition.- Parameters:
info
-JSDocInfo
for the property definition. May benull
.
-
setPropertyNode
public void setPropertyNode(java.lang.String propertyName, Node defSite)
Sets the node where the property was defined.
-
findPropertyTypeWithoutConsideringTemplateTypes
protected JSType findPropertyTypeWithoutConsideringTemplateTypes(java.lang.String propertyName)
Description copied from class:JSType
Looks up a property on this type, but without properly replacing any templates in the result.Subclasses can override this if they need more complicated logic for property lookup than just autoboxing to an object.
This is only for use by
findPropertyType(JSType)
. Call that method instead if you need to lookup a property on a random JSType- Overrides:
findPropertyTypeWithoutConsideringTemplateTypes
in classJSType
-
getPropertyType
public JSType getPropertyType(java.lang.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. This gets overridden by FunctionType for lazily-resolved call() and bind() functions.- Returns:
- the property's type or
UnknownType
. This method never returnsnull
.
-
getPropertyKind
public JSType.HasPropertyKind getPropertyKind(java.lang.String propertyName, boolean autobox)
Description copied from class:JSType
Checks whether the property is present on the object.- Overrides:
getPropertyKind
in classJSType
- Parameters:
propertyName
- The property name.autobox
- Whether to check for the presents on an autoboxed type
-
getOwnPropertyKind
public final JSType.HasPropertyKind getOwnPropertyKind(java.lang.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.
-
hasOwnProperty
public final boolean hasOwnProperty(java.lang.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 java.util.Set<java.lang.String> getOwnPropertyNames()
Returns the names of all the properties directly on this type. Overridden by FunctionType to add "prototype".
-
isPropertyTypeInferred
public final boolean isPropertyTypeInferred(java.lang.String propertyName)
Checks whether the property's type is inferred.
-
isPropertyTypeDeclared
public final boolean isPropertyTypeDeclared(java.lang.String propertyName)
Checks whether the property's type is declared.
-
isStructuralType
public boolean isStructuralType()
- Overrides:
isStructuralType
in classJSType
-
isPropertyInExterns
public final boolean isPropertyInExterns(java.lang.String propertyName)
Checks whether the property was defined in the externs.
-
getPropertiesCount
public final int getPropertiesCount()
Gets the number of properties of this object.
-
getPropertyNames
public final com.google.common.collect.ImmutableSortedSet<java.lang.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.
-
getPossibleToBooleanOutcomes
public BooleanLiteralSet getPossibleToBooleanOutcomes()
Description copied from class:JSType
Computes the set of possible outcomes of theToBoolean
predicate for this type. TheToBoolean
predicate is defined by the ECMA-262 standard, 3rd edition. Its behavior for simple types can be summarized by the following table:ToBoolean results by input type type result undefined
{false} null
{false} boolean
{true, false} number
{true, false} string
{true, false} Object
{true} - Specified by:
getPossibleToBooleanOutcomes
in classJSType
- 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 classJSType
-
isObject
public boolean isObject()
Description copied from class:JSType
Tests whether this type is anObject
, or any subtype thereof.
-
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.- Overrides:
isNativeObjectType
in classJSType
-
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 classJSType
-
getOwnerFunction
public FunctionType getOwnerFunction()
-
getCtorImplementedInterfaces
public java.lang.Iterable<ObjectType> getCtorImplementedInterfaces()
Gets the interfaces implemented by the ctor associated with this type. Intended to be overridden by subclasses.
-
getCtorExtendedInterfaces
public java.lang.Iterable<ObjectType> getCtorExtendedInterfaces()
Gets the interfaces extended by the interface associated with this type. Intended to be overridden by subclasses.
-
getPropertyTypeMap
public java.util.Map<java.lang.String,JSType> getPropertyTypeMap()
get the map of properties to types covered in an object type- Returns:
- a Map that maps the property's name to the property's type
-
getEnumeratedTypeOfEnumObject
public JSType getEnumeratedTypeOfEnumObject()
-
-