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