Class Reflection

java.lang.Object
org.pkl.config.java.mapper.Reflection

public final class Reflection extends Object
Reflection utilities for implementing ConverterFactorys. Mostly covers introspection of parameterized types, which is not covered by the java.util.reflect API.

The heavy lifting under the covers is done by the excellent ge(a)ntyref library.

  • Method Details

    • tryLoadClass

      @Nullable public static @Nullable Class<?> tryLoadClass(String qualifiedName)
      Returns the class with the given fully qualified name, or null if a class with the given name cannot be found.
    • isMissingTypeArguments

      public static boolean isMissingTypeArguments(Type type)
    • normalize

      public static Type normalize(Type type)
      Returns the normalized form of the given type. A normalized type is concrete (no wildcards) and instantiable (not an interface or abstract class).
    • toRawType

      public static Class<?> toRawType(Type type)
      Returns the raw (erased) type for the given parameterized type, type bound for the given wildcard type, or the given type otherwise.
    • toWrapperType

      public static <T> Class<T> toWrapperType(Class<T> type)
      Returns the wrapper type for the given primitive type. If the given type is not a primitive type, returns the given type.
    • getArrayElementType

      public static Type getArrayElementType(Type type)
      Returns the (possibly parameterized) element type for the given array type.
    • getExactSupertype

      public static Type getExactSupertype(Type type, Class<?> rawSupertype)
      Returns a parameterization of the given raw supertype, taking into account type arguments of the given subtype. For example, getExactSupertype(listOf(String.class), Collection.class) will return collectionOf(String.class). If the given subtype is not a parameterized type, returns the given raw supertype. If the given types have no inheritance relationship, returns null.
    • getExactSubtype

      public static Type getExactSubtype(Type type, Class<?> rawSubtype)
      Returns a parameterization of the given raw subtype, taking into account type arguments of the given supertype. For example, getExactSubtype(collectionOf(String.class), List.class) will return listOf(String.class). If the given supertype is not a parameterized type, returns the given raw subtype. If the given types have no inheritance relationship, returns null.
    • getExactParameterTypes

      public static Type[] getExactParameterTypes(Executable m, Type declaringType)
      Returns the exact parameter types of the given method or constructor, taking into account type arguments of the given declaring type. For example, getExactParameterTypes(List.class.getDeclaredMethod("get"), listOf(optionalOf(String.class) will return optionalOf(String.class). Throws IllegalArgumentException if the given method or constructor is not declared by the given type.