Java Code Examples for org.apache.beam.sdk.values.TypeDescriptor#getSupertype()

The following examples show how to use org.apache.beam.sdk.values.TypeDescriptor#getSupertype() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FieldTypeDescriptors.java    From beam with Apache License 2.0 6 votes vote down vote up
private static FieldType getArrayFieldType(TypeDescriptor typeDescriptor) {
  if (typeDescriptor.isArray()) {
    if (typeDescriptor.getComponentType().getType().equals(byte.class)) {
      return FieldType.BYTES;
    } else {
      return FieldType.array(fieldTypeForJavaType(typeDescriptor.getComponentType()));
    }
  }
  if (typeDescriptor.isSubtypeOf(TypeDescriptor.of(Collection.class))) {
    TypeDescriptor<Collection<?>> collection = typeDescriptor.getSupertype(Collection.class);
    if (collection.getType() instanceof ParameterizedType) {
      ParameterizedType ptype = (ParameterizedType) collection.getType();
      java.lang.reflect.Type[] params = ptype.getActualTypeArguments();
      checkArgument(params.length == 1);
      return FieldType.array(fieldTypeForJavaType(TypeDescriptor.of(params[0])));
    }
  }
  throw new RuntimeException("Could not determine array parameter type for field.");
}
 
Example 2
Source File: ReflectUtils.java    From beam with Apache License 2.0 6 votes vote down vote up
/** For an array T[] or a subclass of Iterable<T>, return a TypeDescriptor describing T. */
@Nullable
public static TypeDescriptor getIterableComponentType(TypeDescriptor valueType) {
  TypeDescriptor componentType = null;
  if (valueType.isArray()) {
    Type component = valueType.getComponentType().getType();
    if (!component.equals(byte.class)) {
      // Byte arrays are special cased since we have a schema type corresponding to them.
      componentType = TypeDescriptor.of(component);
    }
  } else if (valueType.isSubtypeOf(TypeDescriptor.of(Iterable.class))) {
    TypeDescriptor<Iterable<?>> collection = valueType.getSupertype(Iterable.class);
    if (collection.getType() instanceof ParameterizedType) {
      ParameterizedType ptype = (ParameterizedType) collection.getType();
      java.lang.reflect.Type[] params = ptype.getActualTypeArguments();
      checkArgument(params.length == 1);
      componentType = TypeDescriptor.of(params[0]);
    } else {
      throw new RuntimeException("Collection parameter is not parameterized!");
    }
  }
  return componentType;
}
 
Example 3
Source File: FieldTypeDescriptors.java    From beam with Apache License 2.0 5 votes vote down vote up
private static FieldType getIterableFieldType(TypeDescriptor typeDescriptor) {
  TypeDescriptor<Iterable<?>> iterable = typeDescriptor.getSupertype(Iterable.class);
  if (iterable.getType() instanceof ParameterizedType) {
    ParameterizedType ptype = (ParameterizedType) iterable.getType();
    java.lang.reflect.Type[] params = ptype.getActualTypeArguments();
    checkArgument(params.length == 1);
    return FieldType.iterable(fieldTypeForJavaType(TypeDescriptor.of(params[0])));
  }
  throw new RuntimeException("Could not determine array parameter type for field.");
}
 
Example 4
Source File: FieldTypeDescriptors.java    From beam with Apache License 2.0 5 votes vote down vote up
private static FieldType getMapFieldType(TypeDescriptor typeDescriptor) {
  TypeDescriptor<Collection<?>> map = typeDescriptor.getSupertype(Map.class);
  if (map.getType() instanceof ParameterizedType) {
    ParameterizedType ptype = (ParameterizedType) map.getType();
    java.lang.reflect.Type[] params = ptype.getActualTypeArguments();
    return FieldType.map(
        fieldTypeForJavaType(TypeDescriptor.of(params[0])),
        fieldTypeForJavaType(TypeDescriptor.of(params[1])));
  }
  throw new RuntimeException("Cound not determine array parameter type for field.");
}
 
Example 5
Source File: ReflectUtils.java    From beam with Apache License 2.0 5 votes vote down vote up
public static TypeDescriptor getMapType(TypeDescriptor valueType, int index) {
  TypeDescriptor mapType = null;
  if (valueType.isSubtypeOf(TypeDescriptor.of(Map.class))) {
    TypeDescriptor<Collection<?>> map = valueType.getSupertype(Map.class);
    if (map.getType() instanceof ParameterizedType) {
      ParameterizedType ptype = (ParameterizedType) map.getType();
      java.lang.reflect.Type[] params = ptype.getActualTypeArguments();
      mapType = TypeDescriptor.of(params[index]);
    } else {
      throw new RuntimeException("Map type is not parameterized! " + map);
    }
  }
  return mapType;
}
 
Example 6
Source File: DoFnSignatures.java    From beam with Apache License 2.0 4 votes vote down vote up
private static Map<String, DoFnSignature.StateDeclaration> analyzeStateDeclarations(
    ErrorReporter errors, Class<?> fnClazz) {

  Map<String, DoFnSignature.StateDeclaration> declarations = new HashMap<>();

  for (Field field : declaredFieldsWithAnnotation(DoFn.StateId.class, fnClazz, DoFn.class)) {
    // StateSpec fields may generally be private, but will be accessed via the signature
    field.setAccessible(true);
    String id = field.getAnnotation(DoFn.StateId.class).value();

    if (declarations.containsKey(id)) {
      errors.throwIllegalArgument(
          "Duplicate %s \"%s\", used on both of [%s] and [%s]",
          format(DoFn.StateId.class),
          id,
          field.toString(),
          declarations.get(id).field().toString());
      continue;
    }

    Class<?> stateSpecRawType = field.getType();
    if (!(TypeDescriptor.of(stateSpecRawType).isSubtypeOf(TypeDescriptor.of(StateSpec.class)))) {
      errors.throwIllegalArgument(
          "%s annotation on non-%s field [%s] that has class %s",
          format(DoFn.StateId.class),
          format(StateSpec.class),
          field.toString(),
          stateSpecRawType.getName());
      continue;
    }

    if (!Modifier.isFinal(field.getModifiers())) {
      errors.throwIllegalArgument(
          "Non-final field %s annotated with %s. State declarations must be final.",
          field.toString(), format(DoFn.StateId.class));
      continue;
    }

    Type stateSpecType = field.getGenericType();

    // A type descriptor for whatever type the @StateId-annotated class has, which
    // must be some subtype of StateSpec
    TypeDescriptor<? extends StateSpec<?>> stateSpecSubclassTypeDescriptor =
        (TypeDescriptor) TypeDescriptor.of(stateSpecType);

    // A type descriptor for StateSpec, with the generic type parameters filled
    // in according to the specialization of the subclass (or just straight params)
    TypeDescriptor<StateSpec<?>> stateSpecTypeDescriptor =
        (TypeDescriptor) stateSpecSubclassTypeDescriptor.getSupertype(StateSpec.class);

    // The type of the state, which may still have free type variables from the
    // context
    Type unresolvedStateType =
        ((ParameterizedType) stateSpecTypeDescriptor.getType()).getActualTypeArguments()[0];

    // By static typing this is already a well-formed State subclass
    TypeDescriptor<? extends State> stateType =
        (TypeDescriptor<? extends State>)
            TypeDescriptor.of(fnClazz).resolveType(unresolvedStateType);

    declarations.put(id, DoFnSignature.StateDeclaration.create(id, field, stateType));
  }

  return ImmutableMap.copyOf(declarations);
}