Class TypeExtractor


  • @Public
    public class TypeExtractor
    extends Object
    A utility for reflection analysis on classes, to determine the return type of implementations of transformation functions.

    NOTES FOR USERS OF THIS CLASS: Automatic type extraction is a hacky business that depends on a lot of variables such as generics, compiler, interfaces, etc. The type extraction fails regularly with either MissingTypeInfo or hard exceptions. Whenever you use methods of this class, make sure to provide a way to pass custom type information as a fallback.

    • Field Detail

      • NO_INDEX

        public static final int[] NO_INDEX
    • Constructor Detail

      • TypeExtractor

        protected TypeExtractor()
    • Method Detail

      • getPartitionerTypes

        @PublicEvolving
        public static <T> TypeInformation<T> getPartitionerTypes​(Partitioner<T> partitioner,
                                                                 String functionName,
                                                                 boolean allowMissing)
      • getInputFormatTypes

        @PublicEvolving
        public static <IN> TypeInformation<IN> getInputFormatTypes​(InputFormat<IN,​?> inputFormatInterface)
      • getUnaryOperatorReturnType

        @PublicEvolving
        public static <IN,​OUT> TypeInformation<OUT> getUnaryOperatorReturnType​(Function function,
                                                                                     Class<?> baseClass,
                                                                                     int inputTypeArgumentIndex,
                                                                                     int outputTypeArgumentIndex,
                                                                                     int[] lambdaOutputTypeArgumentIndices,
                                                                                     TypeInformation<IN> inType,
                                                                                     String functionName,
                                                                                     boolean allowMissing)
        Returns the unary operator's return type.

        This method can extract a type in 4 different ways:

        1. By using the generics of the base class like MyFunction. This is what outputTypeArgumentIndex (in this example "4") is good for.

        2. By using input type inference SubMyFunction. This is what inputTypeArgumentIndex (in this example "0") and inType is good for.

        3. By using the static method that a compiler generates for Java lambdas. This is what lambdaOutputTypeArgumentIndices is good for. Given that MyFunction has the following single abstract method:

         
         void apply(IN value, Collector value)
         
         

        Lambda type indices allow the extraction of a type from lambdas. To extract the output type OUT from the function one should pass new int[] {1, 0}. "1" for selecting the parameter and 0 for the first generic in this type. Use TypeExtractor.NO_INDEX for selecting the return type of the lambda for extraction or if the class cannot be a lambda because it is not a single abstract method interface.

        4. By using interfaces such as TypeInfoFactory or ResultTypeQueryable.

        See also comments in the header of this class.

        Type Parameters:
        IN - Input type
        OUT - Output type
        Parameters:
        function - Function to extract the return type from
        baseClass - Base class of the function
        inputTypeArgumentIndex - Index of input generic type in the base class specification (ignored if inType is null)
        outputTypeArgumentIndex - Index of output generic type in the base class specification
        lambdaOutputTypeArgumentIndices - Table of indices of the type argument specifying the input type. See example.
        inType - Type of the input elements (In case of an iterable, it is the element type) or null
        functionName - Function name
        allowMissing - Can the type information be missing (this generates a MissingTypeInfo for postponing an exception)
        Returns:
        TypeInformation of the return type of the function
      • getBinaryOperatorReturnType

        @PublicEvolving
        public static <IN1,​IN2,​OUT> TypeInformation<OUT> getBinaryOperatorReturnType​(Function function,
                                                                                                 Class<?> baseClass,
                                                                                                 int input1TypeArgumentIndex,
                                                                                                 int input2TypeArgumentIndex,
                                                                                                 int outputTypeArgumentIndex,
                                                                                                 int[] lambdaOutputTypeArgumentIndices,
                                                                                                 TypeInformation<IN1> in1Type,
                                                                                                 TypeInformation<IN2> in2Type,
                                                                                                 String functionName,
                                                                                                 boolean allowMissing)
        Returns the binary operator's return type.

        This method can extract a type in 4 different ways:

        1. By using the generics of the base class like MyFunction. This is what outputTypeArgumentIndex (in this example "4") is good for.

        2. By using input type inference SubMyFunction. This is what inputTypeArgumentIndex (in this example "0") and inType is good for.

        3. By using the static method that a compiler generates for Java lambdas. This is what lambdaOutputTypeArgumentIndices is good for. Given that MyFunction has the following single abstract method:

         
         void apply(IN value, Collector value)
         
         

        Lambda type indices allow the extraction of a type from lambdas. To extract the output type OUT from the function one should pass new int[] {1, 0}. "1" for selecting the parameter and 0 for the first generic in this type. Use TypeExtractor.NO_INDEX for selecting the return type of the lambda for extraction or if the class cannot be a lambda because it is not a single abstract method interface.

        4. By using interfaces such as TypeInfoFactory or ResultTypeQueryable.

        See also comments in the header of this class.

        Type Parameters:
        IN1 - Left side input type
        IN2 - Right side input type
        OUT - Output type
        Parameters:
        function - Function to extract the return type from
        baseClass - Base class of the function
        input1TypeArgumentIndex - Index of first input generic type in the class specification (ignored if in1Type is null)
        input2TypeArgumentIndex - Index of second input generic type in the class specification (ignored if in2Type is null)
        outputTypeArgumentIndex - Index of output generic type in the class specification
        lambdaOutputTypeArgumentIndices - Table of indices of the type argument specifying the output type. See example.
        in1Type - Type of the left side input elements (In case of an iterable, it is the element type)
        in2Type - Type of the right side input elements (In case of an iterable, it is the element type)
        functionName - Function name
        allowMissing - Can the type information be missing (this generates a MissingTypeInfo for postponing an exception)
        Returns:
        TypeInformation of the return type of the function
      • createTypeInfo

        @PublicEvolving
        public static <OUT> TypeInformation<OUT> createTypeInfo​(Object instance,
                                                                Class<?> baseClass,
                                                                Class<?> clazz,
                                                                int returnParamPos)
        Creates a TypeInformation from the given parameters.

        If the given instance implements ResultTypeQueryable, its information is used to determine the type information. Otherwise, the type information is derived based on the given class information.

        Type Parameters:
        OUT - output type
        Parameters:
        instance - instance to determine type information for
        baseClass - base class of instance
        clazz - class of instance
        returnParamPos - index of the return type in the type arguments of clazz
        Returns:
        type information
      • getParameterType

        @PublicEvolving
        public static Type getParameterType​(Class<?> baseClass,
                                            Class<?> clazz,
                                            int pos)
      • getTypeInfoFactory

        @Internal
        public static <OUT> TypeInfoFactory<OUT> getTypeInfoFactory​(Type t)
        Returns the type information factory for a type using the factory registry or annotations.
      • getTypeInfoFactory

        @Internal
        public static <OUT> TypeInfoFactory<OUT> getTypeInfoFactory​(Field field)
        Returns the type information factory for an annotated field.
      • getForClass

        public static <X> TypeInformation<X> getForClass​(Class<X> clazz)
        Creates type information from a given Class such as Integer, String[] or POJOs.

        This method does not support ParameterizedTypes such as Tuples or complex type hierarchies. In most cases createTypeInfo(Type) is the recommended method for type extraction (a Class is a child of Type).

        Parameters:
        clazz - a Class to create TypeInformation for
        Returns:
        TypeInformation that describes the passed Class
      • getAllDeclaredFields

        @PublicEvolving
        public static List<Field> getAllDeclaredFields​(Class<?> clazz,
                                                       boolean ignoreDuplicates)
        Recursively determine all declared fields This is required because class.getFields() is not returning fields defined in parent classes.
        Parameters:
        clazz - class to be analyzed
        ignoreDuplicates - if true, in case of duplicate field names only the lowest one in a hierarchy will be returned; throws an exception otherwise
        Returns:
        list of fields
      • getDeclaredField

        @PublicEvolving
        public static Field getDeclaredField​(Class<?> clazz,
                                             String name)
      • getForObject

        public static <X> TypeInformation<X> getForObject​(X value)
      • createHadoopWritableTypeInfo

        public static <T> TypeInformation<T> createHadoopWritableTypeInfo​(Class<T> clazz)