Search in sources :

Example 1 with InjectPackageStrategy

use of org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.InjectPackageStrategy in project beam by apache.

the class ProtoByteBuddyUtils method createBuilderCreator.

@Experimental(Kind.SCHEMAS)
static <ProtoBuilderT extends MessageLite.Builder> SchemaUserTypeCreator createBuilderCreator(Class<?> protoClass, Class<?> builderClass, List<FieldValueSetter<ProtoBuilderT, Object>> setters, Schema schema) {
    try {
        DynamicType.Builder<Supplier> builder = BYTE_BUDDY.with(new InjectPackageStrategy(builderClass)).subclass(Supplier.class).method(ElementMatchers.named("get")).intercept(new BuilderSupplier(protoClass));
        Supplier supplier = builder.visit(new AsmVisitorWrapper.ForDeclaredMethods().writerFlags(ClassWriter.COMPUTE_FRAMES)).make().load(ReflectHelpers.findClassLoader(), ClassLoadingStrategy.Default.INJECTION).getLoaded().getDeclaredConstructor().newInstance();
        return new ProtoCreatorFactory<>(supplier, setters);
    } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
        throw new RuntimeException("Unable to generate a creator for class " + builderClass + " with schema " + schema);
    }
}
Also used : DynamicType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.DynamicType) InvocationTargetException(java.lang.reflect.InvocationTargetException) InjectPackageStrategy(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.InjectPackageStrategy) AsmVisitorWrapper(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.asm.AsmVisitorWrapper) FieldValueTypeSupplier(org.apache.beam.sdk.schemas.utils.FieldValueTypeSupplier) Supplier(java.util.function.Supplier) Experimental(org.apache.beam.sdk.annotations.Experimental)

Example 2 with InjectPackageStrategy

use of org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.InjectPackageStrategy in project beam by apache.

the class AvroByteBuddyUtils method createCreator.

private static <T> SchemaUserTypeCreator createCreator(Class<T> clazz, Schema schema) {
    Constructor baseConstructor = null;
    Constructor[] constructors = clazz.getDeclaredConstructors();
    for (Constructor constructor : constructors) {
        // TODO: This assumes that Avro only generates one constructor with this many fields.
        if (constructor.getParameterCount() == schema.getFieldCount()) {
            baseConstructor = constructor;
        }
    }
    if (baseConstructor == null) {
        throw new RuntimeException("No matching constructor found for class " + clazz);
    }
    // Generate a method call to create and invoke the SpecificRecord's constructor. .
    MethodCall construct = MethodCall.construct(baseConstructor);
    for (int i = 0; i < baseConstructor.getParameterTypes().length; ++i) {
        Class<?> baseType = baseConstructor.getParameterTypes()[i];
        construct = construct.with(readAndConvertParameter(baseType, i), baseType);
    }
    try {
        DynamicType.Builder<SchemaUserTypeCreator> builder = BYTE_BUDDY.with(new InjectPackageStrategy(clazz)).subclass(SchemaUserTypeCreator.class).method(ElementMatchers.named("create")).intercept(construct);
        return builder.visit(new AsmVisitorWrapper.ForDeclaredMethods().writerFlags(ClassWriter.COMPUTE_FRAMES)).make().load(ReflectHelpers.findClassLoader(clazz.getClassLoader()), ClassLoadingStrategy.Default.INJECTION).getLoaded().getDeclaredConstructor().newInstance();
    } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
        throw new RuntimeException("Unable to generate a getter for class " + clazz + " with schema " + schema);
    }
}
Also used : SchemaUserTypeCreator(org.apache.beam.sdk.schemas.SchemaUserTypeCreator) Constructor(java.lang.reflect.Constructor) DynamicType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.DynamicType) MethodCall(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.MethodCall) InvocationTargetException(java.lang.reflect.InvocationTargetException) InjectPackageStrategy(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.InjectPackageStrategy) AsmVisitorWrapper(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.asm.AsmVisitorWrapper)

Aggregations

InvocationTargetException (java.lang.reflect.InvocationTargetException)2 InjectPackageStrategy (org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.InjectPackageStrategy)2 AsmVisitorWrapper (org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.asm.AsmVisitorWrapper)2 DynamicType (org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.DynamicType)2 Constructor (java.lang.reflect.Constructor)1 Supplier (java.util.function.Supplier)1 Experimental (org.apache.beam.sdk.annotations.Experimental)1 SchemaUserTypeCreator (org.apache.beam.sdk.schemas.SchemaUserTypeCreator)1 FieldValueTypeSupplier (org.apache.beam.sdk.schemas.utils.FieldValueTypeSupplier)1 MethodCall (org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.MethodCall)1