Class TypeExtractor
- java.lang.Object
-
- org.apache.flink.api.java.typeutils.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
MissingTypeInfoor hard exceptions. Whenever you use methods of this class, make sure to provide a way to pass custom type information as a fallback.
-
-
Field Summary
Fields Modifier and Type Field Description static int[]NO_INDEX
-
Constructor Summary
Constructors Modifier Constructor Description protectedTypeExtractor()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected <OUT,IN1,IN2>
TypeInformation<OUT>analyzePojo(Type type, List<Type> typeHierarchy, TypeInformation<IN1> in1Type, TypeInformation<IN2> in2Type)static <T> TypeInformation<T>createHadoopWritableTypeInfo(Class<T> clazz)static <IN1,IN2,OUT>
TypeInformation<OUT>createTypeInfo(Class<?> baseClass, Class<?> clazz, int returnParamPos, TypeInformation<IN1> in1Type, TypeInformation<IN2> in2Type)static <T> TypeInformation<T>createTypeInfo(Class<T> type)static <OUT> TypeInformation<OUT>createTypeInfo(Object instance, Class<?> baseClass, Class<?> clazz, int returnParamPos)Creates aTypeInformationfrom the given parameters.static TypeInformation<?>createTypeInfo(Type t)static <IN,ACC>
TypeInformation<ACC>getAggregateFunctionAccumulatorType(AggregateFunction<IN,ACC,?> function, TypeInformation<IN> inType, String functionName, boolean allowMissing)static <IN,OUT>
TypeInformation<OUT>getAggregateFunctionReturnType(AggregateFunction<IN,?,OUT> function, TypeInformation<IN> inType, String functionName, boolean allowMissing)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.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.static <IN1,IN2,OUT>
TypeInformation<OUT>getCoGroupReturnTypes(CoGroupFunction<IN1,IN2,OUT> coGroupInterface, TypeInformation<IN1> in1Type, TypeInformation<IN2> in2Type)static <IN1,IN2,OUT>
TypeInformation<OUT>getCoGroupReturnTypes(CoGroupFunction<IN1,IN2,OUT> coGroupInterface, TypeInformation<IN1> in1Type, TypeInformation<IN2> in2Type, String functionName, boolean allowMissing)static <IN1,IN2,OUT>
TypeInformation<OUT>getCrossReturnTypes(CrossFunction<IN1,IN2,OUT> crossInterface, TypeInformation<IN1> in1Type, TypeInformation<IN2> in2Type)static <IN1,IN2,OUT>
TypeInformation<OUT>getCrossReturnTypes(CrossFunction<IN1,IN2,OUT> crossInterface, TypeInformation<IN1> in1Type, TypeInformation<IN2> in2Type, String functionName, boolean allowMissing)static FieldgetDeclaredField(Class<?> clazz, String name)static <IN1,IN2,OUT>
TypeInformation<OUT>getFlatJoinReturnTypes(FlatJoinFunction<IN1,IN2,OUT> joinInterface, TypeInformation<IN1> in1Type, TypeInformation<IN2> in2Type)static <IN1,IN2,OUT>
TypeInformation<OUT>getFlatJoinReturnTypes(FlatJoinFunction<IN1,IN2,OUT> joinInterface, TypeInformation<IN1> in1Type, TypeInformation<IN2> in2Type, String functionName, boolean allowMissing)static <IN,OUT>
TypeInformation<OUT>getFlatMapReturnTypes(FlatMapFunction<IN,OUT> flatMapInterface, TypeInformation<IN> inType)static <IN,OUT>
TypeInformation<OUT>getFlatMapReturnTypes(FlatMapFunction<IN,OUT> flatMapInterface, TypeInformation<IN> inType, String functionName, boolean allowMissing)static <X> TypeInformation<X>getForClass(Class<X> clazz)Creates type information from a given Class such as Integer, String[] or POJOs.static <X> TypeInformation<X>getForObject(X value)static <IN,OUT>
TypeInformation<OUT>getGroupCombineReturnTypes(GroupCombineFunction<IN,OUT> combineInterface, TypeInformation<IN> inType)static <IN,OUT>
TypeInformation<OUT>getGroupCombineReturnTypes(GroupCombineFunction<IN,OUT> combineInterface, TypeInformation<IN> inType, String functionName, boolean allowMissing)static <IN,OUT>
TypeInformation<OUT>getGroupReduceReturnTypes(GroupReduceFunction<IN,OUT> groupReduceInterface, TypeInformation<IN> inType)static <IN,OUT>
TypeInformation<OUT>getGroupReduceReturnTypes(GroupReduceFunction<IN,OUT> groupReduceInterface, TypeInformation<IN> inType, String functionName, boolean allowMissing)static <IN> TypeInformation<IN>getInputFormatTypes(InputFormat<IN,?> inputFormatInterface)static <IN1,IN2,OUT>
TypeInformation<OUT>getJoinReturnTypes(JoinFunction<IN1,IN2,OUT> joinInterface, TypeInformation<IN1> in1Type, TypeInformation<IN2> in2Type)static <IN1,IN2,OUT>
TypeInformation<OUT>getJoinReturnTypes(JoinFunction<IN1,IN2,OUT> joinInterface, TypeInformation<IN1> in1Type, TypeInformation<IN2> in2Type, String functionName, boolean allowMissing)static <IN,OUT>
TypeInformation<OUT>getKeySelectorTypes(KeySelector<IN,OUT> selectorInterface, TypeInformation<IN> inType)static <IN,OUT>
TypeInformation<OUT>getKeySelectorTypes(KeySelector<IN,OUT> selectorInterface, TypeInformation<IN> inType, String functionName, boolean allowMissing)static <IN,OUT>
TypeInformation<OUT>getMapPartitionReturnTypes(MapPartitionFunction<IN,OUT> mapPartitionInterface, TypeInformation<IN> inType)static <IN,OUT>
TypeInformation<OUT>getMapPartitionReturnTypes(MapPartitionFunction<IN,OUT> mapPartitionInterface, TypeInformation<IN> inType, String functionName, boolean allowMissing)static <IN,OUT>
TypeInformation<OUT>getMapReturnTypes(MapFunction<IN,OUT> mapInterface, TypeInformation<IN> inType)static <IN,OUT>
TypeInformation<OUT>getMapReturnTypes(MapFunction<IN,OUT> mapInterface, TypeInformation<IN> inType, String functionName, boolean allowMissing)static TypegetParameterType(Class<?> baseClass, Class<?> clazz, int pos)static <T> TypeInformation<T>getPartitionerTypes(Partitioner<T> partitioner)static <T> TypeInformation<T>getPartitionerTypes(Partitioner<T> partitioner, String functionName, boolean allowMissing)static <OUT> TypeInfoFactory<OUT>getTypeInfoFactory(Field field)Returns the type information factory for an annotated field.static <OUT> TypeInfoFactory<OUT>getTypeInfoFactory(Type t)Returns the type information factory for a type using the factory registry or annotations.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.static booleanisRecord(Class<?> clazz)Determine whether the given class is a valid Java record.static voidregisterFactory(Type t, Class<? extends TypeInfoFactory<?>> factory)Registers a type information factory globally for a certain type.
-
-
-
Method Detail
-
registerFactory
@Internal public static void registerFactory(Type t, Class<? extends TypeInfoFactory<?>> factory)
Registers a type information factory globally for a certain type. Every following type extraction operation will use the provided factory for this type. The factory will have the highest precedence for this type. In a hierarchy of types the registered factory has higher precedence than annotations at the same level but lower precedence than factories defined down the hierarchy.- Parameters:
t- type for which a new factory is registeredfactory- type information factory that will produceTypeInformation
-
getMapReturnTypes
@PublicEvolving public static <IN,OUT> TypeInformation<OUT> getMapReturnTypes(MapFunction<IN,OUT> mapInterface, TypeInformation<IN> inType)
-
getMapReturnTypes
@PublicEvolving public static <IN,OUT> TypeInformation<OUT> getMapReturnTypes(MapFunction<IN,OUT> mapInterface, TypeInformation<IN> inType, String functionName, boolean allowMissing)
-
getFlatMapReturnTypes
@PublicEvolving public static <IN,OUT> TypeInformation<OUT> getFlatMapReturnTypes(FlatMapFunction<IN,OUT> flatMapInterface, TypeInformation<IN> inType)
-
getFlatMapReturnTypes
@PublicEvolving public static <IN,OUT> TypeInformation<OUT> getFlatMapReturnTypes(FlatMapFunction<IN,OUT> flatMapInterface, TypeInformation<IN> inType, String functionName, boolean allowMissing)
-
getAggregateFunctionAccumulatorType
@PublicEvolving public static <IN,ACC> TypeInformation<ACC> getAggregateFunctionAccumulatorType(AggregateFunction<IN,ACC,?> function, TypeInformation<IN> inType, String functionName, boolean allowMissing)
-
getAggregateFunctionReturnType
@PublicEvolving public static <IN,OUT> TypeInformation<OUT> getAggregateFunctionReturnType(AggregateFunction<IN,?,OUT> function, TypeInformation<IN> inType, String functionName, boolean allowMissing)
-
getMapPartitionReturnTypes
@PublicEvolving public static <IN,OUT> TypeInformation<OUT> getMapPartitionReturnTypes(MapPartitionFunction<IN,OUT> mapPartitionInterface, TypeInformation<IN> inType)
-
getMapPartitionReturnTypes
@PublicEvolving public static <IN,OUT> TypeInformation<OUT> getMapPartitionReturnTypes(MapPartitionFunction<IN,OUT> mapPartitionInterface, TypeInformation<IN> inType, String functionName, boolean allowMissing)
-
getGroupReduceReturnTypes
@PublicEvolving public static <IN,OUT> TypeInformation<OUT> getGroupReduceReturnTypes(GroupReduceFunction<IN,OUT> groupReduceInterface, TypeInformation<IN> inType)
-
getGroupReduceReturnTypes
@PublicEvolving public static <IN,OUT> TypeInformation<OUT> getGroupReduceReturnTypes(GroupReduceFunction<IN,OUT> groupReduceInterface, TypeInformation<IN> inType, String functionName, boolean allowMissing)
-
getGroupCombineReturnTypes
@PublicEvolving public static <IN,OUT> TypeInformation<OUT> getGroupCombineReturnTypes(GroupCombineFunction<IN,OUT> combineInterface, TypeInformation<IN> inType)
-
getGroupCombineReturnTypes
@PublicEvolving public static <IN,OUT> TypeInformation<OUT> getGroupCombineReturnTypes(GroupCombineFunction<IN,OUT> combineInterface, TypeInformation<IN> inType, String functionName, boolean allowMissing)
-
getFlatJoinReturnTypes
@PublicEvolving public static <IN1,IN2,OUT> TypeInformation<OUT> getFlatJoinReturnTypes(FlatJoinFunction<IN1,IN2,OUT> joinInterface, TypeInformation<IN1> in1Type, TypeInformation<IN2> in2Type)
-
getFlatJoinReturnTypes
@PublicEvolving public static <IN1,IN2,OUT> TypeInformation<OUT> getFlatJoinReturnTypes(FlatJoinFunction<IN1,IN2,OUT> joinInterface, TypeInformation<IN1> in1Type, TypeInformation<IN2> in2Type, String functionName, boolean allowMissing)
-
getJoinReturnTypes
@PublicEvolving public static <IN1,IN2,OUT> TypeInformation<OUT> getJoinReturnTypes(JoinFunction<IN1,IN2,OUT> joinInterface, TypeInformation<IN1> in1Type, TypeInformation<IN2> in2Type)
-
getJoinReturnTypes
@PublicEvolving public static <IN1,IN2,OUT> TypeInformation<OUT> getJoinReturnTypes(JoinFunction<IN1,IN2,OUT> joinInterface, TypeInformation<IN1> in1Type, TypeInformation<IN2> in2Type, String functionName, boolean allowMissing)
-
getCoGroupReturnTypes
@PublicEvolving public static <IN1,IN2,OUT> TypeInformation<OUT> getCoGroupReturnTypes(CoGroupFunction<IN1,IN2,OUT> coGroupInterface, TypeInformation<IN1> in1Type, TypeInformation<IN2> in2Type)
-
getCoGroupReturnTypes
@PublicEvolving public static <IN1,IN2,OUT> TypeInformation<OUT> getCoGroupReturnTypes(CoGroupFunction<IN1,IN2,OUT> coGroupInterface, TypeInformation<IN1> in1Type, TypeInformation<IN2> in2Type, String functionName, boolean allowMissing)
-
getCrossReturnTypes
@PublicEvolving public static <IN1,IN2,OUT> TypeInformation<OUT> getCrossReturnTypes(CrossFunction<IN1,IN2,OUT> crossInterface, TypeInformation<IN1> in1Type, TypeInformation<IN2> in2Type)
-
getCrossReturnTypes
@PublicEvolving public static <IN1,IN2,OUT> TypeInformation<OUT> getCrossReturnTypes(CrossFunction<IN1,IN2,OUT> crossInterface, TypeInformation<IN1> in1Type, TypeInformation<IN2> in2Type, String functionName, boolean allowMissing)
-
getKeySelectorTypes
@PublicEvolving public static <IN,OUT> TypeInformation<OUT> getKeySelectorTypes(KeySelector<IN,OUT> selectorInterface, TypeInformation<IN> inType)
-
getKeySelectorTypes
@PublicEvolving public static <IN,OUT> TypeInformation<OUT> getKeySelectorTypes(KeySelector<IN,OUT> selectorInterface, TypeInformation<IN> inType, String functionName, boolean allowMissing)
-
getPartitionerTypes
@PublicEvolving public static <T> TypeInformation<T> getPartitionerTypes(Partitioner<T> partitioner)
-
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, Collectorvalue) 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. UseTypeExtractor.NO_INDEXfor 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
TypeInfoFactoryorResultTypeQueryable.See also comments in the header of this class.
- Type Parameters:
IN- Input typeOUT- Output type- Parameters:
function- Function to extract the return type frombaseClass- Base class of the functioninputTypeArgumentIndex- 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 specificationlambdaOutputTypeArgumentIndices- 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 nullfunctionName- Function nameallowMissing- 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, Collectorvalue) 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. UseTypeExtractor.NO_INDEXfor 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
TypeInfoFactoryorResultTypeQueryable.See also comments in the header of this class.
- Type Parameters:
IN1- Left side input typeIN2- Right side input typeOUT- Output type- Parameters:
function- Function to extract the return type frombaseClass- Base class of the functioninput1TypeArgumentIndex- 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 specificationlambdaOutputTypeArgumentIndices- 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 nameallowMissing- Can the type information be missing (this generates a MissingTypeInfo for postponing an exception)- Returns:
- TypeInformation of the return type of the function
-
createTypeInfo
public static <T> TypeInformation<T> createTypeInfo(Class<T> type)
-
createTypeInfo
public static TypeInformation<?> createTypeInfo(Type t)
-
createTypeInfo
@PublicEvolving public static <OUT> TypeInformation<OUT> createTypeInfo(Object instance, Class<?> baseClass, Class<?> clazz, int returnParamPos)
Creates aTypeInformationfrom the given parameters.If the given
instanceimplementsResultTypeQueryable, 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 forbaseClass- base class ofinstanceclazz- class ofinstancereturnParamPos- index of the return type in the type arguments ofclazz- Returns:
- type information
-
createTypeInfo
@PublicEvolving public static <IN1,IN2,OUT> TypeInformation<OUT> createTypeInfo(Class<?> baseClass, Class<?> clazz, int returnParamPos, TypeInformation<IN1> in1Type, TypeInformation<IN2> in2Type)
-
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
-
analyzePojo
protected <OUT,IN1,IN2> TypeInformation<OUT> analyzePojo(Type type, List<Type> typeHierarchy, TypeInformation<IN1> in1Type, TypeInformation<IN2> in2Type)
-
isRecord
@PublicEvolving public static boolean isRecord(Class<?> clazz)
Determine whether the given class is a valid Java record.- Parameters:
clazz- class to check- Returns:
- True if the class is a Java record
-
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 analyzedignoreDuplicates- 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
-
getForObject
public static <X> TypeInformation<X> getForObject(X value)
-
createHadoopWritableTypeInfo
public static <T> TypeInformation<T> createHadoopWritableTypeInfo(Class<T> clazz)
-
-