use of org.apache.beam.model.pipeline.v1.ExternalTransforms.JavaClassLookupPayload in project beam by apache.
the class JavaClassLookupTransformProvider method findMappingConstructorMethod.
private Method findMappingConstructorMethod(Method[] methods, JavaClassLookupPayload payload, AllowedClass allowListClass) {
Row constructorRow = decodeRow(payload.getConstructorSchema(), payload.getConstructorPayload());
List<Method> mappingConstructorMethods = Arrays.stream(methods).filter(m -> isConstructorMethodForName(m, payload.getConstructorMethod(), allowListClass)).filter(m -> m.getParameterCount() == payload.getConstructorSchema().getFieldsCount()).filter(m -> parametersCompatible(m.getParameters(), constructorRow)).collect(Collectors.toList());
if (mappingConstructorMethods.size() == 0) {
throw new RuntimeException("Could not find a matching constructor method. When using field names, make sure they " + "are available in the compiled Java class.");
} else if (mappingConstructorMethods.size() != 1) {
throw new RuntimeException("Expected to find a single mapping constructor method but found " + mappingConstructorMethods.size() + " Payload was " + payload);
}
return mappingConstructorMethods.get(0);
}
use of org.apache.beam.model.pipeline.v1.ExternalTransforms.JavaClassLookupPayload in project beam by apache.
the class JavaClassLookupTransformProvider method findMappingConstructor.
private Constructor<PTransform<InputT, OutputT>> findMappingConstructor(Constructor<?>[] constructors, JavaClassLookupPayload payload) {
Row constructorRow = decodeRow(payload.getConstructorSchema(), payload.getConstructorPayload());
List<Constructor<?>> mappingConstructors = Arrays.stream(constructors).filter(c -> c.getParameterCount() == payload.getConstructorSchema().getFieldsCount()).filter(c -> parametersCompatible(c.getParameters(), constructorRow)).collect(Collectors.toList());
if (mappingConstructors.size() == 0) {
throw new RuntimeException("Could not find a matching constructor. When using field names, make sure they are " + "available in the compiled Java class.");
} else if (mappingConstructors.size() != 1) {
throw new RuntimeException("Expected to find a single mapping constructor but found " + mappingConstructors.size());
}
return (Constructor<PTransform<InputT, OutputT>>) mappingConstructors.get(0);
}
use of org.apache.beam.model.pipeline.v1.ExternalTransforms.JavaClassLookupPayload in project beam by apache.
the class JavaClassLookupTransformProvider method applyBuilderMethods.
private PTransform<PInput, POutput> applyBuilderMethods(PTransform<PInput, POutput> transform, JavaClassLookupPayload payload, AllowedClass allowListClass) {
for (BuilderMethod builderMethod : payload.getBuilderMethodsList()) {
Method method = getMethod(transform, builderMethod, allowListClass);
try {
Row builderMethodRow = decodeRow(builderMethod.getSchema(), builderMethod.getPayload());
transform = (PTransform<PInput, POutput>) method.invoke(transform, getParameterValues(method.getParameters(), builderMethodRow, method.getGenericParameterTypes()));
} catch (IllegalAccessException | InvocationTargetException e) {
throw new IllegalArgumentException("Could not invoke the builder method " + builderMethod + " on transform " + transform + " with parameter schema " + builderMethod.getSchema(), e);
}
}
return transform;
}
use of org.apache.beam.model.pipeline.v1.ExternalTransforms.JavaClassLookupPayload 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