use of org.apache.beam.vendor.bytebuddy.v1_11_0.net.bytebuddy.implementation.MethodCall 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