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

The following examples show how to use org.apache.beam.sdk.values.TypeDescriptor#hasUnresolvedParameters() . 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: ByteBuddyUtils.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
protected StackManipulation convertIterable(TypeDescriptor<?> type) {
  TypeDescriptor componentType = ReflectUtils.getIterableComponentType(type);
  Type convertedComponentType = getFactory().createTypeConversion(true).convert(componentType);

  final TypeDescriptor finalComponentType = ReflectUtils.boxIfPrimitive(componentType);
  if (!finalComponentType.hasUnresolvedParameters()) {
    ForLoadedType functionType =
        new ForLoadedType(
            createCollectionTransformFunction(
                componentType.getRawType(),
                convertedComponentType,
                (s) -> getFactory().createGetterConversions(s).convert(finalComponentType)));
    StackManipulation stackManipulation = createTransformingContainer(functionType, readValue);
    return new ShortCircuitReturnNull(readValue, stackManipulation);
  } else {
    return readValue;
  }
}
 
Example 2
Source File: ByteBuddyUtils.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
protected StackManipulation convertCollection(TypeDescriptor<?> type) {
  TypeDescriptor componentType = ReflectUtils.getIterableComponentType(type);
  Type convertedComponentType = getFactory().createTypeConversion(true).convert(componentType);
  final TypeDescriptor finalComponentType = ReflectUtils.boxIfPrimitive(componentType);
  if (!finalComponentType.hasUnresolvedParameters()) {
    ForLoadedType functionType =
        new ForLoadedType(
            createCollectionTransformFunction(
                componentType.getRawType(),
                convertedComponentType,
                (s) -> getFactory().createGetterConversions(s).convert(finalComponentType)));
    StackManipulation stackManipulation = createTransformingContainer(functionType, readValue);
    return new ShortCircuitReturnNull(readValue, stackManipulation);
  } else {
    return readValue;
  }
}
 
Example 3
Source File: ByteBuddyUtils.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
protected StackManipulation convertList(TypeDescriptor<?> type) {
  TypeDescriptor componentType = ReflectUtils.getIterableComponentType(type);
  Type convertedComponentType = getFactory().createTypeConversion(true).convert(componentType);
  final TypeDescriptor finalComponentType = ReflectUtils.boxIfPrimitive(componentType);
  if (!finalComponentType.hasUnresolvedParameters()) {
    ForLoadedType functionType =
        new ForLoadedType(
            createCollectionTransformFunction(
                componentType.getRawType(),
                convertedComponentType,
                (s) -> getFactory().createGetterConversions(s).convert(finalComponentType)));
    StackManipulation stackManipulation = createTransformingContainer(functionType, readValue);
    return new ShortCircuitReturnNull(readValue, stackManipulation);
  } else {
    return readValue;
  }
}
 
Example 4
Source File: ByteBuddyUtils.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
protected StackManipulation convertIterable(TypeDescriptor<?> type) {
  Type rowElementType =
      getFactory()
          .createTypeConversion(false)
          .convert(ReflectUtils.getIterableComponentType(type));
  final TypeDescriptor iterableElementType = ReflectUtils.getIterableComponentType(type);
  if (!iterableElementType.hasUnresolvedParameters()) {
    ForLoadedType conversionFunction =
        new ForLoadedType(
            createCollectionTransformFunction(
                TypeDescriptor.of(rowElementType).getRawType(),
                iterableElementType.getRawType(),
                (s) -> getFactory().createSetterConversions(s).convert(iterableElementType)));
    StackManipulation transformedContainer =
        createTransformingContainer(conversionFunction, readValue);
    return new ShortCircuitReturnNull(readValue, transformedContainer);
  } else {
    return readValue;
  }
}
 
Example 5
Source File: ByteBuddyUtils.java    From beam with Apache License 2.0 6 votes vote down vote up
@Override
protected StackManipulation convertCollection(TypeDescriptor<?> type) {
  Type rowElementType =
      getFactory()
          .createTypeConversion(false)
          .convert(ReflectUtils.getIterableComponentType(type));
  final TypeDescriptor collectionElementType = ReflectUtils.getIterableComponentType(type);

  if (!collectionElementType.hasUnresolvedParameters()) {
    ForLoadedType conversionFunction =
        new ForLoadedType(
            createCollectionTransformFunction(
                TypeDescriptor.of(rowElementType).getRawType(),
                collectionElementType.getRawType(),
                (s) -> getFactory().createSetterConversions(s).convert(collectionElementType)));
    StackManipulation transformedContainer =
        createTransformingContainer(conversionFunction, readValue);
    return new ShortCircuitReturnNull(readValue, transformedContainer);
  } else {
    return readValue;
  }
}
 
Example 6
Source File: ByteBuddyUtils.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
protected StackManipulation convertList(TypeDescriptor<?> type) {
  Type rowElementType =
      getFactory()
          .createTypeConversion(false)
          .convert(ReflectUtils.getIterableComponentType(type));
  final TypeDescriptor collectionElementType = ReflectUtils.getIterableComponentType(type);

  StackManipulation readTrasformedValue = readValue;
  if (!collectionElementType.hasUnresolvedParameters()) {
    ForLoadedType conversionFunction =
        new ForLoadedType(
            createCollectionTransformFunction(
                TypeDescriptor.of(rowElementType).getRawType(),
                collectionElementType.getRawType(),
                (s) -> getFactory().createSetterConversions(s).convert(collectionElementType)));
    readTrasformedValue = createTransformingContainer(conversionFunction, readValue);
  }
  // TODO: Don't copy if already a list!
  StackManipulation transformedList =
      new Compound(
          readTrasformedValue,
          MethodInvocation.invoke(
              new ForLoadedType(Lists.class)
                  .getDeclaredMethods()
                  .filter(
                      ElementMatchers.named("newArrayList")
                          .and(ElementMatchers.takesArguments(Iterable.class)))
                  .getOnly()));
  return new ShortCircuitReturnNull(readValue, transformedList);
}
 
Example 7
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 8
Source File: ByteBuddyUtils.java    From beam with Apache License 2.0 4 votes vote down vote up
@Override
protected StackManipulation convertMap(TypeDescriptor<?> type) {
  final TypeDescriptor keyType = ReflectUtils.getMapType(type, 0);
  final TypeDescriptor valueType = ReflectUtils.getMapType(type, 1);

  Type convertedKeyType = getFactory().createTypeConversion(true).convert(keyType);
  Type convertedValueType = getFactory().createTypeConversion(true).convert(valueType);

  if (!keyType.hasUnresolvedParameters() && !valueType.hasUnresolvedParameters()) {
    ForLoadedType keyFunctionType =
        new ForLoadedType(
            createCollectionTransformFunction(
                keyType.getRawType(),
                convertedKeyType,
                (s) -> getFactory().createGetterConversions(s).convert(keyType)));
    ForLoadedType valueFunctionType =
        new ForLoadedType(
            createCollectionTransformFunction(
                valueType.getRawType(),
                convertedValueType,
                (s) -> getFactory().createGetterConversions(s).convert(valueType)));
    StackManipulation stackManipulation =
        new Compound(
            readValue,
            TypeCreation.of(keyFunctionType),
            Duplication.SINGLE,
            MethodInvocation.invoke(
                keyFunctionType
                    .getDeclaredMethods()
                    .filter(
                        ElementMatchers.isConstructor().and(ElementMatchers.takesArguments(0)))
                    .getOnly()),
            TypeCreation.of(valueFunctionType),
            Duplication.SINGLE,
            MethodInvocation.invoke(
                valueFunctionType
                    .getDeclaredMethods()
                    .filter(
                        ElementMatchers.isConstructor().and(ElementMatchers.takesArguments(0)))
                    .getOnly()),
            MethodInvocation.invoke(
                BYTE_BUDDY_UTILS_TYPE
                    .getDeclaredMethods()
                    .filter(ElementMatchers.named("getTransformingMap"))
                    .getOnly()));
    return new ShortCircuitReturnNull(readValue, stackManipulation);
  } else {
    return readValue;
  }
}
 
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 convertMap(TypeDescriptor<?> type) {
  Type rowKeyType =
      getFactory().createTypeConversion(false).convert(ReflectUtils.getMapType(type, 0));
  final TypeDescriptor keyElementType = ReflectUtils.getMapType(type, 0);
  Type rowValueType =
      getFactory().createTypeConversion(false).convert(ReflectUtils.getMapType(type, 1));
  final TypeDescriptor valueElementType = ReflectUtils.getMapType(type, 1);

  StackManipulation readTrasformedValue = readValue;
  if (!keyElementType.hasUnresolvedParameters()
      && !valueElementType.hasUnresolvedParameters()) {
    ForLoadedType keyConversionFunction =
        new ForLoadedType(
            createCollectionTransformFunction(
                TypeDescriptor.of(rowKeyType).getRawType(),
                keyElementType.getRawType(),
                (s) -> getFactory().createSetterConversions(s).convert(keyElementType)));
    ForLoadedType valueConversionFunction =
        new ForLoadedType(
            createCollectionTransformFunction(
                TypeDescriptor.of(rowValueType).getRawType(),
                valueElementType.getRawType(),
                (s) -> getFactory().createSetterConversions(s).convert(valueElementType)));
    readTrasformedValue =
        new Compound(
            readValue,
            TypeCreation.of(keyConversionFunction),
            Duplication.SINGLE,
            MethodInvocation.invoke(
                keyConversionFunction
                    .getDeclaredMethods()
                    .filter(
                        ElementMatchers.isConstructor().and(ElementMatchers.takesArguments(0)))
                    .getOnly()),
            TypeCreation.of(valueConversionFunction),
            Duplication.SINGLE,
            MethodInvocation.invoke(
                valueConversionFunction
                    .getDeclaredMethods()
                    .filter(
                        ElementMatchers.isConstructor().and(ElementMatchers.takesArguments(0)))
                    .getOnly()),
            MethodInvocation.invoke(
                BYTE_BUDDY_UTILS_TYPE
                    .getDeclaredMethods()
                    .filter(ElementMatchers.named("getTransformingMap"))
                    .getOnly()));
  }
  return new ShortCircuitReturnNull(readValue, readTrasformedValue);
}