use of org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.DefaultTypeConversionsFactory in project beam by apache.
the class JavaFieldSchema 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 POJOUtils.getStaticCreator(targetClass, annotated, schema, JavaFieldTypeSupplier.INSTANCE, new DefaultTypeConversionsFactory());
}
// If a Constructor was tagged with @SchemaCreate, invoke that constructor.
Constructor<?> constructor = ReflectUtils.getAnnotatedConstructor(targetClass);
if (constructor != null) {
return POJOUtils.getConstructorCreator(targetClass, constructor, schema, JavaFieldTypeSupplier.INSTANCE, new DefaultTypeConversionsFactory());
}
return POJOUtils.getSetFieldCreator(targetClass, schema, JavaFieldTypeSupplier.INSTANCE, new DefaultTypeConversionsFactory());
}
use of org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.DefaultTypeConversionsFactory in project beam by apache.
the class AutoValueUtils method getConstructorCreator.
/**
* Try to find an accessible constructor for creating an AutoValue class. Otherwise return null.
*/
@Nullable
public static SchemaUserTypeCreator getConstructorCreator(Class<?> clazz, Schema schema, FieldValueTypeSupplier fieldValueTypeSupplier) {
Class<?> generatedClass = getAutoValueGenerated(clazz);
List<FieldValueTypeInformation> schemaTypes = fieldValueTypeSupplier.get(clazz, schema);
Optional<Constructor<?>> constructor = Arrays.stream(generatedClass.getDeclaredConstructors()).filter(c -> !Modifier.isPrivate(c.getModifiers())).filter(c -> matchConstructor(c, schemaTypes)).findAny();
return constructor.map(c -> JavaBeanUtils.getConstructorCreator(generatedClass, c, schema, fieldValueTypeSupplier, new DefaultTypeConversionsFactory())).orElse(null);
}
use of org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.DefaultTypeConversionsFactory in project beam by apache.
the class JavaBeanSchema 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, GetterTypeSupplier.INSTANCE, new DefaultTypeConversionsFactory());
}
// If a Constructor was tagged with @SchemaCreate, invoke that constructor.
Constructor<?> constructor = ReflectUtils.getAnnotatedConstructor(targetClass);
if (constructor != null) {
return JavaBeanUtils.getConstructorCreator(targetClass, constructor, schema, GetterTypeSupplier.INSTANCE, new DefaultTypeConversionsFactory());
}
// Else try to make a setter-based creator
Factory<SchemaUserTypeCreator> setterBasedFactory = new SetterBasedCreatorFactory(new JavaBeanSetterFactory());
return setterBasedFactory.create(targetClass, schema);
}
use of org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.DefaultTypeConversionsFactory 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);
}
use of org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.DefaultTypeConversionsFactory 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));
}
Aggregations