use of org.apache.beam.sdk.schemas.SchemaUserTypeCreator 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.SchemaUserTypeCreator in project beam by apache.
the class AvroByteBuddyUtils method createCreator.
private static <T> SchemaUserTypeCreator createCreator(Class<T> clazz, Schema schema) {
Constructor baseConstructor = null;
Constructor[] constructors = clazz.getDeclaredConstructors();
for (Constructor constructor : constructors) {
// TODO: This assumes that Avro only generates one constructor with this many fields.
if (constructor.getParameterCount() == schema.getFieldCount()) {
baseConstructor = constructor;
}
}
if (baseConstructor == null) {
throw new RuntimeException("No matching constructor found for class " + clazz);
}
// Generate a method call to create and invoke the SpecificRecord's constructor. .
MethodCall construct = MethodCall.construct(baseConstructor);
for (int i = 0; i < baseConstructor.getParameterTypes().length; ++i) {
Class<?> baseType = baseConstructor.getParameterTypes()[i];
construct = construct.with(readAndConvertParameter(baseType, i), baseType);
}
try {
DynamicType.Builder<SchemaUserTypeCreator> builder = BYTE_BUDDY.with(new InjectPackageStrategy(clazz)).subclass(SchemaUserTypeCreator.class).method(ElementMatchers.named("create")).intercept(construct);
return builder.visit(new AsmVisitorWrapper.ForDeclaredMethods().writerFlags(ClassWriter.COMPUTE_FRAMES)).make().load(ReflectHelpers.findClassLoader(clazz.getClassLoader()), ClassLoadingStrategy.Default.INJECTION).getLoaded().getDeclaredConstructor().newInstance();
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
throw new RuntimeException("Unable to generate a getter for class " + clazz + " with schema " + schema);
}
}
Aggregations