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

The following examples show how to use org.apache.beam.sdk.values.TypeDescriptor#isSubtypeOf() . 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: HBaseMutationCoder.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
public <T> Coder<T> coderFor(
    TypeDescriptor<T> typeDescriptor, List<? extends Coder<?>> componentCoders)
    throws CannotProvideCoderException {
  if (!typeDescriptor.isSubtypeOf(HBASE_MUTATION_TYPE_DESCRIPTOR)) {
    throw new CannotProvideCoderException(
        String.format(
            "Cannot provide %s because %s is not a subclass of %s",
            HBaseMutationCoder.class.getSimpleName(),
            typeDescriptor,
            Mutation.class.getName()));
  }

  try {
    @SuppressWarnings("unchecked")
    Coder<T> coder = (Coder<T>) HBaseMutationCoder.of();
    return coder;
  } catch (IllegalArgumentException e) {
    throw new CannotProvideCoderException(e);
  }
}
 
Example 2
Source File: ProtoCoder.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
public <T> Coder<T> coderFor(
    TypeDescriptor<T> typeDescriptor, List<? extends Coder<?>> componentCoders)
    throws CannotProvideCoderException {
  if (!typeDescriptor.isSubtypeOf(MESSAGE_TYPE)) {
    throw new CannotProvideCoderException(
        String.format(
            "Cannot provide %s because %s is not a subclass of %s",
            ProtoCoder.class.getSimpleName(), typeDescriptor, Message.class.getName()));
  }

  @SuppressWarnings("unchecked")
  TypeDescriptor<? extends Message> messageType =
      (TypeDescriptor<? extends Message>) typeDescriptor;
  try {
    @SuppressWarnings("unchecked")
    Coder<T> coder = (Coder<T>) ProtoCoder.of(messageType);
    return coder;
  } catch (IllegalArgumentException e) {
    throw new CannotProvideCoderException(e);
  }
}
 
Example 3
Source File: DynamicProtoCoder.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
public <T> Coder<T> coderFor(
    TypeDescriptor<T> typeDescriptor, List<? extends Coder<?>> componentCoders)
    throws CannotProvideCoderException {
  if (!typeDescriptor.isSubtypeOf(MESSAGE_TYPE)) {
    throw new CannotProvideCoderException(
        String.format(
            "Cannot provide %s because %s is not a subclass of %s",
            DynamicProtoCoder.class.getSimpleName(), typeDescriptor, Message.class.getName()));
  }

  @SuppressWarnings("unchecked")
  TypeDescriptor<? extends Message> messageType =
      (TypeDescriptor<? extends Message>) typeDescriptor;
  try {
    @SuppressWarnings("unchecked")
    Coder<T> coder = (Coder<T>) DynamicProtoCoder.of(messageType);
    return coder;
  } catch (IllegalArgumentException e) {
    throw new CannotProvideCoderException(e);
  }
}
 
Example 4
Source File: WritableCoder.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
public <T> Coder<T> coderFor(
    TypeDescriptor<T> typeDescriptor, List<? extends Coder<?>> componentCoders)
    throws CannotProvideCoderException {
  if (!typeDescriptor.isSubtypeOf(WRITABLE_TYPE)) {
    throw new CannotProvideCoderException(
        String.format(
            "Cannot provide %s because %s does not implement the interface %s",
            WritableCoder.class.getSimpleName(), typeDescriptor, Writable.class.getName()));
  }

  try {
    @SuppressWarnings("unchecked")
    Coder<T> coder = WritableCoder.of((Class) typeDescriptor.getRawType());
    return coder;
  } catch (IllegalArgumentException e) {
    throw new CannotProvideCoderException(e);
  }
}
 
Example 5
Source File: FieldTypeDescriptors.java    From beam with Apache License 2.0 6 votes vote down vote up
/** Get a {@link FieldType} from a {@link TypeDescriptor}. */
public static FieldType fieldTypeForJavaType(TypeDescriptor typeDescriptor) {
  // TODO: Convert for registered logical types.
  if (typeDescriptor.isArray()
      || typeDescriptor.isSubtypeOf(TypeDescriptor.of(Collection.class))) {
    return getArrayFieldType(typeDescriptor);
  } else if (typeDescriptor.isSubtypeOf(TypeDescriptor.of(Map.class))) {
    return getMapFieldType(typeDescriptor);
  } else if (typeDescriptor.isSubtypeOf(TypeDescriptor.of(Iterable.class))) {
    return getIterableFieldType(typeDescriptor);
  } else if (typeDescriptor.isSubtypeOf(TypeDescriptor.of(Row.class))) {
    throw new IllegalArgumentException(
        "Cannot automatically determine a field type from a Row class"
            + " as we cannot determine the schema. You should set a field type explicitly.");
  } else {
    TypeName typeName = PRIMITIVE_MAPPING.inverse().get(typeDescriptor);
    if (typeName == null) {
      throw new RuntimeException("Couldn't find field type for " + typeDescriptor);
    }
    return FieldType.of(typeName);
  }
}
 
Example 6
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 7
Source File: AvroUtils.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
protected StackManipulation convertDefault(TypeDescriptor<?> type) {
  final ForLoadedType byteArrayType = new ForLoadedType(byte[].class);
  if (type.isSubtypeOf(TypeDescriptor.of(GenericFixed.class))) {
    // Generate the following code:
    // return new T((byte[]) value);
    ForLoadedType loadedType = new ForLoadedType(type.getRawType());
    return new Compound(
        TypeCreation.of(loadedType),
        Duplication.SINGLE,
        // Load the parameter and cast it to a byte[].
        readValue,
        TypeCasting.to(byteArrayType),
        // Create a new instance that wraps this byte[].
        MethodInvocation.invoke(
            loadedType
                .getDeclaredMethods()
                .filter(
                    ElementMatchers.isConstructor()
                        .and(ElementMatchers.takesArguments(byteArrayType)))
                .getOnly()));
  }
  return super.convertDefault(type);
}
 
Example 8
Source File: ByteBuddyUtils.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
protected StackManipulation convertCharSequence(TypeDescriptor<?> type) {
  // If the member is a String, then return it.
  if (type.isSubtypeOf(TypeDescriptor.of(String.class))) {
    return readValue;
  }

  // Otherwise, generate the following code:
  // return value.toString();
  StackManipulation stackManipulation =
      new Compound(
          readValue,
          MethodInvocation.invoke(
              CHAR_SEQUENCE_TYPE
                  .getDeclaredMethods()
                  .filter(
                      ElementMatchers.named("toString").and(ElementMatchers.takesArguments(0)))
                  .getOnly()));
  return new ShortCircuitReturnNull(readValue, stackManipulation);
}
 
Example 9
Source File: AvroUtils.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
protected StackManipulation convertDefault(TypeDescriptor<?> type) {
  if (type.isSubtypeOf(TypeDescriptor.of(GenericFixed.class))) {
    // Generate the following code:
    // return value.bytes();
    return new Compound(
        readValue,
        MethodInvocation.invoke(
            new ForLoadedType(GenericFixed.class)
                .getDeclaredMethods()
                .filter(
                    ElementMatchers.named("bytes")
                        .and(ElementMatchers.returns(new ForLoadedType(byte[].class))))
                .getOnly()));
  }
  return super.convertDefault(type);
}
 
Example 10
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 11
Source File: SamzaDoFnRunners.java    From beam with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static <InT, FnOutT> StatefulDoFnRunner.StateCleaner<?> createStateCleaner(
    DoFn<InT, FnOutT> doFn,
    WindowingStrategy<?, ?> windowingStrategy,
    StateInternals stateInternals) {
  final TypeDescriptor windowType = windowingStrategy.getWindowFn().getWindowTypeDescriptor();
  if (windowType.isSubtypeOf(TypeDescriptor.of(BoundedWindow.class))) {
    final Coder<? extends BoundedWindow> windowCoder =
        windowingStrategy.getWindowFn().windowCoder();
    return new StatefulDoFnRunner.StateInternalsStateCleaner<>(doFn, stateInternals, windowCoder);
  } else {
    return null;
  }
}
 
Example 12
Source File: ProtoByteBuddyUtils.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
public Type convert(TypeDescriptor typeDescriptor) {
  if (typeDescriptor.equals(BYTE_STRING_TYPE_DESCRIPTOR)
      || typeDescriptor.isSubtypeOf(BYTE_STRING_TYPE_DESCRIPTOR)) {
    return byte[].class;
  } else if (typeDescriptor.isSubtypeOf(TypeDescriptor.of(ProtocolMessageEnum.class))) {
    return Integer.class;
  } else if (typeDescriptor.equals(PROTO_TIMESTAMP_TYPE_DESCRIPTOR)
      || typeDescriptor.equals(PROTO_DURATION_TYPE_DESCRIPTOR)) {
    return Row.class;
  } else {
    Type type = TYPE_OVERRIDES.get(typeDescriptor);
    return (type != null) ? type : super.convert(typeDescriptor);
  }
}
 
Example 13
Source File: AvroUtils.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
protected java.lang.reflect.Type convertDefault(TypeDescriptor<?> type) {
  if (type.isSubtypeOf(TypeDescriptor.of(GenericFixed.class))) {
    return byte[].class;
  } else {
    return super.convertDefault(type);
  }
}
 
Example 14
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 15
Source File: ByteBuddyUtils.java    From beam with Apache License 2.0 5 votes vote down vote up
public T convert(TypeDescriptor typeDescriptor) {
  if (typeDescriptor.isArray()
      && !typeDescriptor.getComponentType().getRawType().equals(byte.class)) {
    // Byte arrays are special, so leave those alone.
    return convertArray(typeDescriptor);
  } else if (typeDescriptor.isSubtypeOf(TypeDescriptor.of(Map.class))) {
    return convertMap(typeDescriptor);
  } else if (typeDescriptor.isSubtypeOf(TypeDescriptor.of(ReadableInstant.class))) {
    return convertDateTime(typeDescriptor);
  } else if (typeDescriptor.isSubtypeOf(TypeDescriptor.of(ReadablePartial.class))) {
    return convertDateTime(typeDescriptor);
  } else if (typeDescriptor.isSubtypeOf(TypeDescriptor.of(ByteBuffer.class))) {
    return convertByteBuffer(typeDescriptor);
  } else if (typeDescriptor.isSubtypeOf(TypeDescriptor.of(CharSequence.class))) {
    return convertCharSequence(typeDescriptor);
  } else if (typeDescriptor.getRawType().isPrimitive()) {
    return convertPrimitive(typeDescriptor);
  } else if (typeDescriptor.getRawType().isEnum()) {
    return convertEnum(typeDescriptor);
  } else if (typeDescriptor.isSubtypeOf(TypeDescriptor.of(Iterable.class))) {
    if (typeDescriptor.isSubtypeOf(TypeDescriptor.of(List.class))) {
      return convertList(typeDescriptor);
    } else if (typeDescriptor.isSubtypeOf(TypeDescriptor.of(Collection.class))) {
      return convertCollection(typeDescriptor);
    } else {
      return convertIterable(typeDescriptor);
    }
  } else {
    return convertDefault(typeDescriptor);
  }
}
 
Example 16
Source File: AvroCoder.java    From beam with Apache License 2.0 5 votes vote down vote up
/** Return true if the given type token is a subtype of *any* of the listed parents. */
private static boolean isSubtypeOf(TypeDescriptor<?> type, Class<?>... parents) {
  for (Class<?> parent : parents) {
    if (type.isSubtypeOf(TypeDescriptor.of(parent))) {
      return true;
    }
  }
  return false;
}
 
Example 17
Source File: ByteBuddyUtils.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
protected StackManipulation convertDateTime(TypeDescriptor<?> type) {
  // The setter might be called with a different subclass of ReadableInstant than the one stored
  // in this POJO. We must extract the value passed into the setter and copy it into an instance
  // that the POJO can accept.

  // Generate the following code:
  //   return new T(value.getMillis());
  // Unless T is a sub-class of BaseLocal. Then generate:
  //   return new T(value.getMillis(), DateTimeZone.UTC);

  ForLoadedType loadedType = new ForLoadedType(type.getRawType());
  List<StackManipulation> stackManipulations = new ArrayList<>();

  // Create a new instance of the target type.
  stackManipulations.add(TypeCreation.of(loadedType));
  stackManipulations.add(Duplication.SINGLE);
  // Load the parameter and cast it to a ReadableInstant.
  stackManipulations.add(readValue);
  stackManipulations.add(TypeCasting.to(READABLE_INSTANT_TYPE));
  // Call ReadableInstant.getMillis to extract the millis since the epoch.
  stackManipulations.add(
      MethodInvocation.invoke(
          READABLE_INSTANT_TYPE
              .getDeclaredMethods()
              .filter(ElementMatchers.named("getMillis"))
              .getOnly()));

  if (type.isSubtypeOf(TypeDescriptor.of(BaseLocal.class))) {
    // Access DateTimeZone.UTC
    stackManipulations.add(
        FieldAccess.forField(
                DATE_TIME_ZONE_TYPE
                    .getDeclaredFields()
                    .filter(ElementMatchers.named("UTC"))
                    .getOnly())
            .read());
    // All subclasses of BaseLocal contain a ()(long, DateTimeZone) constructor
    // that takes in a millis and time zone argument. Call that constructor of the field to
    // initialize it.
    stackManipulations.add(
        MethodInvocation.invoke(
            loadedType
                .getDeclaredMethods()
                .filter(
                    ElementMatchers.isConstructor()
                        .and(
                            ElementMatchers.takesArguments(
                                ForLoadedType.of(long.class), DATE_TIME_ZONE_TYPE)))
                .getOnly()));
  } else {
    // All subclasses of ReadableInstant and ReadablePartial contain a ()(long) constructor
    // that takes in a millis argument. Call that constructor of the field to initialize it.
    stackManipulations.add(
        MethodInvocation.invoke(
            loadedType
                .getDeclaredMethods()
                .filter(
                    ElementMatchers.isConstructor()
                        .and(ElementMatchers.takesArguments(ForLoadedType.of(long.class))))
                .getOnly()));
  }

  StackManipulation stackManipulation = new Compound(stackManipulations);
  return new ShortCircuitReturnNull(readValue, stackManipulation);
}
 
Example 18
Source File: ProtoByteBuddyUtils.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public StackManipulation convert(TypeDescriptor type) {
  if (type.isSubtypeOf(TypeDescriptor.of(ByteString.class))) {
    return new Compound(
        readValue,
        TypeCasting.to(BYTE_ARRAY_TYPE),
        MethodInvocation.invoke(
            BYTE_STRING_TYPE
                .getDeclaredMethods()
                .filter(
                    ElementMatchers.named("copyFrom")
                        .and(ElementMatchers.takesArguments(BYTE_ARRAY_TYPE)))
                .getOnly()));
  } else if (type.isSubtypeOf(TypeDescriptor.of(ProtocolMessageEnum.class))) {
    ForLoadedType loadedType = new ForLoadedType(type.getRawType());
    // Convert the stored number back to the enum constant.
    return new Compound(
        readValue,
        Assigner.DEFAULT.assign(
            INTEGER_TYPE.asBoxed().asGenericType(),
            INTEGER_TYPE.asUnboxed().asGenericType(),
            Typing.STATIC),
        MethodInvocation.invoke(
            loadedType
                .getDeclaredMethods()
                .filter(
                    ElementMatchers.named("forNumber")
                        .and(ElementMatchers.isStatic().and(ElementMatchers.takesArguments(1))))
                .getOnly()));
  } else if (type.equals(PROTO_TIMESTAMP_TYPE_DESCRIPTOR)) {
    return new Compound(
        readValue,
        MethodInvocation.invoke(
            new ForLoadedType(ProtoSchemaLogicalTypes.TimestampConvert.class)
                .getDeclaredMethods()
                .filter(ElementMatchers.named("toTimestamp"))
                .getOnly()));
  } else if (type.equals(PROTO_DURATION_TYPE_DESCRIPTOR)) {
    return new Compound(
        readValue,
        MethodInvocation.invoke(
            new ForLoadedType(ProtoSchemaLogicalTypes.DurationConvert.class)
                .getDeclaredMethods()
                .filter(ElementMatchers.named("toDuration"))
                .getOnly()));
  } else {
    ForLoadedType wrapperType = WRAPPER_LOADED_TYPES.get(type);
    if (wrapperType != null) {
      if (type.equals(PROTO_BYTES_VALUE_TYPE_DESCRIPTOR)) {
        readValue =
            getFactory()
                .createSetterConversions(readValue)
                .convert(TypeDescriptor.of(ByteString.class));
      }
      MethodDescription.InDefinedShape ofMethod =
          wrapperType.getDeclaredMethods().filter(ElementMatchers.named("of")).getOnly();
      TypeDescription.Generic argumentType = ofMethod.getParameters().get(0).getType();
      return new Compound(
          readValue,
          Assigner.DEFAULT.assign(
              argumentType.asErasure().asBoxed().asGenericType(), argumentType, Typing.STATIC),
          MethodInvocation.invoke(ofMethod));
    } else {
      return super.convert(type);
    }
  }
}
 
Example 19
Source File: ProtoByteBuddyUtils.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
public StackManipulation convert(TypeDescriptor type) {
  if (type.equals(BYTE_STRING_TYPE_DESCRIPTOR)
      || type.isSubtypeOf(BYTE_STRING_TYPE_DESCRIPTOR)) {
    return new Compound(
        readValue,
        MethodInvocation.invoke(
            BYTE_STRING_TYPE
                .getDeclaredMethods()
                .filter(ElementMatchers.named("toByteArray"))
                .getOnly()));
  } else if (type.isSubtypeOf(TypeDescriptor.of(ProtocolMessageEnum.class))) {
    return new Compound(
        readValue,
        MethodInvocation.invoke(
            PROTO_ENUM_TYPE
                .getDeclaredMethods()
                .filter(
                    ElementMatchers.named("getNumber").and(ElementMatchers.takesArguments(0)))
                .getOnly()),
        Assigner.DEFAULT.assign(
            INTEGER_TYPE.asUnboxed().asGenericType(),
            INTEGER_TYPE.asGenericType(),
            Typing.STATIC));
  } else if (type.equals(PROTO_TIMESTAMP_TYPE_DESCRIPTOR)) {
    return new Compound(
        readValue,
        MethodInvocation.invoke(
            new ForLoadedType(ProtoSchemaLogicalTypes.TimestampConvert.class)
                .getDeclaredMethods()
                .filter(ElementMatchers.named("toRow"))
                .getOnly()));
  } else if (type.equals(PROTO_DURATION_TYPE_DESCRIPTOR)) {
    return new Compound(
        readValue,
        MethodInvocation.invoke(
            new ForLoadedType(ProtoSchemaLogicalTypes.DurationConvert.class)
                .getDeclaredMethods()
                .filter(ElementMatchers.named("toRow"))
                .getOnly()));
  } else {
    ForLoadedType wrapperType = WRAPPER_LOADED_TYPES.get(type);
    if (wrapperType != null) {
      MethodDescription.InDefinedShape getValueMethod =
          wrapperType.getDeclaredMethods().filter(ElementMatchers.named("getValue")).getOnly();
      TypeDescription.Generic returnType = getValueMethod.getReturnType();
      StackManipulation stackManipulation =
          new Compound(
              readValue,
              MethodInvocation.invoke(getValueMethod),
              Assigner.DEFAULT.assign(
                  returnType, returnType.asErasure().asBoxed().asGenericType(), Typing.STATIC));
      if (type.equals(PROTO_BYTES_VALUE_TYPE_DESCRIPTOR)) {
        stackManipulation =
            getFactory()
                .createGetterConversions(stackManipulation)
                .convert(BYTE_STRING_TYPE_DESCRIPTOR);
      }
      return stackManipulation;
    }
    return super.convert(type);
  }
}