/** 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; }
static void verifyStateSupported(DoFn<?, ?> fn) { DoFnSignature signature = DoFnSignatures.getSignature(fn.getClass()); for (DoFnSignature.StateDeclaration stateDecl : signature.stateDeclarations().values()) { // https://issues.apache.org/jira/browse/BEAM-1474 if (stateDecl.stateType().isSubtypeOf(TypeDescriptor.of(MapState.class))) { throw new UnsupportedOperationException( String.format( "%s does not currently support %s", DataflowRunner.class.getSimpleName(), MapState.class.getSimpleName())); } // https://issues.apache.org/jira/browse/BEAM-1479 if (stateDecl.stateType().isSubtypeOf(TypeDescriptor.of(SetState.class))) { throw new UnsupportedOperationException( String.format( "%s does not currently support %s", DataflowRunner.class.getSimpleName(), SetState.class.getSimpleName())); } } }
@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); } } }
@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); } } }
@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); } } }
@VisibleForTesting static DoFnSignature.GetRestrictionCoderMethod analyzeGetRestrictionCoderMethod( ErrorReporter errors, TypeDescriptor<? extends DoFn> fnT, Method m) { errors.checkArgument(m.getParameterTypes().length == 0, "Must have zero arguments"); TypeDescriptor<?> resT = fnT.resolveType(m.getGenericReturnType()); errors.checkArgument( resT.isSubtypeOf(TypeDescriptor.of(Coder.class)), "Must return a Coder, but returns %s", formatType(resT)); return DoFnSignature.GetRestrictionCoderMethod.create(m, resT); }
/** Get a {@link FieldType} from a {@link TypeDescriptor}. */ public static FieldType fieldTypeForJavaType(TypeDescriptor typeDescriptor) { 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(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); } FieldType fieldType = FieldType.of(typeName); return fieldType; } }
@SuppressWarnings("unchecked") private static Implementation getMapType(TypeDescriptor valueType, int index) { 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(); return FixedValue.reference(params[index]); } else { throw new RuntimeException("Map type is not parameterized! " + map); } } return FixedValue.nullValue(); } }
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(Collection.class))) { return convertCollection(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(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 { return convertDefault(typeDescriptor); } }
@VisibleForTesting static DoFnSignature.NewTrackerMethod analyzeNewTrackerMethod( ErrorReporter errors, TypeDescriptor<? extends DoFn> fnT, Method m) { // Method is of the form: // @NewTracker // TrackerT newTracker(RestrictionT restriction); Type[] params = m.getGenericParameterTypes(); errors.checkArgument(params.length == 1, "Must have a single argument"); TypeDescriptor<?> restrictionT = fnT.resolveType(params[0]); TypeDescriptor<?> trackerT = fnT.resolveType(m.getGenericReturnType()); TypeDescriptor<?> expectedTrackerT = restrictionTrackerTypeOf(restrictionT); errors.checkArgument( trackerT.isSubtypeOf(expectedTrackerT), "Returns %s, but must return a subtype of %s", formatType(trackerT), formatType(expectedTrackerT)); return DoFnSignature.NewTrackerMethod.create(m, restrictionT, trackerT); }
if (!(TypeDescriptor.of(stateSpecRawType).isSubtypeOf(TypeDescriptor.of(StateSpec.class)))) { errors.throwIllegalArgument( "%s annotation on non-%s field [%s] that has class %s",
@SuppressWarnings("unchecked") static Implementation getArrayComponentType(TypeDescriptor valueType) { if (valueType.isArray()) { Type component = valueType.getComponentType().getType(); if (!component.equals(byte.class)) { return FixedValue.reference(component); } } else if (valueType.isSubtypeOf(TypeDescriptor.of(Collection.class))) { TypeDescriptor<Collection<?>> collection = valueType.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 FixedValue.reference(params[0]); } else { throw new RuntimeException("Collection parameter is not parameterized!"); } } return FixedValue.nullValue(); }
} else if (type.isSubtypeOf(TypeDescriptor.of(Collection.class))) { TypeDescriptor<Collection<?>> collection = type.getSupertype(Collection.class); if (collection.getType() instanceof ParameterizedType) { throw new RuntimeException("Cannot infer schema from unparameterized collection."); } else if (type.isSubtypeOf(TypeDescriptor.of(Map.class))) { TypeDescriptor<Collection<?>> map = type.getSupertype(Map.class); if (map.getType() instanceof ParameterizedType) { throw new RuntimeException("Cannot infer schema from unparameterized map."); } else if (type.isSubtypeOf(TypeDescriptor.of(CharSequence.class))) { return FieldType.STRING; } else if (type.isSubtypeOf(TypeDescriptor.of(ReadableInstant.class))) { return FieldType.DATETIME; } else if (type.isSubtypeOf(TypeDescriptor.of(ByteBuffer.class))) { return FieldType.BYTES; } else {
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."); }
@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(); return new Compound( readValue, MethodInvocation.invoke( CHAR_SEQUENCE_TYPE .getDeclaredMethods() .filter(ElementMatchers.named("toString")) .getOnly())); }
stateDecl.stateType().isSubtypeOf(stateType), "data type of reference to %s %s must be a supertype of %s", StateId.class.getSimpleName(),
&& getInitialRestriction .restrictionT() .isSubtypeOf(TypeDescriptor.of(HasDefaultTracker.class))) { getRestrictionCoder.coderT().isSubtypeOf(coderTypeOf(restrictionT)), "Uses restriction type %s, but @%s method %s returns %s " + "which is not a subtype of %s",