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

The following examples show how to use org.apache.beam.sdk.values.TypeDescriptor#getRawType() . 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: AvroCoder.java    From beam with Apache License 2.0 6 votes vote down vote up
private void checkRecord(TypeDescriptor<?> type, Schema schema) {
  // For a record, we want to make sure that all the fields are deterministic.
  Class<?> clazz = type.getRawType();
  for (Schema.Field fieldSchema : schema.getFields()) {
    Field field = getField(clazz, fieldSchema.name());
    String fieldContext = field.getDeclaringClass().getName() + "#" + field.getName();

    if (field.isAnnotationPresent(AvroEncode.class)) {
      reportError(
          fieldContext, "Custom encoders may be non-deterministic -- remove @AvroEncode");
      continue;
    }

    if (!IndexedRecord.class.isAssignableFrom(field.getType())
        && field.isAnnotationPresent(AvroSchema.class)) {
      // TODO: We should be able to support custom schemas on POJO fields, but we shouldn't
      // need to, so we just allow it in the case of IndexedRecords.
      reportError(
          fieldContext, "Custom schemas are only supported for subtypes of IndexedRecord.");
      continue;
    }

    TypeDescriptor<?> fieldType = type.resolveType(field.getGenericType());
    recurse(fieldContext, fieldType, fieldSchema.schema());
  }
}
 
Example 2
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 3
Source File: AvroUtils.java    From beam with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an {@code SchemaCoder} instance for the provided element type.
 *
 * @param <T> the element type
 */
public static <T> SchemaCoder<T> schemaCoder(TypeDescriptor<T> type) {
  @SuppressWarnings("unchecked")
  Class<T> clazz = (Class<T>) type.getRawType();
  org.apache.avro.Schema avroSchema = new ReflectData(clazz.getClassLoader()).getSchema(clazz);
  Schema beamSchema = toBeamSchema(avroSchema);
  return SchemaCoder.of(
      beamSchema, type, getToRowFunction(clazz, avroSchema), getFromRowFunction(clazz));
}
 
Example 4
Source File: ByteBuddyUtils.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
protected StackManipulation convertPrimitive(TypeDescriptor<?> type) {
  ForLoadedType valueType = new ForLoadedType(type.getRawType());
  // Unbox the type.
  return new StackManipulation.Compound(
      readValue,
      Assigner.DEFAULT.assign(
          valueType.asBoxed().asGenericType(),
          valueType.asUnboxed().asGenericType(),
          Typing.STATIC));
}
 
Example 5
Source File: ByteBuddyUtils.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
protected StackManipulation convertCharSequence(TypeDescriptor<?> type) {
  // If the type is a String, just return it.
  if (type.getRawType().isAssignableFrom(String.class)) {
    return readValue;
  }

  // Otherwise, generate the following code:
  // return new T((CharacterSequence) value).

  ForLoadedType loadedType = new ForLoadedType(type.getRawType());
  StackManipulation stackManipulation =
      new StackManipulation.Compound(
          TypeCreation.of(loadedType),
          Duplication.SINGLE,
          // Load the parameter and cast it to a CharSequence.
          readValue,
          TypeCasting.to(CHAR_SEQUENCE_TYPE),
          // Create an element of the field type that wraps this one.
          MethodInvocation.invoke(
              loadedType
                  .getDeclaredMethods()
                  .filter(
                      ElementMatchers.isConstructor()
                          .and(ElementMatchers.takesArguments(CHAR_SEQUENCE_TYPE)))
                  .getOnly()));
  return new ShortCircuitReturnNull(readValue, stackManipulation);
}
 
Example 6
Source File: ByteBuddyUtils.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
protected StackManipulation convertPrimitive(TypeDescriptor<?> type) {
  ForLoadedType loadedType = new ForLoadedType(type.getRawType());
  // Box the primitive type.
  return new Compound(
      readValue,
      Assigner.DEFAULT.assign(
          loadedType.asGenericType(), loadedType.asBoxed().asGenericType(), Typing.STATIC));
}
 
Example 7
Source File: ByteBuddyUtils.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
protected Type convertList(TypeDescriptor<?> type) {
  TypeDescriptor ret = createCollectionType(ReflectUtils.getIterableComponentType(type));
  return returnRawTypes ? ret.getRawType() : ret.getType();
}
 
Example 8
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 9
Source File: ByteBuddyUtils.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
protected StackManipulation convertArray(TypeDescriptor<?> type) {
  // Generate the following code:
  // T[] toArray = (T[]) value.toArray(new T[0]);
  // return isPrimitive ? toArray : ArrayUtils.toPrimitive(toArray);

  ForLoadedType loadedType = new ForLoadedType(type.getRawType());
  // The type of the array containing the (possibly) boxed values.
  TypeDescription arrayType =
      TypeDescription.Generic.Builder.rawType(loadedType.getComponentType().asBoxed())
          .asArray()
          .build()
          .asErasure();

  Type rowElementType =
      getFactory().createTypeConversion(false).convert(type.getComponentType());
  final TypeDescriptor arrayElementType = ReflectUtils.boxIfPrimitive(type.getComponentType());
  StackManipulation readTransformedValue = readValue;
  if (!arrayElementType.hasUnresolvedParameters()) {
    ForLoadedType conversionFunction =
        new ForLoadedType(
            createCollectionTransformFunction(
                TypeDescriptor.of(rowElementType).getRawType(),
                Primitives.wrap(arrayElementType.getRawType()),
                (s) -> getFactory().createSetterConversions(s).convert(arrayElementType)));
    readTransformedValue = createTransformingContainer(conversionFunction, readValue);
  }

  // Extract an array from the collection.
  StackManipulation stackManipulation =
      new Compound(
          readTransformedValue,
          TypeCasting.to(COLLECTION_TYPE),
          // Call Collection.toArray(T[[]) to extract the array. Push new T[0] on the stack
          // before
          // calling toArray.
          ArrayFactory.forType(loadedType.getComponentType().asBoxed().asGenericType())
              .withValues(Collections.emptyList()),
          MethodInvocation.invoke(
              COLLECTION_TYPE
                  .getDeclaredMethods()
                  .filter(
                      ElementMatchers.named("toArray")
                          .and(
                              ElementMatchers.takesArguments(
                                  TypeDescription.Generic.Builder.rawType(Object.class)
                                      .asArray()
                                      .build()
                                      .asErasure())))
                  .getOnly()),
          // Cast the result to T[].
          TypeCasting.to(arrayType));

  if (loadedType.getComponentType().isPrimitive()) {
    // The array we extract will be an array of objects. If the pojo field is an array of
    // primitive types, we need to then convert to an array of unboxed objects.
    stackManipulation =
        new StackManipulation.Compound(
            stackManipulation,
            MethodInvocation.invoke(
                ARRAY_UTILS_TYPE
                    .getDeclaredMethods()
                    .filter(
                        ElementMatchers.named("toPrimitive")
                            .and(ElementMatchers.takesArguments(arrayType)))
                    .getOnly()));
  }
  return new ShortCircuitReturnNull(readValue, stackManipulation);
}
 
Example 10
Source File: ByteBuddyUtils.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
protected StackManipulation convertArray(TypeDescriptor<?> type) {
  // Generate the following code:
  // return isComponentTypePrimitive ? Arrays.asList(ArrayUtils.toObject(value))
  //     : Arrays.asList(value);

  TypeDescriptor<?> componentType = type.getComponentType();
  ForLoadedType loadedArrayType = new ForLoadedType(type.getRawType());
  StackManipulation readArrayValue = readValue;
  // Row always expects to get an Iterable back for array types. Wrap this array into a
  // List using Arrays.asList before returning.
  if (loadedArrayType.getComponentType().isPrimitive()) {
    // Arrays.asList doesn't take primitive arrays, so convert first using ArrayUtils.toObject.
    readArrayValue =
        new Compound(
            readArrayValue,
            MethodInvocation.invoke(
                ARRAY_UTILS_TYPE
                    .getDeclaredMethods()
                    .filter(
                        ElementMatchers.isStatic()
                            .and(ElementMatchers.named("toObject"))
                            .and(ElementMatchers.takesArguments(loadedArrayType)))
                    .getOnly()));

    componentType = TypeDescriptor.of(Primitives.wrap(componentType.getRawType()));
  }
  // Now convert to a List object.
  StackManipulation readListValue =
      new Compound(
          readArrayValue,
          MethodInvocation.invoke(
              ARRAYS_TYPE
                  .getDeclaredMethods()
                  .filter(ElementMatchers.isStatic().and(ElementMatchers.named("asList")))
                  .getOnly()));

  // Generate a SerializableFunction to convert the element-type objects.
  StackManipulation stackManipulation;
  final TypeDescriptor finalComponentType = ReflectUtils.boxIfPrimitive(componentType);
  if (!finalComponentType.hasUnresolvedParameters()) {
    Type convertedComponentType =
        getFactory().createTypeConversion(true).convert(componentType);
    ForLoadedType functionType =
        new ForLoadedType(
            createCollectionTransformFunction(
                componentType.getRawType(),
                convertedComponentType,
                (s) -> getFactory().createGetterConversions(s).convert(finalComponentType)));
    stackManipulation = createTransformingContainer(functionType, readListValue);
  } else {
    stackManipulation = readListValue;
  }
  return new ShortCircuitReturnNull(readValue, stackManipulation);
}
 
Example 11
Source File: ByteBuddyUtils.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
protected Type convertDefault(TypeDescriptor<?> type) {
  return returnRawTypes ? type.getRawType() : type.getType();
}
 
Example 12
Source File: ByteBuddyUtils.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
protected Type convertIterable(TypeDescriptor<?> type) {
  TypeDescriptor ret = createIterableType(ReflectUtils.getIterableComponentType(type));
  return returnRawTypes ? ret.getRawType() : ret.getType();
}
 
Example 13
Source File: ByteBuddyUtils.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
protected Type convertCollection(TypeDescriptor<?> type) {
  TypeDescriptor ret = createCollectionType(ReflectUtils.getIterableComponentType(type));
  return returnRawTypes ? ret.getRawType() : ret.getType();
}
 
Example 14
Source File: ByteBuddyUtils.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
protected Type convertArray(TypeDescriptor<?> type) {
  TypeDescriptor ret = createCollectionType(type.getComponentType());
  return returnRawTypes ? ret.getRawType() : ret.getType();
}
 
Example 15
Source File: CoderRegistry.java    From beam with Apache License 2.0 4 votes vote down vote up
/**
 * Returns {@code true} if the given {@link Coder} can possibly encode elements of the given type.
 */
@VisibleForTesting
static <T, CoderT extends Coder<T>, CandidateT> void verifyCompatible(
    CoderT coder, Type candidateType) throws IncompatibleCoderException {

  // Various representations of the coder's class
  @SuppressWarnings("unchecked")
  Class<CoderT> coderClass = (Class<CoderT>) coder.getClass();
  TypeDescriptor<CoderT> coderDescriptor = TypeDescriptor.of(coderClass);

  // Various representations of the actual coded type
  @SuppressWarnings("unchecked")
  TypeDescriptor<T> codedDescriptor = CoderUtils.getCodedType(coderDescriptor);
  @SuppressWarnings("unchecked")
  Class<T> codedClass = (Class<T>) codedDescriptor.getRawType();
  Type codedType = codedDescriptor.getType();

  // Various representations of the candidate type
  @SuppressWarnings("unchecked")
  TypeDescriptor<CandidateT> candidateDescriptor =
      (TypeDescriptor<CandidateT>) TypeDescriptor.of(candidateType);
  @SuppressWarnings("unchecked")
  Class<CandidateT> candidateClass = (Class<CandidateT>) candidateDescriptor.getRawType();

  // If coder has type Coder<T> where the actual value of T is lost
  // to erasure, then we cannot rule it out.
  if (candidateType instanceof TypeVariable) {
    return;
  }

  // If the raw types are not compatible, we can certainly rule out
  // coder compatibility
  if (!codedClass.isAssignableFrom(candidateClass)) {
    throw new IncompatibleCoderException(
        String.format(
            "Cannot encode elements of type %s with coder %s because the"
                + " coded type %s is not assignable from %s",
            candidateType, coder, codedClass, candidateType),
        coder,
        candidateType);
  }
  // we have established that this is a covariant upcast... though
  // coders are invariant, we are just checking one direction
  @SuppressWarnings("unchecked")
  TypeDescriptor<T> candidateOkDescriptor = (TypeDescriptor<T>) candidateDescriptor;

  // If the coded type is a parameterized type where any of the actual
  // type parameters are not compatible, then the whole thing is certainly not
  // compatible.
  if ((codedType instanceof ParameterizedType) && !isNullOrEmpty(coder.getCoderArguments())) {
    ParameterizedType parameterizedSupertype =
        (ParameterizedType) candidateOkDescriptor.getSupertype(codedClass).getType();
    Type[] typeArguments = parameterizedSupertype.getActualTypeArguments();
    List<? extends Coder<?>> typeArgumentCoders = coder.getCoderArguments();
    if (typeArguments.length < typeArgumentCoders.size()) {
      throw new IncompatibleCoderException(
          String.format(
              "Cannot encode elements of type %s with coder %s:"
                  + " the generic supertype %s has %s type parameters, which is less than the"
                  + " number of coder arguments %s has (%s).",
              candidateOkDescriptor,
              coder,
              parameterizedSupertype,
              typeArguments.length,
              coder,
              typeArgumentCoders.size()),
          coder,
          candidateOkDescriptor.getType());
    }
    for (int i = 0; i < typeArgumentCoders.size(); i++) {
      try {
        Coder<?> typeArgumentCoder = typeArgumentCoders.get(i);
        verifyCompatible(
            typeArgumentCoder, candidateDescriptor.resolveType(typeArguments[i]).getType());
      } catch (IncompatibleCoderException exn) {
        throw new IncompatibleCoderException(
            String.format(
                "Cannot encode elements of type %s with coder %s"
                    + " because some component coder is incompatible",
                candidateType, coder),
            coder,
            candidateType,
            exn);
      }
    }
  }
}
 
Example 16
Source File: SerializableCoder.java    From beam with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a {@link SerializableCoder} instance for the provided element type.
 *
 * @param <T> the element type
 */
public static <T extends Serializable> SerializableCoder<T> of(TypeDescriptor<T> type) {
  @SuppressWarnings("unchecked")
  Class<T> clazz = (Class<T>) type.getRawType();
  return new SerializableCoder<>(clazz, type);
}
 
Example 17
Source File: AvroCoder.java    From beam with Apache License 2.0 4 votes vote down vote up
/**
 * Returns an {@code AvroCoder} instance for the provided element type.
 *
 * @param <T> the element type
 */
public static <T> AvroCoder<T> of(TypeDescriptor<T> type) {
  @SuppressWarnings("unchecked")
  Class<T> clazz = (Class<T>) type.getRawType();
  return of(clazz);
}
 
Example 18
Source File: ProtoCoder.java    From beam with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a {@link ProtoCoder} for the Protocol Buffers {@link Message} indicated by the given
 * {@link TypeDescriptor}.
 */
public static <T extends Message> ProtoCoder<T> of(TypeDescriptor<T> protoMessageType) {
  @SuppressWarnings("unchecked")
  Class<T> protoMessageClass = (Class<T>) protoMessageType.getRawType();
  return of(protoMessageClass);
}
 
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.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 20
Source File: InstanceBuilder.java    From beam with Apache License 2.0 2 votes vote down vote up
/**
 * Create an InstanceBuilder for the given type.
 *
 * <p>The specified type is the type returned by {@link #build}, which is typically the common
 * base type or interface for the instance to be constructed.
 *
 * <p>The TypeDescriptor argument allows specification of generic types. For example, a {@code
 * List<String>} return type can be specified as {@code ofType(new
 * TypeDescriptor<List<String>>(){})}.
 */
public static <T> InstanceBuilder<T> ofType(TypeDescriptor<T> token) {
  @SuppressWarnings("unchecked")
  Class<T> type = (Class<T>) token.getRawType();
  return new InstanceBuilder<>(type);
}