com.google.javascript.rhino.jstype
Class EnumType

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.EnumType
All Implemented Interfaces:
StaticScope<JSType>, Serializable

public class EnumType
extends ObjectType

An enum type representing a branded collection of elements. Each element is referenced by its name, and has an EnumElementType type.

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.
 boolean defineElement(String name, Node definingNode)
          Defines a new element on this enum.
 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.
 Set<String> getElements()
          Gets the elements defined on this enum.
 EnumElementType getElementsType()
          Gets the elements' type.
 ObjectType getImplicitPrototype()
          Gets the implicit prototype (a.k.a.
 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.
 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 property)
          Gets the property type of the property whose name is given.
 String getReferenceName()
          Gets the reference name for this object.
 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 enum.
 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.
 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 isNativeObjectType()
          Whether this is a built-in object.
 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 isSubtype(JSType that)
          Checks whether this is a subtype of that.
 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 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.
 EnumType toMaybeEnumType()
          Downcasts this to an EnumType, or returns null if this is not an EnumType.
 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, clearCachedValues, createDelegateSuffix, defineDeclaredProperty, defineInferredProperty, findPropertyType, getIndexType, getJSDocInfo, getNormalizedReferenceName, getOwnSlot, getParameterType, getParentScope, getPossibleToBooleanOutcomes, getPropertyNames, getRootNode, getTypeOfThis, isFunctionPrototypeType, isObject, isUnknownType, setJSDocInfo
 
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, 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, isTemplateType, isUnionType, isVoidType, matchesInt32Context, matchesUint32Context, resolve, restrictByNotNullOrUndefined, setValidator, toAnnotationString, toDebugHashCodeString, toMaybeEnumElementType, toMaybeFunctionType, toMaybeFunctionType, toMaybeUnionType, toObjectType, toString
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Method Detail

getSource

public Node getSource()
Gets the source node or null if this is an unknown enum.


toMaybeEnumType

public EnumType toMaybeEnumType()
Description copied from class: JSType
Downcasts this to an EnumType, or returns null if this is not an EnumType.

Overrides:
toMaybeEnumType in class JSType

getImplicitPrototype

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


getElements

public Set<String> getElements()
Gets the elements defined on this enum.

Returns:
the elements' names defined on this enum. The returned set is immutable.

defineElement

public boolean defineElement(String name,
                             Node definingNode)
Defines a new element on this enum.

Parameters:
name - the name of the new element
definingNode - the Node that defines this new element
Returns:
true iff the new element is added successfully

getElementsType

public EnumElementType getElementsType()
Gets the elements' type.


testForEquality

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

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

isSubtype

public boolean isSubtype(JSType that)
Description copied from class: JSType
Checks whether this is a subtype of that.

Subtyping rules:

Returns:
this &lt;: that

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 ObjectType
Returns:
the display name of the type, or null if one is not available

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

getConstructor

public FunctionType getConstructor()
Description copied from class: ObjectType
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)

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.


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.


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.


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>
Specified by:
getSlot in class ObjectType
Parameters:
name - The name of the variable slot to look up.
Returns:
The defined slot for the variable, or null if no definition exists.

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

getOwnPropertyNames

public Set<String> getOwnPropertyNames()
Description copied from class: ObjectType
Returns the names of all the properties directly on this type.

Overrides:
getOwnPropertyNames 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

getPropertyType

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

Specified by:
getPropertyType in class ObjectType
Returns:
the property's type or UnknownType. This method never returns null.

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.

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.

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.

Overrides:
canBeCalled in class JSType
Returns:
true if this type might be callable.

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

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.

Overrides:
hasCachedValues in class ObjectType

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