Search in sources :

Example 1 with SchemaUserTypeCreator

use of org.apache.beam.sdk.schemas.SchemaUserTypeCreator in project beam by apache.

the class AutoValueUtils method getConstructorCreator.

/**
 * Try to find an accessible constructor for creating an AutoValue class. Otherwise return null.
 */
@Nullable
public static SchemaUserTypeCreator getConstructorCreator(Class<?> clazz, Schema schema, FieldValueTypeSupplier fieldValueTypeSupplier) {
    Class<?> generatedClass = getAutoValueGenerated(clazz);
    List<FieldValueTypeInformation> schemaTypes = fieldValueTypeSupplier.get(clazz, schema);
    Optional<Constructor<?>> constructor = Arrays.stream(generatedClass.getDeclaredConstructors()).filter(c -> !Modifier.isPrivate(c.getModifiers())).filter(c -> matchConstructor(c, schemaTypes)).findAny();
    return constructor.map(c -> JavaBeanUtils.getConstructorCreator(generatedClass, c, schema, fieldValueTypeSupplier, new DefaultTypeConversionsFactory())).orElse(null);
}
Also used : ByteBuddy(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.ByteBuddy) Preconditions.checkNotNull(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions.checkNotNull) Arrays(java.util.Arrays) TypeDescriptor(org.apache.beam.sdk.values.TypeDescriptor) MethodVariableAccess(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.member.MethodVariableAccess) Experimental(org.apache.beam.sdk.annotations.Experimental) ArrayAccess(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.collection.ArrayAccess) DynamicType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.DynamicType) TypeCasting(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.assign.TypeCasting) AsmVisitorWrapper(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.asm.AsmVisitorWrapper) Constructor(java.lang.reflect.Constructor) Function(java.util.function.Function) InstrumentedType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.scaffold.InstrumentedType) ByteCodeAppender(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.ByteCodeAppender) Parameter(java.lang.reflect.Parameter) Kind(org.apache.beam.sdk.annotations.Experimental.Kind) SchemaUserTypeCreator(org.apache.beam.sdk.schemas.SchemaUserTypeCreator) Map(java.util.Map) MethodReturn(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.member.MethodReturn) FieldValueTypeInformation(org.apache.beam.sdk.schemas.FieldValueTypeInformation) ForLoadedType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.type.TypeDescription.ForLoadedType) Method(java.lang.reflect.Method) IntegerConstant(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.constant.IntegerConstant) DefaultTypeConversionsFactory(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.DefaultTypeConversionsFactory) Nullable(org.checkerframework.checker.nullness.qual.Nullable) ClassLoadingStrategy(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.loading.ClassLoadingStrategy) TypeCreation(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.TypeCreation) Duplication(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.Duplication) Size(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.ByteCodeAppender.Size) ElementMatchers(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.matcher.ElementMatchers) Lists(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Lists) ClassWriter(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.jar.asm.ClassWriter) Collectors(java.util.stream.Collectors) Schema(org.apache.beam.sdk.schemas.Schema) TypeConversion(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.TypeConversion) InvocationTargetException(java.lang.reflect.InvocationTargetException) List(java.util.List) StackManipulation(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.StackManipulation) MethodInvocation(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.member.MethodInvocation) Type(java.lang.reflect.Type) Modifier(java.lang.reflect.Modifier) InjectPackageStrategy(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.InjectPackageStrategy) ReflectHelpers(org.apache.beam.sdk.util.common.ReflectHelpers) TypeConversionsFactory(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.TypeConversionsFactory) Optional(java.util.Optional) Removal(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.Removal) Implementation(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.Implementation) ForLoadedMethod(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.method.MethodDescription.ForLoadedMethod) FieldValueTypeInformation(org.apache.beam.sdk.schemas.FieldValueTypeInformation) Constructor(java.lang.reflect.Constructor) DefaultTypeConversionsFactory(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.DefaultTypeConversionsFactory) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 2 with SchemaUserTypeCreator

use of org.apache.beam.sdk.schemas.SchemaUserTypeCreator 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)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 SchemaUserTypeCreator (org.apache.beam.sdk.schemas.SchemaUserTypeCreator)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 Method (java.lang.reflect.Method)1 Modifier (java.lang.reflect.Modifier)1 Parameter (java.lang.reflect.Parameter)1 Type (java.lang.reflect.Type)1 Arrays (java.util.Arrays)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Function (java.util.function.Function)1 Collectors (java.util.stream.Collectors)1 Experimental (org.apache.beam.sdk.annotations.Experimental)1 Kind (org.apache.beam.sdk.annotations.Experimental.Kind)1 FieldValueTypeInformation (org.apache.beam.sdk.schemas.FieldValueTypeInformation)1 Schema (org.apache.beam.sdk.schemas.Schema)1