use of org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.DefaultTypeConversionsFactory 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());
}
use of org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.DefaultTypeConversionsFactory 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.utils.ByteBuddyUtils.DefaultTypeConversionsFactory 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.utils.ByteBuddyUtils.DefaultTypeConversionsFactory 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());
}
use of org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.DefaultTypeConversionsFactory in project beam by apache.
the class AutoValueSchema method schemaTypeCreator.
@Override
public SchemaUserTypeCreator schemaTypeCreator(Class<?> targetClass, Schema schema) {
// If a static method is marked with @SchemaCreate, use that.
Method annotated = ReflectUtils.getAnnotatedCreateMethod(targetClass);
if (annotated != null) {
return JavaBeanUtils.getStaticCreator(targetClass, annotated, schema, AbstractGetterTypeSupplier.INSTANCE, new DefaultTypeConversionsFactory());
}
// Try to find a generated builder class. If one exists, use that to generate a
// SchemaTypeCreator for creating AutoValue objects.
SchemaUserTypeCreator creatorFactory = AutoValueUtils.getBuilderCreator(targetClass, schema, AbstractGetterTypeSupplier.INSTANCE);
if (creatorFactory != null) {
return creatorFactory;
}
// If there is no builder, there should be a package-private constructor in the generated
// class. Use that for creating AutoValue objects.
creatorFactory = AutoValueUtils.getConstructorCreator(targetClass, schema, AbstractGetterTypeSupplier.INSTANCE);
if (creatorFactory == null) {
throw new RuntimeException("Could not find a way to create AutoValue class " + targetClass);
}
return creatorFactory;
}
Aggregations