Class GenericTypeExtractor


  • public final class GenericTypeExtractor
    extends java.lang.Object
    Attempts to extract generic type of given target base class or target interface
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static java.lang.Class<?> extractGeneric​(java.lang.reflect.Type type)
      Attempts to extract generic parameter type of given type.
      private static java.lang.reflect.Type findGenericInterface​(java.lang.Class<?> sourceClass, java.lang.Class<?> targetBaseInterface)
      Finds generic interface implementation based on the source class and the target interface.
      static java.lang.Class<?> genericTypeOf​(java.lang.Class<?> rootClass, java.lang.Class<?> targetBaseClass, java.lang.Class<?> targetBaseInterface)
      Extract generic type of root class either from the target base class or from target base interface.
      • Methods inherited from class java.lang.Object

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

      • GenericTypeExtractor

        private GenericTypeExtractor()
    • Method Detail

      • genericTypeOf

        public static java.lang.Class<?> genericTypeOf​(java.lang.Class<?> rootClass,
                                                       java.lang.Class<?> targetBaseClass,
                                                       java.lang.Class<?> targetBaseInterface)
        Extract generic type of root class either from the target base class or from target base interface. Examples:

        1. Foo implements IFoo[Integer]: genericTypeOf(Foo.class, Object.class, IFoo.class) returns Integer

        2. Foo extends BaseFoo[String]: genericTypeOf(Foo.class, BaseFoo.class, IFoo.class) returns String

        3. Foo extends BaseFoo; BaseFoo implements IFoo[String]: genericTypeOf(Foo.class, BaseFoo.class, Object.class) returns String

        Does not support nested generics, only supports single type parameter.

        Parameters:
        rootClass - - the root class that the search begins from
        targetBaseClass - - if one of the classes in the root class' hierarchy extends this base class it will be used for generic type extraction
        targetBaseInterface - - if one of the interfaces in the root class' hierarchy implements this interface it will be used for generic type extraction
        Returns:
        generic interface if found, Object.class if not found.
      • findGenericInterface

        private static java.lang.reflect.Type findGenericInterface​(java.lang.Class<?> sourceClass,
                                                                   java.lang.Class<?> targetBaseInterface)
        Finds generic interface implementation based on the source class and the target interface. Returns null if not found. Recurses the interface hierarchy.
      • extractGeneric

        private static java.lang.Class<?> extractGeneric​(java.lang.reflect.Type type)
        Attempts to extract generic parameter type of given type. If there is no generic parameter it returns Object.class