Search in sources :

Example 1 with AvroTypeConversionFactory

use of org.apache.beam.sdk.schemas.utils.AvroUtils.AvroTypeConversionFactory in project beam by apache.

the class AvroByteBuddyUtils method readAndConvertParameter.

private static StackManipulation readAndConvertParameter(Class<?> constructorParameterType, int index) {
    TypeConversionsFactory typeConversionsFactory = new AvroTypeConversionFactory();
    // The types in the AVRO-generated constructor might be the types returned by Beam's Row class,
    // so we have to convert the types used by Beam's Row class.
    // We know that AVRO generates constructor parameters in the same order as fields
    // in the schema, so we can just add the parameters sequentially.
    TypeConversion<Type> convertType = typeConversionsFactory.createTypeConversion(true);
    // Map the AVRO-generated type to the one Beam will use.
    ForLoadedType convertedType = new ForLoadedType((Class) convertType.convert(TypeDescriptor.of(constructorParameterType)));
    // This will run inside the generated creator. Read the parameter and convert it to the
    // type required by the SpecificRecord constructor.
    StackManipulation readParameter = new StackManipulation.Compound(MethodVariableAccess.REFERENCE.loadFrom(1), IntegerConstant.forValue(index), ArrayAccess.REFERENCE.load(), TypeCasting.to(convertedType));
    // Convert to the parameter accepted by the SpecificRecord constructor.
    return typeConversionsFactory.createSetterConversions(readParameter).convert(TypeDescriptor.of(constructorParameterType));
}
Also used : DynamicType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.DynamicType) ForLoadedType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.type.TypeDescription.ForLoadedType) Type(java.lang.reflect.Type) StackManipulation(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.StackManipulation) ForLoadedType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.type.TypeDescription.ForLoadedType) AvroTypeConversionFactory(org.apache.beam.sdk.schemas.utils.AvroUtils.AvroTypeConversionFactory) TypeConversionsFactory(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.TypeConversionsFactory)

Aggregations

Type (java.lang.reflect.Type)1 AvroTypeConversionFactory (org.apache.beam.sdk.schemas.utils.AvroUtils.AvroTypeConversionFactory)1 TypeConversionsFactory (org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.TypeConversionsFactory)1 ForLoadedType (org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.type.TypeDescription.ForLoadedType)1 DynamicType (org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.DynamicType)1 StackManipulation (org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.StackManipulation)1