google / google/gson

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

Open
#1,335 1 comment 0 reactions 0 assignees View on GitHub
bug documentation
Dominant language
Java
Stars
24.2k
Forks
4.5k
Avg merge
6d 4h
Merged PRs (30d)
12

Description

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 rawType = typeToken.getRawType();
if (!Multiset.class.isAssignableFrom(rawType)) {
return null;
}

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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.