Skip to content

The Multiset code sample in TypeAdapterFactory's JavaDoc does not work #1335

@tlinkowski

Description

@tlinkowski

In Gson 2.8.5, the JavaDoc to TypeAdapterFactory presents a code sample of MultisetTypeAdapterFactory. However, this code sample does not work (instead of MultisetTypeAdapterFactory, CollectionTypeAdapterFactory is triggered). The problem is in these lines:

if (typeToken.getRawType() != Multiset.class || !(type instanceof ParameterizedType)) {
  return null;
}

Type elementType = ((ParameterizedType) type).getActualTypeArguments()[0];

Instead, the following should be used (like in CollectionTypeAdapterFactory):

Class<? super T> rawType = typeToken.getRawType();
if (!Multiset.class.isAssignableFrom(rawType)) {
  return null;
}

Type elementType = $Gson$Types.getCollectionElementType(type, rawType);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions