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));
}
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());
}
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);
}
}
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));
}
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));
}
Aggregations