Class TypeDescriptor

java.lang.Object
org.graalvm.polyglot.tck.TypeDescriptor

public final class TypeDescriptor extends Object
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 Details

    • NULL

      public static final TypeDescriptor NULL
      The NULL type represents a type of null or undefined value.
      Since:
      0.30
    • BOOLEAN

      public static final TypeDescriptor BOOLEAN
      Represents a boolean type.
      Since:
      0.30
    • NUMBER

      public static final TypeDescriptor NUMBER
      Represents a numeric type.
      Since:
      0.30
    • STRING

      public static final TypeDescriptor STRING
      Represents a string type.
      Since:
      0.30
    • ITERABLE

      public static final TypeDescriptor 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

      public static final TypeDescriptor 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

      public static final TypeDescriptor 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:
      • Value.hasHashEntries()
    • OBJECT

      public static final TypeDescriptor OBJECT
      Represents an object created by a guest language.
      Since:
      0.30
    • ARRAY

      public static final TypeDescriptor 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

      public static final TypeDescriptor HOST_OBJECT
      Represents a host object.
      Since:
      0.30
    • NATIVE_POINTER

      public static final TypeDescriptor NATIVE_POINTER
      Represents a native pointer.
      Since:
      0.30
    • DATE

      public static final TypeDescriptor DATE
      Type descriptor for date.
      Since:
      20.0
    • TIME

      public static final TypeDescriptor TIME
      Type descriptor for time.
      Since:
      20.0
    • TIME_ZONE

      public static final TypeDescriptor TIME_ZONE
      Type descriptor for time zone.
      Since:
      20.0
    • DURATION

      public static final TypeDescriptor DURATION
      Type descriptor for duration.
      Since:
      20.0
    • META_OBJECT

      public static final TypeDescriptor META_OBJECT
      Type descriptor for metaobjects.
      Since:
      20.0
    • EXCEPTION

      public static final TypeDescriptor EXCEPTION
      Type descriptor for exception.
      Since:
      19.3
      See Also:
      • Value.isException()
    • EXECUTABLE

      public static final TypeDescriptor 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 use executable(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

      public static final TypeDescriptor 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 use executable(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

      public static final TypeDescriptor INSTANTIABLE
      Represents an instantiable type accepting any number of parameters of any type. To create an instantiable type with concrete parameter types use instantiable(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

      public static final TypeDescriptor 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 use instantiable(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

      public static final TypeDescriptor ANY
      Represents all types. It's an intersection of no type.
      Since:
      0.30
  • Method Details

    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
      Since:
      0.30
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
      Since:
      0.30
    • toString

      public String toString()
      Overrides:
      toString in class Object
      Since:
      0.30
    • isAssignable

      public boolean isAssignable(TypeDescriptor fromType)
      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 this TypeDescriptor represent an union type.
      Returns:
      true if this type represents an union type
      Since:
      0.30
    • isIntersection

      public boolean isIntersection()
      Checks if this TypeDescriptor represent an intersection type.
      Returns:
      true if this type represents an intersection type
      Since:
      0.30
    • union

      public static TypeDescriptor union(TypeDescriptor... types)
      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

      public static TypeDescriptor intersection(TypeDescriptor... types)
      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 to ANY and 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

      public TypeDescriptor subtract(TypeDescriptor toRemove)
      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 toRemove type represent the same types then no type is returned.
      • If this type represents a union type the toRemove type is removed from the union type. When the toRemove type is also a union type then all types in the toRemove union type are removed.
      • If this type represents an intersection type the toRemove type is removed from the intersection type. When the toRemove type is also an intersection type then all types in the toRemove intersection type are removed.
      • If this and toRemove types 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
    • array

      public static TypeDescriptor array(TypeDescriptor componentType)
      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

      public static TypeDescriptor iterable(TypeDescriptor componentType)
      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

      public static TypeDescriptor iterator(TypeDescriptor componentType)
      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

      public static TypeDescriptor hash(TypeDescriptor keyType, TypeDescriptor valueType)
      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 type
      parameterTypes - 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 type
      vararg - the executable has variable length arguments or ignores additional parameters. For executables created by the LanguageProvider.createValueConstructors(org.graalvm.polyglot.Context) set to false if 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 type
      vararg - the instantiable has variable length arguments or ignores additional parameters. For instantiables created by the LanguageProvider.createValueConstructors(org.graalvm.polyglot.Context) set to false if 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

      public static TypeDescriptor forValue(org.graalvm.polyglot.Value value)
      Creates a type for given Value.
      Parameters:
      value - the value to create TypeDescriptor for
      Returns:
      the type of value, may by an union type containing more primitive or array types.
      Since:
      0.30