public static FieldType fieldTypeForJavaType(TypeDescriptor typeDescriptor) {
    // TODO: Convert for registered logical types.
    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(Iterable.class))) {
      return getIterableFieldType(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);
      }
      return FieldType.of(typeName);
    }
  }
@Test
  public void testMapTypeToFieldType() {
    assertEquals(
        FieldType.map(FieldType.STRING, FieldType.INT64),
        FieldTypeDescriptors.fieldTypeForJavaType(
            TypeDescriptors.maps(TypeDescriptors.strings(), TypeDescriptors.longs())));
    assertEquals(
        FieldType.map(FieldType.STRING, FieldType.array(FieldType.INT64)),
        FieldTypeDescriptors.fieldTypeForJavaType(
            TypeDescriptors.maps(
                TypeDescriptors.strings(), TypeDescriptors.lists(TypeDescriptors.longs()))));
    assertEquals(
        FieldType.map(FieldType.STRING, FieldType.iterable(FieldType.INT64)),
        FieldTypeDescriptors.fieldTypeForJavaType(
            TypeDescriptors.maps(
                TypeDescriptors.strings(), TypeDescriptors.iterables(TypeDescriptors.longs()))));
  }