Class GenericTypeUtils


  • public class GenericTypeUtils
    extends java.lang.Object
    Utility methods for dealing with generic types via reflection. Generally reflection is to be avoided in Micronaut. Hence this class is regarded as internal and used for only certain niche cases.
    Since:
    1.0
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.util.Optional<java.lang.Class> resolveGenericTypeArgument​(java.lang.reflect.Field field)
      Resolves a single generic type argument for the given field.
      static java.util.Optional<java.lang.Class> resolveInterfaceTypeArgument​(java.lang.Class type, java.lang.Class interfaceType)
      Resolves a single type argument from the given interface of the given class.
      static java.lang.Class[] resolveInterfaceTypeArguments​(java.lang.Class<?> type, java.lang.Class<?> interfaceType)
      Resolve all of the type arguments for the given interface from the given type.
      static java.util.Optional<java.lang.Class> resolveSuperGenericTypeArgument​(java.lang.Class type)
      Resolves a single generic type argument from the super class of the given type.
      static java.lang.Class[] resolveSuperTypeGenericArguments​(java.lang.Class<?> type, java.lang.Class<?> superTypeToResolve)
      Resolve all of the type arguments for the given super type from the given type.
      static java.lang.Class[] resolveTypeArguments​(java.lang.reflect.Type genericType)
      Resolves the type arguments for a generic type.
      • Methods inherited from class java.lang.Object

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

      • GenericTypeUtils

        public GenericTypeUtils()
    • Method Detail

      • resolveGenericTypeArgument

        public static java.util.Optional<java.lang.Class> resolveGenericTypeArgument​(java.lang.reflect.Field field)
        Resolves a single generic type argument for the given field.
        Parameters:
        field - The field
        Returns:
        The type argument or Optional.empty()
      • resolveInterfaceTypeArguments

        public static java.lang.Class[] resolveInterfaceTypeArguments​(java.lang.Class<?> type,
                                                                      java.lang.Class<?> interfaceType)
        Resolve all of the type arguments for the given interface from the given type. Also searches superclasses.
        Parameters:
        type - The type to resolve from
        interfaceType - The interface to resolve from
        Returns:
        The type arguments to the interface
      • resolveSuperTypeGenericArguments

        public static java.lang.Class[] resolveSuperTypeGenericArguments​(java.lang.Class<?> type,
                                                                         java.lang.Class<?> superTypeToResolve)
        Resolve all of the type arguments for the given super type from the given type.
        Parameters:
        type - The type to resolve from
        superTypeToResolve - The suepr type to resolve from
        Returns:
        The type arguments to the interface
      • resolveSuperGenericTypeArgument

        public static java.util.Optional<java.lang.Class> resolveSuperGenericTypeArgument​(java.lang.Class type)
        Resolves a single generic type argument from the super class of the given type.
        Parameters:
        type - The type to resolve from
        Returns:
        A single Class or null
      • resolveTypeArguments

        public static java.lang.Class[] resolveTypeArguments​(java.lang.reflect.Type genericType)
        Resolves the type arguments for a generic type.
        Parameters:
        genericType - The generic type
        Returns:
        The type arguments
      • resolveInterfaceTypeArgument

        public static java.util.Optional<java.lang.Class> resolveInterfaceTypeArgument​(java.lang.Class type,
                                                                                       java.lang.Class interfaceType)
        Resolves a single type argument from the given interface of the given class. Also searches superclasses.
        Parameters:
        type - The type to resolve from
        interfaceType - The interface to resolve for
        Returns:
        The class or null