Search in sources :

Example 1 with FieldValueTypeInformation

use of org.apache.beam.sdk.schemas.FieldValueTypeInformation 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 FieldValueTypeInformation

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

the class AutoValueUtils method getBuilderCreator.

/**
 * Try to find an accessible builder class for creating an AutoValue class. Otherwise return null.
 */
@Nullable
public static SchemaUserTypeCreator getBuilderCreator(Class<?> clazz, Schema schema, FieldValueTypeSupplier fieldValueTypeSupplier) {
    Class<?> builderClass = getAutoValueGeneratedBuilder(clazz);
    if (builderClass == null) {
        return null;
    }
    Map<String, FieldValueTypeInformation> setterTypes = ReflectUtils.getMethods(builderClass).stream().filter(ReflectUtils::isSetter).map(FieldValueTypeInformation::forSetter).collect(Collectors.toMap(FieldValueTypeInformation::getName, Function.identity()));
    List<FieldValueTypeInformation> setterMethods = // The builder methods to call in order.
    Lists.newArrayList();
    List<FieldValueTypeInformation> schemaTypes = fieldValueTypeSupplier.get(clazz, schema);
    for (FieldValueTypeInformation type : schemaTypes) {
        String autoValueFieldName = ReflectUtils.stripGetterPrefix(type.getMethod().getName());
        FieldValueTypeInformation setterType = setterTypes.get(autoValueFieldName);
        if (setterType == null) {
            throw new RuntimeException("AutoValue builder class " + builderClass + " did not contain " + "a setter for " + autoValueFieldName);
        }
        setterMethods.add(setterType);
    }
    Method buildMethod = ReflectUtils.getMethods(builderClass).stream().filter(m -> m.getName().equals("build")).findAny().orElseThrow(() -> new RuntimeException("No build method in builder"));
    return createBuilderCreator(builderClass, setterMethods, buildMethod, schema, schemaTypes);
}
Also used : FieldValueTypeInformation(org.apache.beam.sdk.schemas.FieldValueTypeInformation) Method(java.lang.reflect.Method) ForLoadedMethod(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.method.MethodDescription.ForLoadedMethod) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 3 with FieldValueTypeInformation

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

the class JavaBeanUtils method validateJavaBean.

// Make sure that there are matching setters and getters.
public static void validateJavaBean(List<FieldValueTypeInformation> getters, List<FieldValueTypeInformation> setters, Schema schema) {
    Map<String, FieldValueTypeInformation> setterMap = new HashMap<>();
    int bound = schema.getFieldCount();
    for (int i = 0; i < bound; i++) {
        Integer integer = i;
        if (setterMap.put(schema.getField(integer).getName(), setters.get(integer)) != null) {
            throw new IllegalStateException("Duplicate key");
        }
    }
    for (FieldValueTypeInformation type : getters) {
        FieldValueTypeInformation setterType = setterMap.get(type.getName());
        if (setterType == null) {
            throw new RuntimeException(String.format("Java Bean '%s' contains a getter for field '%s', but does not contain a matching setter. %s", type.getMethod().getDeclaringClass(), type.getName(), CONSTRUCTOR_HELP_STRING));
        }
        if (!type.getType().equals(setterType.getType())) {
            throw new RuntimeException(String.format("Java Bean '%s' contains a setter for field '%s' that has a mismatching type. %s", type.getMethod().getDeclaringClass(), type.getName(), CONSTRUCTOR_HELP_STRING));
        }
        if (!type.isNullable() == setterType.isNullable()) {
            throw new RuntimeException(String.format("Java Bean '%s' contains a setter for field '%s' that has a mismatching nullable attribute. %s", type.getMethod().getDeclaringClass(), type.getName(), CONSTRUCTOR_HELP_STRING));
        }
    }
}
Also used : FieldValueTypeInformation(org.apache.beam.sdk.schemas.FieldValueTypeInformation) HashMap(java.util.HashMap)

Example 4 with FieldValueTypeInformation

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

the class ProtoByteBuddyUtils method createGetter.

private static <ProtoT> FieldValueGetter createGetter(FieldValueTypeInformation fieldValueTypeInformation, TypeConversionsFactory typeConversionsFactory, Class clazz, Multimap<String, Method> methods, Field field, FieldValueTypeSupplier fieldValueTypeSupplier) {
    if (field.getType().isLogicalType(OneOfType.IDENTIFIER)) {
        OneOfType oneOfType = field.getType().getLogicalType(OneOfType.class);
        // The case accessor method in the proto is named getOneOfNameCase.
        Method caseMethod = getProtoGetter(methods, field.getName() + "_case", FieldType.logicalType(oneOfType.getCaseEnumType()));
        // Create a map of case enum value to getter. This must be sorted, so store in a TreeMap.
        TreeMap<Integer, FieldValueGetter<ProtoT, OneOfType.Value>> oneOfGetters = Maps.newTreeMap();
        Map<String, FieldValueTypeInformation> oneOfFieldTypes = fieldValueTypeSupplier.get(clazz, oneOfType.getOneOfSchema()).stream().collect(Collectors.toMap(FieldValueTypeInformation::getName, f -> f));
        for (Field oneOfField : oneOfType.getOneOfSchema().getFields()) {
            int protoFieldIndex = getFieldNumber(oneOfField);
            FieldValueGetter oneOfFieldGetter = createGetter(oneOfFieldTypes.get(oneOfField.getName()), typeConversionsFactory, clazz, methods, oneOfField, fieldValueTypeSupplier);
            oneOfGetters.put(protoFieldIndex, oneOfFieldGetter);
        }
        return createOneOfGetter(fieldValueTypeInformation, oneOfGetters, clazz, oneOfType, caseMethod);
    } else {
        return JavaBeanUtils.createGetter(fieldValueTypeInformation, typeConversionsFactory);
    }
}
Also used : Arrays(java.util.Arrays) ProtocolMessageEnum(com.google.protobuf.ProtocolMessageEnum) ArrayAccess(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.collection.ArrayAccess) OneOfType(org.apache.beam.sdk.schemas.logicaltypes.OneOfType) TypeCasting(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.assign.TypeCasting) StringValue(com.google.protobuf.StringValue) AsmVisitorWrapper(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.asm.AsmVisitorWrapper) ImmutableMap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap) ByteBuddyUtils(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils) ByteCodeAppender(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.ByteCodeAppender) Label(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.jar.asm.Label) NullConstant(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.constant.NullConstant) Visibility(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.modifier.Visibility) Map(java.util.Map) EnumerationType(org.apache.beam.sdk.schemas.logicaltypes.EnumerationType) MethodReturn(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.member.MethodReturn) Method(java.lang.reflect.Method) ClassLoadingStrategy(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.loading.ClassLoadingStrategy) FloatValue(com.google.protobuf.FloatValue) Size(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.ByteCodeAppender.Size) Set(java.util.Set) FieldType(org.apache.beam.sdk.schemas.Schema.FieldType) Collectors(java.util.stream.Collectors) TypeConversion(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.TypeConversion) InvocationTargetException(java.lang.reflect.InvocationTargetException) FixedValue(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.FixedValue) ByteString(com.google.protobuf.ByteString) BytesValue(com.google.protobuf.BytesValue) FieldValueTypeSupplier(org.apache.beam.sdk.schemas.utils.FieldValueTypeSupplier) List(java.util.List) Type(java.lang.reflect.Type) Modifier(java.lang.reflect.Modifier) ReflectHelpers(org.apache.beam.sdk.util.common.ReflectHelpers) ProtoSchemaTranslator.getFieldNumber(org.apache.beam.sdk.extensions.protobuf.ProtoSchemaTranslator.getFieldNumber) Typing(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.assign.Assigner.Typing) Implementation(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.Implementation) ByteBuddy(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.ByteBuddy) TypeDescription(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.type.TypeDescription) TypeDescriptor(org.apache.beam.sdk.values.TypeDescriptor) Int64Value(com.google.protobuf.Int64Value) ConvertType(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.ConvertType) MethodVariableAccess(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.member.MethodVariableAccess) BoolValue(com.google.protobuf.BoolValue) Experimental(org.apache.beam.sdk.annotations.Experimental) DynamicType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.DynamicType) UInt64Value(com.google.protobuf.UInt64Value) CaseFormat(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.CaseFormat) ClassWithSchema(org.apache.beam.sdk.schemas.utils.ReflectUtils.ClassWithSchema) Timestamp(com.google.protobuf.Timestamp) Supplier(java.util.function.Supplier) FieldAccess(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.member.FieldAccess) InstrumentedType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.scaffold.InstrumentedType) FieldManifestation(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.modifier.FieldManifestation) FieldValueSetter(org.apache.beam.sdk.schemas.FieldValueSetter) Multimap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Multimap) Kind(org.apache.beam.sdk.annotations.Experimental.Kind) SchemaUserTypeCreator(org.apache.beam.sdk.schemas.SchemaUserTypeCreator) Maps(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Maps) MethodDescription(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.method.MethodDescription) FieldValueTypeInformation(org.apache.beam.sdk.schemas.FieldValueTypeInformation) ForLoadedType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.type.TypeDescription.ForLoadedType) Row(org.apache.beam.sdk.values.Row) IntegerConstant(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.constant.IntegerConstant) JavaBeanUtils(org.apache.beam.sdk.schemas.utils.JavaBeanUtils) Nullable(org.checkerframework.checker.nullness.qual.Nullable) DoubleValue(com.google.protobuf.DoubleValue) Field(org.apache.beam.sdk.schemas.Schema.Field) Assigner(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.assign.Assigner) Int32Value(com.google.protobuf.Int32Value) Duplication(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.Duplication) Iterator(java.util.Iterator) ReflectUtils(org.apache.beam.sdk.schemas.utils.ReflectUtils) Compound(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.bytecode.StackManipulation.Compound) ConvertValueForGetter(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.ConvertValueForGetter) ElementMatchers(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.matcher.ElementMatchers) UInt32Value(com.google.protobuf.UInt32Value) 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) Schema(org.apache.beam.sdk.schemas.Schema) TypeName(org.apache.beam.sdk.schemas.Schema.TypeName) EnumLite(com.google.protobuf.Internal.EnumLite) MessageLite(com.google.protobuf.MessageLite) Duration(com.google.protobuf.Duration) 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) TreeMap(java.util.TreeMap) ConvertValueForSetter(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.ConvertValueForSetter) InjectPackageStrategy(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.InjectPackageStrategy) Preconditions(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions) TypeConversionsFactory(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.TypeConversionsFactory) FieldValueGetter(org.apache.beam.sdk.schemas.FieldValueGetter) ForLoadedMethod(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.method.MethodDescription.ForLoadedMethod) Field(org.apache.beam.sdk.schemas.Schema.Field) FieldValueGetter(org.apache.beam.sdk.schemas.FieldValueGetter) FieldValueTypeInformation(org.apache.beam.sdk.schemas.FieldValueTypeInformation) Method(java.lang.reflect.Method) ForLoadedMethod(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.method.MethodDescription.ForLoadedMethod) ByteString(com.google.protobuf.ByteString) OneOfType(org.apache.beam.sdk.schemas.logicaltypes.OneOfType)

Aggregations

FieldValueTypeInformation (org.apache.beam.sdk.schemas.FieldValueTypeInformation)4 Method (java.lang.reflect.Method)3 ForLoadedMethod (org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.method.MethodDescription.ForLoadedMethod)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Modifier (java.lang.reflect.Modifier)2 Type (java.lang.reflect.Type)2 Arrays (java.util.Arrays)2 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 Experimental (org.apache.beam.sdk.annotations.Experimental)2 Kind (org.apache.beam.sdk.annotations.Experimental.Kind)2 Schema (org.apache.beam.sdk.schemas.Schema)2 Nullable (org.checkerframework.checker.nullness.qual.Nullable)2 BoolValue (com.google.protobuf.BoolValue)1 ByteString (com.google.protobuf.ByteString)1 BytesValue (com.google.protobuf.BytesValue)1 DoubleValue (com.google.protobuf.DoubleValue)1 Duration (com.google.protobuf.Duration)1 FloatValue (com.google.protobuf.FloatValue)1