Package org.graalvm.polyglot.tck
Class TypeDescriptor
java.lang.Object
org.graalvm.polyglot.tck.TypeDescriptor
Represents a type of a polyglot value. Types include primitive types, null type, object type,
array type with an optional content type and union type.
- Since:
- 0.30
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final TypeDescriptorRepresents all types.static final TypeDescriptorRepresents an array with any content type.static final TypeDescriptorRepresents a boolean type.static final TypeDescriptorType descriptor for date.static final TypeDescriptorType descriptor for duration.static final TypeDescriptorType descriptor for exception.static final TypeDescriptorRepresents an executable type returning any type and accepting any number of parameters of any type.static final TypeDescriptorRepresents a raw executable type.static final TypeDescriptorRepresents a hash map with any key type and any value type.static final TypeDescriptorRepresents a host object.static final TypeDescriptorRepresents an instantiable type accepting any number of parameters of any type.static final TypeDescriptorRepresents a raw instantiable type.static final TypeDescriptorRepresents an iterable with any content type.static final TypeDescriptorRepresents an iterator with any content type.static final TypeDescriptorType descriptor for metaobjects.static final TypeDescriptorRepresents a native pointer.static final TypeDescriptorThe NULL type represents a type of null or undefined value.static final TypeDescriptorRepresents a numeric type.static final TypeDescriptorRepresents an object created by a guest language.static final TypeDescriptorRepresents a string type.static final TypeDescriptorType descriptor for time.static final TypeDescriptorType descriptor for time zone. -
Method Summary
Modifier and TypeMethodDescriptionstatic TypeDescriptorarray(TypeDescriptor componentType) Creates a new array type with given component type.booleanstatic TypeDescriptorexecutable(TypeDescriptor returnType, boolean vararg, TypeDescriptor... parameterTypes) Creates a new executable type with a given return type and parameter types.static TypeDescriptorexecutable(TypeDescriptor returnType, TypeDescriptor... parameterTypes) Creates a new executable type with a given return type and parameter types.static TypeDescriptorforValue(org.graalvm.polyglot.Value value) Creates a type for givenValue.static TypeDescriptorhash(TypeDescriptor keyType, TypeDescriptor valueType) Creates a new hash map type with given key type and value type.inthashCode()static TypeDescriptorinstantiable(TypeDescriptor instanceType, boolean vararg, TypeDescriptor... parameterTypes) Creates a new instantiable type with a given parameter types.static TypeDescriptorintersection(TypeDescriptor... types) Creates a new intersection type.booleanisAssignable(TypeDescriptor fromType) Checks if the given type is assignable to this type.booleanChecks if thisTypeDescriptorrepresent an intersection type.booleanisUnion()Checks if thisTypeDescriptorrepresent an union type.static TypeDescriptoriterable(TypeDescriptor componentType) Creates a new iterable type with given component type.static TypeDescriptoriterator(TypeDescriptor componentType) Creates a new iterator type with given component type.subtract(TypeDescriptor toRemove) Creates a new type by removing the given type from this type.toString()static TypeDescriptorunion(TypeDescriptor... types) Creates a new union type.
-
Field Details
-
NULL
The NULL type represents a type of null or undefined value.- Since:
- 0.30
-
BOOLEAN
Represents a boolean type.- Since:
- 0.30
-
NUMBER
Represents a numeric type.- Since:
- 0.30
-
STRING
Represents a string type.- Since:
- 0.30
-
ITERABLE
Represents an iterable with any content type. Any iterable type, including those with content type, is assignable to this type. This iterable type is not assignable to any iterable type having a content type.- Since:
- 21.1
-
ITERATOR
Represents an iterator with any content type. Any iterator type, including those with content type, is assignable to this type. This iterator type is not assignable to any iterator type having a content type.- Since:
- 21.1
-
HASH
Represents a hash map with any key type and any value type. Any hash map type, including those with specified key and value types, is assignable to this type. This hash map type is not assignable to any hash map type with specified key or value types.- Since:
- 21.1
- See Also:
-
OBJECT
Represents an object created by a guest language.- Since:
- 0.30
-
ARRAY
Represents an array with any content type. Any array type, including those with content type, is assignable to this type. This array type is not assignable to any array type having a content type.- Since:
- 0.30
-
HOST_OBJECT
Represents a host object.- Since:
- 0.30
-
NATIVE_POINTER
Represents a native pointer.- Since:
- 0.30
-
DATE
Type descriptor for date.- Since:
- 20.0
-
TIME
Type descriptor for time.- Since:
- 20.0
-
TIME_ZONE
Type descriptor for time zone.- Since:
- 20.0
-
DURATION
Type descriptor for duration.- Since:
- 20.0
-
META_OBJECT
Type descriptor for metaobjects.- Since:
- 20.0
-
EXCEPTION
Type descriptor for exception.- Since:
- 19.3
- See Also:
-
EXECUTABLE
Represents an executable type returning any type and accepting any number of parameters of any type. To create an executable type with concrete types useexecutable(org.graalvm.polyglot.tck.TypeDescriptor, org.graalvm.polyglot.tck.TypeDescriptor...). This type can be used for creating value constructors but should not be used for specifying expressions or statements parameter types as no other executable is assignable to it.The JavaScript sample usage for no argument function constructor:
@Override public Collection<? extends Snippet> createValueConstructors(Context context) { return Collections.singleton(Snippet.newBuilder( "function", context.eval("js", "(function(){ return function(){}})"), TypeDescriptor.EXECUTABLE).build()); }- Since:
- 0.30
-
EXECUTABLE_ANY
Represents a raw executable type. Any executable can be assigned into the raw executable type, but the raw executable type cannot be assigned to any other executable. To create an executable type with concrete types useexecutable(org.graalvm.polyglot.tck.TypeDescriptor, org.graalvm.polyglot.tck.TypeDescriptor...). This type can be used for specifying expressions or statements parameter types when the passed executable is actually not invoked.@Override public Collection<? extends Snippet> createExpressions(Context context) { final Collection<Snippet> expressions = new ArrayList<>(); final TypeDescriptor numeric = TypeDescriptor.union( TypeDescriptor.NUMBER, TypeDescriptor.BOOLEAN); final TypeDescriptor nonNumeric = TypeDescriptor.union( TypeDescriptor.STRING, TypeDescriptor.OBJECT, TypeDescriptor.ARRAY, TypeDescriptor.EXECUTABLE_ANY); Snippet.Builder builder = Snippet.newBuilder( "+", context.eval("js", "(function (a, b){ return a + b;})"), TypeDescriptor.NUMBER).parameterTypes(numeric, numeric); expressions.add(builder.build()); builder = Snippet.newBuilder( "+", context.eval("js", "(function (a, b){ return a + b;})"), TypeDescriptor.STRING).parameterTypes(nonNumeric, TypeDescriptor.ANY); expressions.add(builder.build()); builder = Snippet.newBuilder( "+", context.eval("js", "(function (a, b){ return a + b;})"), TypeDescriptor.STRING).parameterTypes(TypeDescriptor.ANY, nonNumeric); expressions.add(builder.build()); return expressions; }- Since:
- 19.0
- See Also:
-
INSTANTIABLE
Represents an instantiable type accepting any number of parameters of any type. To create an instantiable type with concrete parameter types useinstantiable(org.graalvm.polyglot.tck.TypeDescriptor, boolean, org.graalvm.polyglot.tck.TypeDescriptor...). This type can be used for creating value constructors but should not be used for specifying expressions or statements parameter types as no other instantiable is assignable to it.- Since:
- 19.0
-
INSTANTIABLE_ANY
Represents a raw instantiable type. Any instantiable can be assigned into this raw instantiable type, but the raw instantiable type cannot be assigned to any other instantiable. To create an instantiable type with concrete types useinstantiable(org.graalvm.polyglot.tck.TypeDescriptor, boolean, org.graalvm.polyglot.tck.TypeDescriptor...). This type can be used for specifying expressions or statements parameter types when the passed instantiable is actually not invoked.- Since:
- 19.0
- See Also:
-
ANY
Represents all types. It's an intersection of no type.- Since:
- 0.30
-
-
Method Details
-
hashCode
public int hashCode() -
equals
-
toString
-
isAssignable
Checks if the given type is assignable to this type. The primitive types are assignable only to itself or to an union type containing the given primitive type. The array type with a component type can be assigned to itself, to an array type without a component type and to an union type containing the given array type or an array type without a component type. The union type can be assigned to other union type containing all union elements. The intersection type can be assigned to type having any intersection type. To the target intersection type only an intersection type having all the target intersection elements can be assigned.- Parameters:
fromType- the type to assign- Returns:
- true if the fromType is assignable to this type
- Since:
- 0.30
-
isUnion
public boolean isUnion()Checks if thisTypeDescriptorrepresent an union type.- Returns:
- true if this type represents an union type
- Since:
- 0.30
-
isIntersection
public boolean isIntersection()Checks if thisTypeDescriptorrepresent an intersection type.- Returns:
- true if this type represents an intersection type
- Since:
- 0.30
-
union
Creates a new union type. The union type is any of the given types.- Parameters:
types- the types to include in the union- Returns:
- the union type containing the given types
- Since:
- 0.30
-
intersection
Creates a new intersection type. The intersection type is all of the given types. The intersection can be also used to create a no type. The no type is a type which has no other specialized type. The no type can be assigned toANYand itself. The no type is created as an empty intersection,TypeDescriptor.intersection().- Parameters:
types- the types to include in the intersection- Returns:
- the intersection type containing the given types
- Since:
- 0.30
-
subtract
Creates a new type by removing the given type from this type. The type subtraction works in the following way:- If this type and the
toRemovetype represent the same types then no type is returned. - If this type represents a
uniontype thetoRemovetype is removed from the union type. When thetoRemovetype is also a union type then all types in thetoRemoveunion type are removed. - If this type represents an
intersectiontype thetoRemovetype is removed from the intersection type. When thetoRemovetype is also an intersection type then all types in thetoRemoveintersection type are removed. - If this and
toRemovetypes are parameterized types (array, iterable, iterator, hash) of the same kind the type parameter types are subtracted applying the same rules.
Examples:
- NUMBER.subtract(NUMBER) -> no type
- NUMBER.subtract(STRING) -> NUMBER
- UNION[NUMBER|STRING|OBJECT].subtract(NUMBER) -> UNION[STRING|OBJECT]
- UNION[NUMBER|STRING|OBJECT].subtract(UNION[NUMBER|STRING]) -> OBJECT
- INTERSECTION[NUMBER&STRING&OBJECT].subtract(NUMBER) -> INTERSECTION[STRING&OBJECT]
- INTERSECTION[NUMBER&STRING&OBJECT].subtract(INTERSECTION[NUMBER&STRING]) -> OBJECT
- ARRAY[UNION[NUMBER|STRING]].subtract(ARRAY[NUMBER]) -> ARRAY[STRING]
- ARRAY[INTERSECTION[NUMBER|STRING]].subtract(ARRAY[NUMBER]) -> ARRAY[STRING]
- Parameters:
toRemove- the type to remove.- Since:
- 20.2
- If this type and the
-
array
Creates a new array type with given component type. To create a multi-dimensional array use an array type as a component type.- Parameters:
componentType- the required component type.- Returns:
- an array type with given component
- Since:
- 0.30
-
iterable
Creates a new iterable type with given component type.- Parameters:
componentType- the required component type.- Returns:
- an iterable type with given component
- Since:
- 21.1
-
iterator
Creates a new iterator type with given component type.- Parameters:
componentType- the required component type.- Returns:
- an iterator type with given component
- Since:
- 21.1
-
hash
Creates a new hash map type with given key type and value type.- Parameters:
keyType- the required key type.valueType- the required value type.- Returns:
- a new hash map type with given key and value types
- Since:
- 21.1
-
executable
public static TypeDescriptor executable(TypeDescriptor returnType, TypeDescriptor... parameterTypes) Creates a new executable type with a given return type and parameter types.- Parameters:
returnType- the required return type, use ANY as any typeparameterTypes- the required parameter types- Returns:
- an executable type
- Since:
- 0.30
-
executable
public static TypeDescriptor executable(TypeDescriptor returnType, boolean vararg, TypeDescriptor... parameterTypes) Creates a new executable type with a given return type and parameter types.- Parameters:
returnType- the required return type, use ANY as any typevararg- the executable has variable length arguments or ignores additional parameters. For executables created by theLanguageProvider.createValueConstructors(org.graalvm.polyglot.Context)set tofalseif the language neither ignores extra parameters nor the executable has variable arguments length.parameterTypes- the required parameter types- Returns:
- an executable type
- Since:
- 0.31
-
instantiable
public static TypeDescriptor instantiable(TypeDescriptor instanceType, boolean vararg, TypeDescriptor... parameterTypes) Creates a new instantiable type with a given parameter types.- Parameters:
instanceType- the type of an instance, use ANY as any typevararg- the instantiable has variable length arguments or ignores additional parameters. For instantiables created by theLanguageProvider.createValueConstructors(org.graalvm.polyglot.Context)set tofalseif the language neither ignores extra parameters nor the instantiable has variable arguments length.parameterTypes- the required parameter types- Returns:
- an instantiable type
- Since:
- 19.0
-
forValue
Creates a type for givenValue.- Parameters:
value- the value to createTypeDescriptorfor- Returns:
- the type of value, may by an union type containing more primitive or array types.
- Since:
- 0.30
-