Class JavaType<T>

java.lang.Object
org.pkl.config.java.JavaType<T>
Type Parameters:
T - the type reified by this JavaType instance

public class JavaType<T> extends Object
Runtime representation of a possibly parameterized Java type. Factory methods are provided to ease construction of commonly used Java standard library types. For example, a JavaType for List<String> can be constructed using JavaType.listOf(String.class).

Parameterizations of other types can be constructed using the super type token idiom:


 class Mapping<T> {} // some user-defined type
 Config config = ...

 Mapping<String> container = config.as(
   // construct super type token for Mapping<String>
   new JavaType<Mapping<String>>() {}
 );
 
  • Constructor Details

    • JavaType

      protected JavaType()
  • Method Details

    • of

      public static <T> JavaType<T> of(Class<T> type)
      Creates a JavaType for the given Class.
    • of

      public static <T> JavaType<T> of(Type type)
      Creates a JavaType for the given Type.

      Note: This method is not type safe, and should be used with care.

    • optionalOf

      public static <E> JavaType<Optional<E>> optionalOf(Class<E> elementType)
      Creates an Optional type with the given element type.
    • optionalOf

      public static <E> JavaType<Optional<E>> optionalOf(JavaType<E> elementType)
      Creates an Optional type with the given element type.
    • pairOf

      public static <F, S> JavaType<org.pkl.core.Pair<F,S>> pairOf(Class<F> firstType, Class<S> secondType)
      Creates a Pair type with the given first and second element types.
    • pairOf

      public static <F, S> JavaType<org.pkl.core.Pair<F,S>> pairOf(JavaType<F> firstType, JavaType<S> secondType)
      Creates a Pair type with the given first and second element types.
    • arrayOf

      public static <E> JavaType<E[]> arrayOf(Class<E> elementType)
      Creates an array type with the given element type.
    • arrayOf

      public static <E> JavaType<E[]> arrayOf(JavaType<E> elementType)
      Creates an array type with the given element type.
    • iterableOf

      public static <E> JavaType<Iterable<E>> iterableOf(Class<E> elementType)
      Creates an Iterable type with the given element type.
    • iterableOf

      public static <E> JavaType<Iterable<E>> iterableOf(JavaType<E> elementType)
      Creates an Iterable type with the given element type.
    • collectionOf

      public static <E> JavaType<Collection<E>> collectionOf(Class<E> elementType)
      Creates a Collection type with the given element type.
    • collectionOf

      public static <E> JavaType<Collection<E>> collectionOf(JavaType<E> elementType)
      Creates a Collection type with the given element type.
    • listOf

      public static <E> JavaType<List<E>> listOf(Class<E> elementType)
      Creates a List type with the given element type.
    • listOf

      public static <E> JavaType<List<E>> listOf(JavaType<E> elementType)
      Creates a List type with the given element type.
    • setOf

      public static <E> JavaType<Set<E>> setOf(Class<E> elementType)
      Creates a Set type with the given element type.
    • setOf

      public static <E> JavaType<Set<E>> setOf(JavaType<E> elementType)
      Creates a Set type with the given element type.
    • mapOf

      public static <K, V> JavaType<Map<K,V>> mapOf(Class<K> keyType, Class<V> valueType)
      Creates a Map type with the given key and value types.
    • mapOf

      public static <K, V> JavaType<Map<K,V>> mapOf(JavaType<K> keyType, JavaType<V> valueType)
      Creates a Map type with the given key and value types.
    • getType

      public Type getType()
      Returns the underlying Type.
    • equals

      public boolean equals(@Nullable @Nullable Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object