Search in sources :

Example 1 with FieldValueSetter

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

the class ProtoByteBuddyUtils method getProtoFieldValueSetter.

private static <ProtoBuilderT extends MessageLite.Builder> FieldValueSetter<ProtoBuilderT, Object> getProtoFieldValueSetter(Field field, Multimap<String, Method> methods, Class<ProtoBuilderT> builderClass) {
    if (field.getType().isLogicalType(OneOfType.IDENTIFIER)) {
        OneOfType oneOfType = field.getType().getLogicalType(OneOfType.class);
        TreeMap<Integer, FieldValueSetter<ProtoBuilderT, Object>> oneOfSetters = Maps.newTreeMap();
        for (Field oneOfField : oneOfType.getOneOfSchema().getFields()) {
            FieldValueSetter setter = getProtoFieldValueSetter(oneOfField, methods, builderClass);
            oneOfSetters.put(getFieldNumber(oneOfField), setter);
        }
        return createOneOfSetter(field.getName(), oneOfSetters, builderClass);
    } else {
        Method method = getProtoSetter(methods, field.getName(), field.getType());
        return JavaBeanUtils.createSetter(FieldValueTypeInformation.forSetter(method, protoSetterPrefix(field.getType())), new ProtoTypeConversionsFactory());
    }
}
Also used : Field(org.apache.beam.sdk.schemas.Schema.Field) Method(java.lang.reflect.Method) ForLoadedMethod(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.method.MethodDescription.ForLoadedMethod) FieldValueSetter(org.apache.beam.sdk.schemas.FieldValueSetter) OneOfType(org.apache.beam.sdk.schemas.logicaltypes.OneOfType)

Example 2 with FieldValueSetter

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

the class POJOUtilsTest method testGeneratedSimpleSetters.

@Test
public void testGeneratedSimpleSetters() {
    SimplePOJO simplePojo = new SimplePOJO();
    List<FieldValueSetter> setters = POJOUtils.getSetters(SimplePOJO.class, SIMPLE_POJO_SCHEMA, JavaFieldTypeSupplier.INSTANCE, new DefaultTypeConversionsFactory());
    assertEquals(12, setters.size());
    setters.get(0).set(simplePojo, "field1");
    setters.get(1).set(simplePojo, (byte) 41);
    setters.get(2).set(simplePojo, (short) 42);
    setters.get(3).set(simplePojo, (int) 43);
    setters.get(4).set(simplePojo, (long) 44);
    setters.get(5).set(simplePojo, true);
    setters.get(6).set(simplePojo, DATE.toInstant());
    setters.get(7).set(simplePojo, INSTANT);
    setters.get(8).set(simplePojo, BYTE_ARRAY);
    setters.get(9).set(simplePojo, BYTE_BUFFER.array());
    setters.get(10).set(simplePojo, new BigDecimal(42));
    setters.get(11).set(simplePojo, "stringBuilder");
    assertEquals("field1", simplePojo.str);
    assertEquals((byte) 41, simplePojo.aByte);
    assertEquals((short) 42, simplePojo.aShort);
    assertEquals((int) 43, simplePojo.anInt);
    assertEquals((long) 44, simplePojo.aLong);
    assertTrue(simplePojo.aBoolean);
    assertEquals(DATE, simplePojo.dateTime);
    assertEquals(INSTANT, simplePojo.instant);
    assertArrayEquals("Unexpected bytes", BYTE_ARRAY, simplePojo.bytes);
    assertEquals(BYTE_BUFFER, simplePojo.byteBuffer);
    assertEquals(new BigDecimal(42), simplePojo.bigDecimal);
    assertEquals("stringBuilder", simplePojo.stringBuilder.toString());
}
Also used : DefaultTypeConversionsFactory(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.DefaultTypeConversionsFactory) FieldValueSetter(org.apache.beam.sdk.schemas.FieldValueSetter) SimplePOJO(org.apache.beam.sdk.schemas.utils.TestPOJOs.SimplePOJO) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 3 with FieldValueSetter

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

the class JavaBeanUtilsTest method testGeneratedSimpleSetters.

@Test
public void testGeneratedSimpleSetters() {
    SimpleBean simpleBean = new SimpleBean();
    List<FieldValueSetter> setters = JavaBeanUtils.getSetters(SimpleBean.class, SIMPLE_BEAN_SCHEMA, new SetterTypeSupplier(), new DefaultTypeConversionsFactory());
    assertEquals(12, setters.size());
    setters.get(0).set(simpleBean, "field1");
    setters.get(1).set(simpleBean, (byte) 41);
    setters.get(2).set(simpleBean, (short) 42);
    setters.get(3).set(simpleBean, (int) 43);
    setters.get(4).set(simpleBean, (long) 44);
    setters.get(5).set(simpleBean, true);
    setters.get(6).set(simpleBean, DateTime.parse("1979-03-14").toInstant());
    setters.get(7).set(simpleBean, DateTime.parse("1979-03-15").toInstant());
    setters.get(8).set(simpleBean, "bytes1".getBytes(StandardCharsets.UTF_8));
    setters.get(9).set(simpleBean, "bytes2".getBytes(StandardCharsets.UTF_8));
    setters.get(10).set(simpleBean, new BigDecimal(42));
    setters.get(11).set(simpleBean, "stringBuilder");
    assertEquals("field1", simpleBean.getStr());
    assertEquals((byte) 41, simpleBean.getaByte());
    assertEquals((short) 42, simpleBean.getaShort());
    assertEquals((int) 43, simpleBean.getAnInt());
    assertEquals((long) 44, simpleBean.getaLong());
    assertTrue(simpleBean.isaBoolean());
    assertEquals(DateTime.parse("1979-03-14"), simpleBean.getDateTime());
    assertEquals(DateTime.parse("1979-03-15").toInstant(), simpleBean.getInstant());
    assertArrayEquals("Unexpected bytes", "bytes1".getBytes(StandardCharsets.UTF_8), simpleBean.getBytes());
    assertEquals(ByteBuffer.wrap("bytes2".getBytes(StandardCharsets.UTF_8)), simpleBean.getByteBuffer());
    assertEquals(new BigDecimal(42), simpleBean.getBigDecimal());
    assertEquals("stringBuilder", simpleBean.getStringBuilder().toString());
}
Also used : SimpleBean(org.apache.beam.sdk.schemas.utils.TestJavaBeans.SimpleBean) DefaultTypeConversionsFactory(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.DefaultTypeConversionsFactory) FieldValueSetter(org.apache.beam.sdk.schemas.FieldValueSetter) SetterTypeSupplier(org.apache.beam.sdk.schemas.JavaBeanSchema.SetterTypeSupplier) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 4 with FieldValueSetter

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

the class POJOUtils method createSetter.

/**
 * Generate the following {@link FieldValueSetter} class for the {@link Field}.
 *
 * <pre><code>
 *   class Setter implements {@literal FieldValueSetter<POJO, FieldType>} {
 *     {@literal @}Override public String name() { return field.getName(); }
 *     {@literal @}Override public Class type() { return field.getType(); }
 *     {@literal @}Override public Type elementType() { return elementType; }
 *     {@literal @}Override public Type mapKeyType() { return mapKeyType; }
 *     {@literal @}Override public Type mapValueType() { return mapValueType; }
 *     {@literal @}Override public void set(POJO pojo, FieldType value) {
 *        pojo.field = convert(value);
 *      }
 *   }
 * </code></pre>
 */
@SuppressWarnings("unchecked")
private static <ObjectT, ValueT> FieldValueSetter<ObjectT, ValueT> createSetter(FieldValueTypeInformation typeInformation, TypeConversionsFactory typeConversionsFactory) {
    Field field = typeInformation.getField();
    DynamicType.Builder<FieldValueSetter> builder = ByteBuddyUtils.subclassSetterInterface(BYTE_BUDDY, field.getDeclaringClass(), typeConversionsFactory.createTypeConversion(false).convert(TypeDescriptor.of(field.getType())));
    builder = implementSetterMethods(builder, field, typeConversionsFactory);
    try {
        return builder.visit(new AsmVisitorWrapper.ForDeclaredMethods().writerFlags(ClassWriter.COMPUTE_FRAMES)).make().load(ReflectHelpers.findClassLoader(field.getDeclaringClass().getClassLoader()), ClassLoadingStrategy.Default.INJECTION).getLoaded().getDeclaredConstructor().newInstance();
    } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
        throw new RuntimeException("Unable to generate a getter for field '" + field + "'.", e);
    }
}
Also used : ForLoadedField(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.field.FieldDescription.ForLoadedField) Field(java.lang.reflect.Field) DynamicType(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.DynamicType) AsmVisitorWrapper(org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.asm.AsmVisitorWrapper) FieldValueSetter(org.apache.beam.sdk.schemas.FieldValueSetter) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 5 with FieldValueSetter

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

the class POJOUtilsTest method testGeneratedByteBufferSetters.

@Test
public void testGeneratedByteBufferSetters() {
    POJOWithByteArray pojo = new POJOWithByteArray();
    List<FieldValueSetter> setters = POJOUtils.getSetters(POJOWithByteArray.class, POJO_WITH_BYTE_ARRAY_SCHEMA, JavaFieldTypeSupplier.INSTANCE, new DefaultTypeConversionsFactory());
    setters.get(0).set(pojo, BYTE_ARRAY);
    setters.get(1).set(pojo, BYTE_BUFFER.array());
    assertArrayEquals("not equal", BYTE_ARRAY, pojo.bytes1);
    assertEquals(BYTE_BUFFER, pojo.bytes2);
}
Also used : DefaultTypeConversionsFactory(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.DefaultTypeConversionsFactory) FieldValueSetter(org.apache.beam.sdk.schemas.FieldValueSetter) POJOWithByteArray(org.apache.beam.sdk.schemas.utils.TestPOJOs.POJOWithByteArray) Test(org.junit.Test)

Aggregations

FieldValueSetter (org.apache.beam.sdk.schemas.FieldValueSetter)8 DefaultTypeConversionsFactory (org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.DefaultTypeConversionsFactory)6 Test (org.junit.Test)6 SetterTypeSupplier (org.apache.beam.sdk.schemas.JavaBeanSchema.SetterTypeSupplier)3 BigDecimal (java.math.BigDecimal)2 Field (java.lang.reflect.Field)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 Field (org.apache.beam.sdk.schemas.Schema.Field)1 OneOfType (org.apache.beam.sdk.schemas.logicaltypes.OneOfType)1 BeanWithBoxedFields (org.apache.beam.sdk.schemas.utils.TestJavaBeans.BeanWithBoxedFields)1 BeanWithByteArray (org.apache.beam.sdk.schemas.utils.TestJavaBeans.BeanWithByteArray)1 SimpleBean (org.apache.beam.sdk.schemas.utils.TestJavaBeans.SimpleBean)1 POJOWithBoxedFields (org.apache.beam.sdk.schemas.utils.TestPOJOs.POJOWithBoxedFields)1 POJOWithByteArray (org.apache.beam.sdk.schemas.utils.TestPOJOs.POJOWithByteArray)1 SimplePOJO (org.apache.beam.sdk.schemas.utils.TestPOJOs.SimplePOJO)1 AsmVisitorWrapper (org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.asm.AsmVisitorWrapper)1 ForLoadedField (org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.field.FieldDescription.ForLoadedField)1 ForLoadedMethod (org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.description.method.MethodDescription.ForLoadedMethod)1 DynamicType (org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.dynamic.DynamicType)1