@Nullable private static TypeDescriptor<? extends BoundedWindow> getWindowType( TypeDescriptor<?> fnClass, Method method) { Type[] params = method.getGenericParameterTypes(); for (Type param : params) { TypeDescriptor<?> paramT = fnClass.resolveType(param); if (BoundedWindow.class.isAssignableFrom(paramT.getRawType())) { return (TypeDescriptor<? extends BoundedWindow>) paramT; } } return null; }
private void checkString(String context, TypeDescriptor<?> type) { // For types that are encoded as strings, we need to make sure they're in an approved // whitelist. For other types that are annotated @Stringable, Avro will just use the // #toString() methods, which has no guarantees of determinism. if (!DETERMINISTIC_STRINGABLE_CLASSES.contains(type.getRawType())) { reportError(context, "%s may not have deterministic #toString()", type); } }
/** * 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); }
/** * 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); }
@Nullable private static TypeDescriptor<?> getTrackerType(TypeDescriptor<?> fnClass, Method method) { Type[] params = method.getGenericParameterTypes(); for (Type param : params) { TypeDescriptor<?> paramT = fnClass.resolveType(param); if (RestrictionTracker.class.isAssignableFrom(paramT.getRawType())) { return paramT; } } return null; }
/** * 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); }
@Override protected Type convertPrimitive(TypeDescriptor<?> type) { return ClassUtils.primitiveToWrapper(type.getRawType()); }
public List<String> check(TypeDescriptor<?> type, Schema schema) { recurse(type.getRawType().getName(), type, schema); return reasons; }
/** Validates translation function given for key/value translation. */ private void validateTranslationFunction( TypeDescriptor<?> inputType, SimpleFunction<?, ?> simpleFunction, String errorMsg) { if (simpleFunction != null && !simpleFunction.getInputTypeDescriptor().equals(inputType)) { throw new IllegalArgumentException( String.format(errorMsg, getinputFormatClass().getRawType(), inputType.getRawType())); } }
@Override public <T> Coder<T> coderFor( TypeDescriptor<T> typeDescriptor, List<? extends Coder<?>> componentCoders) throws CannotProvideCoderException { CoderProvider factory = commonTypesToCoderProviders.get(typeDescriptor.getRawType()); if (factory == null) { throw new CannotProvideCoderException( String.format("%s is not one of the common types.", typeDescriptor)); } return factory.coderFor(typeDescriptor, componentCoders); } }
@Override public <T> Coder<T> coderFor(TypeDescriptor<T> typeDescriptor, List<? extends Coder<?>> componentCoders) throws CannotProvideCoderException { Type t = typeDescriptor.getType(); if (IndexedRecord.class.isAssignableFrom(typeDescriptor.getRawType())) { Coder<T> c = LazyAvroCoder.<T> of(); return c; } throw new CannotProvideCoderException(String.format("Cannot provide %s because %s is not implement IndexedRecord", LazyAvroCoder.class.getSimpleName(), typeDescriptor)); } }
@Override public <T> Coder<T> coderFor( TypeDescriptor<T> typeDescriptor, List<? extends Coder<?>> componentCoders) throws CannotProvideCoderException { return OldCustomSerializableCoder.of((Class) typeDescriptor.getRawType()); } };
private void checkMap(String context, TypeDescriptor<?> type, Schema schema) { if (!isSubtypeOf(type, SortedMap.class)) { reportError(context, "%s may not be deterministically ordered", type); } // Avro (currently) asserts that all keys are strings. // In case that changes, we double check that the key was a string: Class<?> keyType = type.resolveType(Map.class.getTypeParameters()[0]).getRawType(); if (!String.class.equals(keyType)) { reportError(context, "map keys should be Strings, but was %s", keyType); } recurse(context, type.resolveType(Map.class.getTypeParameters()[1]), schema.getValueType()); }
@Override public StackManipulation dispatch(RestrictionTrackerParameter p) { // DoFnInvoker.ArgumentProvider.restrictionTracker() returns a RestrictionTracker, // but the @ProcessElement method expects a concrete subtype of it. // Insert a downcast. return new StackManipulation.Compound( simpleExtraContextParameter(RESTRICTION_TRACKER_PARAMETER_METHOD), TypeCasting.to(new TypeDescription.ForLoadedType(p.trackerT().getRawType()))); }
@Override public StackManipulation dispatch(WindowParameter p) { return new StackManipulation.Compound( simpleExtraContextParameter(WINDOW_PARAMETER_METHOD), TypeCasting.to(new TypeDescription.ForLoadedType(p.windowT().getRawType()))); }
@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)); }
@Override public StackManipulation dispatch(ElementParameter p) { return new StackManipulation.Compound( pushDelegate, MethodInvocation.invoke( getExtraContextFactoryMethodDescription(ELEMENT_PARAMETER_METHOD, DoFn.class)), TypeCasting.to(new TypeDescription.ForLoadedType(p.elementT().getRawType()))); }
@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)); }
@Test public void testTypeDescriptorOfRawType() throws Exception { assertEquals( TypeToken.of(String.class).getRawType(), TypeDescriptor.of(String.class).getRawType()); }
private static DynamicType.Builder<FieldValueGetter> implementGetterMethods( DynamicType.Builder<FieldValueGetter> builder, Method method) { TypeInformation typeInformation = TypeInformation.forGetter(method); return builder .method(ElementMatchers.named("name")) .intercept(FixedValue.reference(typeInformation.getName())) .method(ElementMatchers.named("type")) .intercept(FixedValue.reference(typeInformation.getType().getRawType())) .method(ElementMatchers.named("get")) .intercept(new InvokeGetterInstruction(method)); }