Class ObjectType
java.lang.Object
com.google.javascript.rhino.jstype.JSType
com.google.javascript.rhino.jstype.ObjectType
- All Implemented Interfaces:
StaticScope<JSType>,Serializable
- Direct Known Subclasses:
EnumElementType,EnumType,FunctionType,TemplateType,TemplatizedType,UnknownType
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:
-
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
Modifier and TypeMethodDescriptionstatic ObjectTypeA null-safe version of JSType#toObjectType.voidClear cached values.static StringcreateDelegateSuffix(String suffix) Creates a suffix for a proxy delegate.final booleandefineDeclaredProperty(String propertyName, JSType type, Node propertyNode) Defines a property whose type is explicitly declared by the programmer.final booleandefineInferredProperty(String propertyName, JSType type, Node propertyNode) Defines a property whose type is inferred.final booleandefineSynthesizedProperty(String propertyName, JSType type, Node propertyNode) Defines a property whose type is on a synthesized object.findPropertyType(String propertyName) Coerces this type to an Object type, then gets the type of the property whose name is given.abstract FunctionTypeGets this object's constructor.Gets the interfaces extended by the interface associated with this type.Gets the interfaces implemented by the ctor associated with this type.Returns a user meaningful label for the JSType instance.abstract ObjectTypeGets the implicit prototype (a.k.a.Gets the docInfo for this type.Due to the complexity of some of our internal type systems, sometimes we have different types constructed by the same constructor.Gets the owner of this if it's a function prototype.getOwnPropertyJSDocInfo(String propertyName) Gets the docInfo on the specified property on this type.Returns the names of all the properties directly on this type.getOwnSlot(String name) LikegetSlotbut does not recurse into parent scopes.Returns the scope enclosing this one or null if none.Computes the set of possible outcomes of theToBooleanpredicate for this type.intGets the number of properties of this object.Returns a list of properties defined or inferred on this type and any of its supertypes.getPropertyNode(String propertyName) Gets the node corresponding to the definition of the specified property.getPropertyType(String propertyName) Gets the property type of the property whose name is given.abstract StringGets the reference name for this object.Returns the root node associated with this scope.Default getSlot implementation.com.google.common.collect.ImmutableList<JSType>Gets the declared default element type.Returns the expected type ofthisin the current scope.booleanReturns true if any cached values have been set for this type.booleanhasOwnProperty(String propertyName) Checks whether the property whose name is given is present directly on the object.booleanhasProperty(String propertyName) Checks whether the property is present on the object.booleanReturns true if the object is named.final booleanWhether this is the prototype of a function.booleanWhether this is a built-in object.booleanisObject()Tests whether this type is anObject, or any subtype thereof.booleanisPropertyInExterns(String propertyName) Checks whether the property was defined in the externs.booleanisPropertyTypeDeclared(String propertyName) Checks whether the property's type is declared.booleanisPropertyTypeInferred(String propertyName) Checks whether the property's type is inferred.booleanWe treat this as the unknown type if any of its implicit prototype properties is unknown.booleanremoveProperty(String propertyName) Removes the declared or inferred property from this ObjectType.voidsetJSDocInfo(JSDocInfo info) Sets the docInfo for this type from the givenJSDocInfo.voidsetPropertyJSDocInfo(String propertyName, JSDocInfo info) Sets the docInfo for the specified property from theJSDocInfoon its definition.testForEquality(JSType that) Comparesthisandthat.<T> TVisit this type with the given visitor.Methods inherited from class com.google.javascript.rhino.jstype.JSType
autobox, autoboxesTo, canBeCalled, canCastTo, canTestForEqualityWith, canTestForShallowEqualityWith, clearResolved, collapseUnion, dereference, differsFrom, equals, forceResolve, getGreatestSubtype, getLeastSupertype, getRestrictedTypeGivenToBooleanOutcome, getTemplateTypeMap, getTypesUnderEquality, getTypesUnderInequality, getTypesUnderShallowEquality, getTypesUnderShallowInequality, hasAnyTemplateTypes, hasDisplayName, hashCode, isAllType, isArrayType, isBooleanObjectType, isBooleanValueType, isCheckedUnknownType, isConstructor, isDateType, isDict, isEmptyType, isEnumElementType, isEnumType, isEquivalent, isEquivalentTo, isFunctionType, isGlobalThisType, isInstanceType, isInterface, isInvariant, isNominalConstructor, isNominalType, isNoObjectType, isNoResolvedType, isNoType, isNullable, isNullType, isNumber, isNumberObjectType, isNumberValueType, isOrdinaryFunction, isRecordType, isRegexpType, isResolved, isString, isStringObjectType, isStringValueType, isStruct, isSubtype, isTemplateType, isTemplatizedType, isUnionType, isVoidType, matchConstraint, matchesInt32Context, matchesNumberContext, matchesObjectContext, matchesStringContext, matchesUint32Context, resolve, restrictByNotNullOrUndefined, setValidator, toAnnotationString, toDebugHashCodeString, toMaybeEnumElementType, toMaybeEnumType, toMaybeFunctionType, toMaybeFunctionType, toMaybeTemplateType, toMaybeTemplateType, toMaybeTemplatizedType, toMaybeTemplatizedType, toMaybeUnionType, toObjectType, toString, unboxesTo
-
Method Details
-
getRootNode
Description copied from interface:StaticScopeReturns the root node associated with this scope. May be null.- Specified by:
getRootNodein interfaceStaticScope<JSType>
-
getParentScope
Description copied from interface:StaticScopeReturns the scope enclosing this one or null if none.- Specified by:
getParentScopein interfaceStaticScope<JSType>
-
getSlot
Default getSlot implementation. This gets overridden by FunctionType for lazily-resolved prototypes.- Specified by:
getSlotin interfaceStaticScope<JSType>- Parameters:
name- The name of the variable slot to look up.- Returns:
- The defined slot for the variable, or
nullif no definition exists.
-
getOwnSlot
Description copied from interface:StaticScopeLikegetSlotbut does not recurse into parent scopes.- Specified by:
getOwnSlotin interfaceStaticScope<JSType>
-
getTypeOfThis
Description copied from interface:StaticScopeReturns the expected type ofthisin the current scope.- Specified by:
getTypeOfThisin interfaceStaticScope<JSType>
-
getTemplateTypes
Gets the declared default element type.- See Also:
-
getJSDocInfo
Gets the docInfo for this type.- Overrides:
getJSDocInfoin classJSType
-
setJSDocInfo
-
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
nullif this is an anonymous object
-
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
Description copied from class:JSTypeReturns 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:
getDisplayNamein classJSType- Returns:
- the display name of the type, or null if one is not available
-
createDelegateSuffix
Creates a suffix for a proxy delegate.- See Also:
-
hasReferenceName
public boolean hasReferenceName()Returns true if the object is named.- Returns:
- true if the object is named, false if it is anonymous
-
testForEquality
Description copied from class:JSTypeComparesthisandthat.- Overrides:
testForEqualityin classJSType- Returns:
TernaryValue.TRUEif the comparison of values ofthistype andthatalways succeed (such asundefinedcompared tonull)TernaryValue.FALSEif the comparison of values ofthistype andthatalways fails (such asundefinedcompared tonumber)TernaryValue.UNKNOWNif the comparison can succeed or fail depending on the concrete values
-
getConstructor
Gets this object's constructor.- Returns:
- this object's constructor or
nullif it is a native object (constructed natively v.s. by instantiation of a function)
-
getImplicitPrototype
Gets the implicit prototype (a.k.a. the[[Prototype]]property). -
defineDeclaredProperty
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
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
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
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
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
Nodecorresponding to the property or null.
-
getOwnPropertyJSDocInfo
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. -
setPropertyJSDocInfo
Sets the docInfo for the specified property from theJSDocInfoon its definition.- Parameters:
info-JSDocInfofor the property definition. May benull.
-
findPropertyType
Description copied from class:JSTypeCoerces this type to an Object type, then gets the type of the property whose name is given. UnlikegetPropertyType(java.lang.String), returns null if the property is not found.- Overrides:
findPropertyTypein classJSType- Returns:
- The property's type.
nullif the current type cannot have properties, or if the type is not found.
-
getPropertyType
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.
-
hasProperty
Description copied from class:JSTypeChecks whether the property is present on the object.- Overrides:
hasPropertyin classJSType- Parameters:
propertyName- The property name.
-
hasOwnProperty
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
Returns the names of all the properties directly on this type. Overridden by FunctionType to add "prototype". -
isPropertyTypeInferred
Checks whether the property's type is inferred. -
isPropertyTypeDeclared
Checks whether the property's type is declared. -
isPropertyInExterns
Checks whether the property was defined in the externs. -
getPropertiesCount
public int getPropertiesCount()Gets the number of properties of this object. -
getPropertyNames
Returns a list of properties defined or inferred on this type and any of its supertypes. -
visit
Description copied from class:JSTypeVisit this type with the given visitor. -
getPossibleToBooleanOutcomes
Description copied from class:JSTypeComputes the set of possible outcomes of theToBooleanpredicate for this type. TheToBooleanpredicate is defined by the ECMA-262 standard, 3rd edition. Its behavior for simple types can be summarized by the following table:type result undefined{false} null{false} boolean{true, false} number{true, false} string{true, false} Object{true} - Specified by:
getPossibleToBooleanOutcomesin 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:
isUnknownTypein classJSType
-
isObject
public boolean isObject()Description copied from class:JSTypeTests 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. -
cast
A null-safe version of JSType#toObjectType. -
isFunctionPrototypeType
public final boolean isFunctionPrototypeType()Description copied from class:JSTypeWhether this is the prototype of a function.- Overrides:
isFunctionPrototypeTypein classJSType
-
getOwnerFunction
Gets the owner of this if it's a function prototype. -
getCtorImplementedInterfaces
Gets the interfaces implemented by the ctor associated with this type. Intended to be overridden by subclasses. -
getCtorExtendedInterfaces
Gets the interfaces extended by the interface associated with this type. Intended to be overridden by subclasses.
-