use of org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.InjectPackageStrategy in project beam by apache.
the class ProtoByteBuddyUtils method createBuilderCreator.
@Experimental(Kind.SCHEMAS)
static <ProtoBuilderT extends MessageLite.Builder> SchemaUserTypeCreator createBuilderCreator(Class<?> protoClass, Class<?> builderClass, List<FieldValueSetter<ProtoBuilderT, Object>> setters, Schema schema) {
try {
DynamicType.Builder<Supplier> builder = BYTE_BUDDY.with(new InjectPackageStrategy(builderClass)).subclass(Supplier.class).method(ElementMatchers.named("get")).intercept(new BuilderSupplier(protoClass));
Supplier supplier = builder.visit(new AsmVisitorWrapper.ForDeclaredMethods().writerFlags(ClassWriter.COMPUTE_FRAMES)).make().load(ReflectHelpers.findClassLoader(), ClassLoadingStrategy.Default.INJECTION).getLoaded().getDeclaredConstructor().newInstance();
return new ProtoCreatorFactory<>(supplier, setters);
} catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
throw new RuntimeException("Unable to generate a creator for class " + builderClass + " with schema " + schema);
}
}
use of org.apache.beam.sdk.schemas.utils.ByteBuddyUtils.InjectPackageStrategy 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