Search in sources :

Example 1 with FieldValueGetter

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

the class POJOUtilsTest method testGeneratedSimpleBoxedGetters.

@Test
public void testGeneratedSimpleBoxedGetters() {
    POJOWithBoxedFields pojo = new POJOWithBoxedFields((byte) 41, (short) 42, 43, 44L, true);
    List<FieldValueGetter> getters = POJOUtils.getGetters(POJOWithBoxedFields.class, POJO_WITH_BOXED_FIELDS_SCHEMA, JavaFieldTypeSupplier.INSTANCE, new DefaultTypeConversionsFactory());
    assertEquals((byte) 41, getters.get(0).get(pojo));
    assertEquals((short) 42, getters.get(1).get(pojo));
    assertEquals((int) 43, getters.get(2).get(pojo));
    assertEquals((long) 44, getters.get(3).get(pojo));
    assertTrue((Boolean) getters.get(4).get(pojo));
}
Also used : FieldValueGetter(org.apache.beam.sdk.schemas.FieldValueGetter) DefaultTypeConversionsFactory(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.DefaultTypeConversionsFactory) POJOWithBoxedFields(org.apache.beam.sdk.schemas.utils.TestPOJOs.POJOWithBoxedFields) Test(org.junit.Test)

Example 2 with FieldValueGetter

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

the class JavaBeanUtilsTest method testGeneratedSimpleGetters.

@Test
public void testGeneratedSimpleGetters() {
    SimpleBean simpleBean = new SimpleBean();
    simpleBean.setStr("field1");
    simpleBean.setaByte((byte) 41);
    simpleBean.setaShort((short) 42);
    simpleBean.setAnInt(43);
    simpleBean.setaLong(44);
    simpleBean.setaBoolean(true);
    simpleBean.setDateTime(DateTime.parse("1979-03-14"));
    simpleBean.setInstant(DateTime.parse("1979-03-15").toInstant());
    simpleBean.setBytes("bytes1".getBytes(StandardCharsets.UTF_8));
    simpleBean.setByteBuffer(ByteBuffer.wrap("bytes2".getBytes(StandardCharsets.UTF_8)));
    simpleBean.setBigDecimal(new BigDecimal(42));
    simpleBean.setStringBuilder(new StringBuilder("stringBuilder"));
    List<FieldValueGetter> getters = JavaBeanUtils.getGetters(SimpleBean.class, SIMPLE_BEAN_SCHEMA, new JavaBeanSchema.GetterTypeSupplier(), new DefaultTypeConversionsFactory());
    assertEquals(12, getters.size());
    assertEquals("str", getters.get(0).name());
    assertEquals("field1", getters.get(0).get(simpleBean));
    assertEquals((byte) 41, getters.get(1).get(simpleBean));
    assertEquals((short) 42, getters.get(2).get(simpleBean));
    assertEquals((int) 43, getters.get(3).get(simpleBean));
    assertEquals((long) 44, getters.get(4).get(simpleBean));
    assertTrue((Boolean) getters.get(5).get(simpleBean));
    assertEquals(DateTime.parse("1979-03-14").toInstant(), getters.get(6).get(simpleBean));
    assertEquals(DateTime.parse("1979-03-15").toInstant(), getters.get(7).get(simpleBean));
    assertArrayEquals("Unexpected bytes", "bytes1".getBytes(StandardCharsets.UTF_8), (byte[]) getters.get(8).get(simpleBean));
    assertArrayEquals("Unexpected bytes", "bytes2".getBytes(StandardCharsets.UTF_8), (byte[]) getters.get(9).get(simpleBean));
    assertEquals(new BigDecimal(42), getters.get(10).get(simpleBean));
    assertEquals("stringBuilder", getters.get(11).get(simpleBean).toString());
}
Also used : FieldValueGetter(org.apache.beam.sdk.schemas.FieldValueGetter) JavaBeanSchema(org.apache.beam.sdk.schemas.JavaBeanSchema) SimpleBean(org.apache.beam.sdk.schemas.utils.TestJavaBeans.SimpleBean) DefaultTypeConversionsFactory(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.DefaultTypeConversionsFactory) GetterTypeSupplier(org.apache.beam.sdk.schemas.JavaBeanSchema.GetterTypeSupplier) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 3 with FieldValueGetter

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

the class POJOUtils method createGetter.

/**
 * Generate the following {@link FieldValueSetter} class for the {@link Field}.
 *
 * <pre><code>
 *   class Getter implements {@literal FieldValueGetter<POJO, FieldType>} {
 *     {@literal @}Override public String name() { return field.getName(); }
 *     {@literal @}Override public Class type() { return field.getType(); }
 *     {@literal @}Override public FieldType get(POJO pojo) {
 *        return convert(pojo.field);
 *      }
 *   }
 * </code></pre>
 */
@SuppressWarnings("unchecked")
@Nullable
static <ObjectT, ValueT> FieldValueGetter<ObjectT, ValueT> createGetter(FieldValueTypeInformation typeInformation, TypeConversionsFactory typeConversionsFactory) {
    Field field = typeInformation.getField();
    DynamicType.Builder<FieldValueGetter> builder = ByteBuddyUtils.subclassGetterInterface(BYTE_BUDDY, field.getDeclaringClass(), typeConversionsFactory.createTypeConversion(false).convert(TypeDescriptor.of(field.getType())));
    builder = implementGetterMethods(builder, field, typeInformation.getName(), 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) FieldValueGetter(org.apache.beam.sdk.schemas.FieldValueGetter) 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) InvocationTargetException(java.lang.reflect.InvocationTargetException) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 4 with FieldValueGetter

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

the class POJOUtilsTest method testGeneratedSimpleGetters.

@Test
public void testGeneratedSimpleGetters() {
    SimplePOJO simplePojo = new SimplePOJO("field1", (byte) 41, (short) 42, 43, 44L, true, DATE, INSTANT, BYTE_ARRAY, BYTE_BUFFER, new BigDecimal(42), new StringBuilder("stringBuilder"));
    List<FieldValueGetter> getters = POJOUtils.getGetters(SimplePOJO.class, SIMPLE_POJO_SCHEMA, JavaFieldTypeSupplier.INSTANCE, new DefaultTypeConversionsFactory());
    assertEquals(12, getters.size());
    assertEquals("str", getters.get(0).name());
    assertEquals("field1", getters.get(0).get(simplePojo));
    assertEquals((byte) 41, getters.get(1).get(simplePojo));
    assertEquals((short) 42, getters.get(2).get(simplePojo));
    assertEquals((int) 43, getters.get(3).get(simplePojo));
    assertEquals((long) 44, getters.get(4).get(simplePojo));
    assertTrue((Boolean) getters.get(5).get(simplePojo));
    assertEquals(DATE.toInstant(), getters.get(6).get(simplePojo));
    assertEquals(INSTANT, getters.get(7).get(simplePojo));
    assertArrayEquals("Unexpected bytes", BYTE_ARRAY, (byte[]) getters.get(8).get(simplePojo));
    assertArrayEquals("Unexpected bytes", BYTE_BUFFER.array(), (byte[]) getters.get(9).get(simplePojo));
    assertEquals(new BigDecimal(42), getters.get(10).get(simplePojo));
    assertEquals("stringBuilder", getters.get(11).get(simplePojo));
}
Also used : FieldValueGetter(org.apache.beam.sdk.schemas.FieldValueGetter) DefaultTypeConversionsFactory(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.DefaultTypeConversionsFactory) SimplePOJO(org.apache.beam.sdk.schemas.utils.TestPOJOs.SimplePOJO) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 5 with FieldValueGetter

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

the class JavaBeanUtilsTest method testGeneratedSimpleBoxedGetters.

@Test
public void testGeneratedSimpleBoxedGetters() {
    BeanWithBoxedFields bean = new BeanWithBoxedFields();
    bean.setaByte((byte) 41);
    bean.setaShort((short) 42);
    bean.setAnInt(43);
    bean.setaLong(44L);
    bean.setaBoolean(true);
    List<FieldValueGetter> getters = JavaBeanUtils.getGetters(BeanWithBoxedFields.class, BEAN_WITH_BOXED_FIELDS_SCHEMA, new JavaBeanSchema.GetterTypeSupplier(), new DefaultTypeConversionsFactory());
    assertEquals((byte) 41, getters.get(0).get(bean));
    assertEquals((short) 42, getters.get(1).get(bean));
    assertEquals((int) 43, getters.get(2).get(bean));
    assertEquals((long) 44, getters.get(3).get(bean));
    assertTrue((Boolean) getters.get(4).get(bean));
}
Also used : FieldValueGetter(org.apache.beam.sdk.schemas.FieldValueGetter) JavaBeanSchema(org.apache.beam.sdk.schemas.JavaBeanSchema) BeanWithBoxedFields(org.apache.beam.sdk.schemas.utils.TestJavaBeans.BeanWithBoxedFields) DefaultTypeConversionsFactory(org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.DefaultTypeConversionsFactory) GetterTypeSupplier(org.apache.beam.sdk.schemas.JavaBeanSchema.GetterTypeSupplier) Test(org.junit.Test)

Aggregations

FieldValueGetter (org.apache.beam.sdk.schemas.FieldValueGetter)6 DefaultTypeConversionsFactory (org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.DefaultTypeConversionsFactory)4 Test (org.junit.Test)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 BigDecimal (java.math.BigDecimal)2 JavaBeanSchema (org.apache.beam.sdk.schemas.JavaBeanSchema)2 GetterTypeSupplier (org.apache.beam.sdk.schemas.JavaBeanSchema.GetterTypeSupplier)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 Int32Value (com.google.protobuf.Int32Value)1 Int64Value (com.google.protobuf.Int64Value)1 EnumLite (com.google.protobuf.Internal.EnumLite)1 MessageLite (com.google.protobuf.MessageLite)1 ProtocolMessageEnum (com.google.protobuf.ProtocolMessageEnum)1 StringValue (com.google.protobuf.StringValue)1 Timestamp (com.google.protobuf.Timestamp)1