use of org.apache.beam.model.pipeline.v1.RunnerApi.PTransform in project beam by apache.
the class JavaClassLookupTransformProvider method getTransform.
@Override
public PTransform<PInput, POutput> getTransform(FunctionSpec spec) {
JavaClassLookupPayload payload;
try {
payload = JavaClassLookupPayload.parseFrom(spec.getPayload());
} catch (InvalidProtocolBufferException e) {
throw new IllegalArgumentException("Invalid payload type for URN " + getUrn(ExpansionMethods.Enum.JAVA_CLASS_LOOKUP), e);
}
String className = payload.getClassName();
try {
AllowedClass allowlistClass = allowList.getAllowedClass(className);
Class<PTransform<InputT, OutputT>> transformClass = (Class<PTransform<InputT, OutputT>>) ReflectHelpers.findClassLoader().loadClass(className);
PTransform<PInput, POutput> transform;
Row constructorRow = decodeRow(payload.getConstructorSchema(), payload.getConstructorPayload());
if (payload.getConstructorMethod().isEmpty()) {
Constructor<?>[] constructors = transformClass.getConstructors();
Constructor<PTransform<InputT, OutputT>> constructor = findMappingConstructor(constructors, payload);
Object[] parameterValues = getParameterValues(constructor.getParameters(), constructorRow, constructor.getGenericParameterTypes());
transform = (PTransform<PInput, POutput>) constructor.newInstance(parameterValues);
} else {
Method[] methods = transformClass.getMethods();
Method method = findMappingConstructorMethod(methods, payload, allowlistClass);
Object[] parameterValues = getParameterValues(method.getParameters(), constructorRow, method.getGenericParameterTypes());
transform = (PTransform<PInput, POutput>) method.invoke(null, /* static */
parameterValues);
}
return applyBuilderMethods(transform, payload, allowlistClass);
} catch (ClassNotFoundException e) {
throw new IllegalArgumentException("Could not find class " + className, e);
} catch (InstantiationException | IllegalArgumentException | IllegalAccessException | InvocationTargetException e) {
throw new IllegalArgumentException("Could not instantiate class " + className, e);
}
}
Aggregations