use of org.apache.beam.sdk.schemas.FieldValueTypeInformation 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.FieldValueTypeInformation in project beam by apache.
the class AutoValueUtils method getBuilderCreator.
/**
* Try to find an accessible builder class for creating an AutoValue class. Otherwise return null.
*/
@Nullable
public static SchemaUserTypeCreator getBuilderCreator(Class<?> clazz, Schema schema, FieldValueTypeSupplier fieldValueTypeSupplier) {
Class<?> builderClass = getAutoValueGeneratedBuilder(clazz);
if (builderClass == null) {
return null;
}
Map<String, FieldValueTypeInformation> setterTypes = ReflectUtils.getMethods(builderClass).stream().filter(ReflectUtils::isSetter).map(FieldValueTypeInformation::forSetter).collect(Collectors.toMap(FieldValueTypeInformation::getName, Function.identity()));
List<FieldValueTypeInformation> setterMethods = // The builder methods to call in order.
Lists.newArrayList();
List<FieldValueTypeInformation> schemaTypes = fieldValueTypeSupplier.get(clazz, schema);
for (FieldValueTypeInformation type : schemaTypes) {
String autoValueFieldName = ReflectUtils.stripGetterPrefix(type.getMethod().getName());
FieldValueTypeInformation setterType = setterTypes.get(autoValueFieldName);
if (setterType == null) {
throw new RuntimeException("AutoValue builder class " + builderClass + " did not contain " + "a setter for " + autoValueFieldName);
}
setterMethods.add(setterType);
}
Method buildMethod = ReflectUtils.getMethods(builderClass).stream().filter(m -> m.getName().equals("build")).findAny().orElseThrow(() -> new RuntimeException("No build method in builder"));
return createBuilderCreator(builderClass, setterMethods, buildMethod, schema, schemaTypes);
}
use of org.apache.beam.sdk.schemas.FieldValueTypeInformation in project beam by apache.
the class JavaBeanUtils method validateJavaBean.
// Make sure that there are matching setters and getters.
public static void validateJavaBean(List<FieldValueTypeInformation> getters, List<FieldValueTypeInformation> setters, Schema schema) {
Map<String, FieldValueTypeInformation> setterMap = new HashMap<>();
int bound = schema.getFieldCount();
for (int i = 0; i < bound; i++) {
Integer integer = i;
if (setterMap.put(schema.getField(integer).getName(), setters.get(integer)) != null) {
throw new IllegalStateException("Duplicate key");
}
}
for (FieldValueTypeInformation type : getters) {
FieldValueTypeInformation setterType = setterMap.get(type.getName());
if (setterType == null) {
throw new RuntimeException(String.format("Java Bean '%s' contains a getter for field '%s', but does not contain a matching setter. %s", type.getMethod().getDeclaringClass(), type.getName(), CONSTRUCTOR_HELP_STRING));
}
if (!type.getType().equals(setterType.getType())) {
throw new RuntimeException(String.format("Java Bean '%s' contains a setter for field '%s' that has a mismatching type. %s", type.getMethod().getDeclaringClass(), type.getName(), CONSTRUCTOR_HELP_STRING));
}
if (!type.isNullable() == setterType.isNullable()) {
throw new RuntimeException(String.format("Java Bean '%s' contains a setter for field '%s' that has a mismatching nullable attribute. %s", type.getMethod().getDeclaringClass(), type.getName(), CONSTRUCTOR_HELP_STRING));
}
}
}
use of org.apache.beam.sdk.schemas.FieldValueTypeInformation in project beam by apache.
the class ProtoByteBuddyUtils method createGetter.
private static <ProtoT> FieldValueGetter createGetter(FieldValueTypeInformation fieldValueTypeInformation, TypeConversionsFactory typeConversionsFactory, Class clazz, Multimap<String, Method> methods, Field field, FieldValueTypeSupplier fieldValueTypeSupplier) {
if (field.getType().isLogicalType(OneOfType.IDENTIFIER)) {
OneOfType oneOfType = field.getType().getLogicalType(OneOfType.class);
// The case accessor method in the proto is named getOneOfNameCase.
Method caseMethod = getProtoGetter(methods, field.getName() + "_case", FieldType.logicalType(oneOfType.getCaseEnumType()));
// Create a map of case enum value to getter. This must be sorted, so store in a TreeMap.
TreeMap<Integer, FieldValueGetter<ProtoT, OneOfType.Value>> oneOfGetters = Maps.newTreeMap();
Map<String, FieldValueTypeInformation> oneOfFieldTypes = fieldValueTypeSupplier.get(clazz, oneOfType.getOneOfSchema()).stream().collect(Collectors.toMap(FieldValueTypeInformation::getName, f -> f));
for (Field oneOfField : oneOfType.getOneOfSchema().getFields()) {
int protoFieldIndex = getFieldNumber(oneOfField);
FieldValueGetter oneOfFieldGetter = createGetter(oneOfFieldTypes.get(oneOfField.getName()), typeConversionsFactory, clazz, methods, oneOfField, fieldValueTypeSupplier);
oneOfGetters.put(protoFieldIndex, oneOfFieldGetter);
}
return createOneOfGetter(fieldValueTypeInformation, oneOfGetters, clazz, oneOfType, caseMethod);
} else {
return JavaBeanUtils.createGetter(fieldValueTypeInformation, typeConversionsFactory);
}
}
Aggregations