in sdk/src/main/java/com/google/cloud/dataflow/sdk/transforms/Create.java [245:295]
public Coder<T> getDefaultOutputCoder(PInput input) throws CannotProvideCoderException {
if (coder.isPresent()) {
return coder.get();
}
// First try to deduce a coder using the types of the elements.
Class<?> elementClazz = Void.class;
for (T elem : elems) {
if (elem == null) {
continue;
}
Class<?> clazz = elem.getClass();
if (elementClazz.equals(Void.class)) {
elementClazz = clazz;
} else if (!elementClazz.equals(clazz)) {
// Elements are not the same type, require a user-specified coder.
throw new CannotProvideCoderException(
"Cannot provide coder for Create: The elements are not all of the same class.");
}
}
if (elementClazz.getTypeParameters().length == 0) {
try {
@SuppressWarnings("unchecked") // elementClazz is a wildcard type
Coder<T> coder = (Coder<T>) input.getPipeline().getCoderRegistry()
.getDefaultCoder(TypeDescriptor.of(elementClazz));
return coder;
} catch (CannotProvideCoderException exc) {
// let the next stage try
}
}
// If that fails, try to deduce a coder using the elements themselves
Optional<Coder<T>> coder = Optional.absent();
for (T elem : elems) {
Coder<T> c = input.getPipeline().getCoderRegistry().getDefaultCoder(elem);
if (!coder.isPresent()) {
coder = Optional.of(c);
} else if (!Objects.equals(c, coder.get())) {
throw new CannotProvideCoderException(
"Cannot provide coder for elements of " + Create.class.getSimpleName() + ":"
+ " For their common class, no coder could be provided."
+ " Based on their values, they do not all default to the same Coder.");
}
}
if (!coder.isPresent()) {
throw new CannotProvideCoderException("Unable to infer a coder. Please register "
+ "a coder for ");
}
return coder.get();
}