Class Reflection


  • public final class Reflection
    extends java.lang.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 Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.reflect.Type getArrayElementType​(java.lang.reflect.Type type)
      Returns the (possibly parameterized) element type for the given array type.
      static java.lang.reflect.Type[] getExactParameterTypes​(java.lang.reflect.Executable m, java.lang.reflect.Type declaringType)
      Returns the exact parameter types of the given method or constructor, taking into account type arguments of the given declaring type.
      static java.lang.reflect.Type getExactSubtype​(java.lang.reflect.Type type, java.lang.Class<?> rawSubtype)
      Returns a parameterization of the given raw subtype, taking into account type arguments of the given supertype.
      static java.lang.reflect.Type getExactSupertype​(java.lang.reflect.Type type, java.lang.Class<?> rawSupertype)
      Returns a parameterization of the given raw supertype, taking into account type arguments of the given subtype.
      static boolean isMissingTypeArguments​(java.lang.reflect.Type type)  
      static java.lang.reflect.Type normalize​(java.lang.reflect.Type type)
      Returns the normalized form of the given type.
      static java.lang.Class<?> toRawType​(java.lang.reflect.Type type)
      Returns the raw (erased) type for the given parameterized type, type bound for the given wildcard type, or the given type otherwise.
      static <T> java.lang.Class<T> toWrapperType​(java.lang.Class<T> type)
      Returns the wrapper type for the given primitive type.
      static @Nullable java.lang.Class<?> tryLoadClass​(java.lang.String qualifiedName)
      Returns the class with the given fully qualified name, or null if a class with the given name cannot be found.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • tryLoadClass

        public static @Nullable java.lang.Class<?> tryLoadClass​(java.lang.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​(java.lang.reflect.Type type)
      • normalize

        public static java.lang.reflect.Type normalize​(java.lang.reflect.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 java.lang.Class<?> toRawType​(java.lang.reflect.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> java.lang.Class<T> toWrapperType​(java.lang.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 java.lang.reflect.Type getArrayElementType​(java.lang.reflect.Type type)
        Returns the (possibly parameterized) element type for the given array type.
      • getExactSupertype

        public static java.lang.reflect.Type getExactSupertype​(java.lang.reflect.Type type,
                                                               java.lang.Class<?> rawSupertype)
        Returns a parameterization of the given raw supertype, taking into account type arguments of the given subtype. For example, @{code getExactSupertype(listOf(String.class), Collection.class)} will return @{code 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 java.lang.reflect.Type getExactSubtype​(java.lang.reflect.Type type,
                                                             java.lang.Class<?> rawSubtype)
        Returns a parameterization of the given raw subtype, taking into account type arguments of the given supertype. For example, @{code getExactSubtype(collectionOf(String.class), List.class)} will return @{code 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 java.lang.reflect.Type[] getExactParameterTypes​(java.lang.reflect.Executable m,
                                                                      java.lang.reflect.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.