Search in sources :

Example 1 with MethodCall

use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.MethodCall 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

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