Skip to content

fix: Correct Multiset example in TypeAdapterFactory Javadoc#3001

Open
daguimu wants to merge 1 commit intogoogle:mainfrom
daguimu:fix/javadoc-multiset-example-issue1335
Open

fix: Correct Multiset example in TypeAdapterFactory Javadoc#3001
daguimu wants to merge 1 commit intogoogle:mainfrom
daguimu:fix/javadoc-multiset-example-issue1335

Conversation

@daguimu
Copy link
Copy Markdown
Contributor

@daguimu daguimu commented Mar 26, 2026

Problem

The TypeAdapterFactory Javadoc contains a MultisetTypeAdapterFactory code sample that does not work in practice. The factory uses typeToken.getRawType() != Multiset.class to check whether the type is a Multiset, but this identity check only matches the exact Multiset interface, not concrete implementations like HashMultiset or LinkedHashMultiset. As a result, the factory always returns null and Gson's built-in CollectionTypeAdapterFactory handles the type instead.

Root Cause

The raw type check uses != (reference equality) instead of isAssignableFrom, so it fails for all Multiset subtypes.

Fix

Change typeToken.getRawType() != Multiset.class to !Multiset.class.isAssignableFrom(typeToken.getRawType()), which correctly matches all Multiset implementations.

Impact

Documentation-only change. No runtime behavior affected.

Fixes #1335

The code sample used `typeToken.getRawType() != Multiset.class` which
only matches the exact Multiset class, not its subtypes like
HashMultiset or LinkedHashMultiset. This causes the factory to return
null for all practical Multiset implementations, falling through to
CollectionTypeAdapterFactory instead.

Change to `!Multiset.class.isAssignableFrom(typeToken.getRawType())`
to correctly match all Multiset subtypes.

Fixes google#1335
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant