Skip to content

Avoid creating the discarded reflective adapter for the runtime type - #3206

Open
Inf1mum wants to merge 1 commit into
google:mainfrom
Inf1mum:runtime-type-optionally-reflective-adapter
Open

Inf1mum wants to merge 1 commit into
google:mainfrom
Inf1mum:runtime-type-optionally-reflective-adapter

Conversation

@Inf1mum

@Inf1mum Inf1mum commented Dec 5, 2026

Copy link
Copy Markdown

Fixes #3122

Purpose

When a value is serialized as an element of an array or a collection (or through any other TypeAdapterRuntimeTypeWrapper path), the adapter for the runtime type was resolved unconditionally via Gson.getAdapter. When only a reflective adapter could be created for the runtime type and its creation failed — for example for sun.reflect.generics.reflectiveObjects.WildcardTypeImpl, whose package java.base does not export — that JsonIOException escaped to the caller even in the branch where the wrapper discards the runtime adapter and uses the adapter for the declared type. Registering an adapter for the declared interface was therefore enough to serialize a value directly, but not to serialize the same value inside an array or a collection.

Description

Implements the direction proposed by @eamonnmcmanus in this comment: a structural lookup

<T> TypeAdapter<T> getOptionallyReflectiveAdapter(TypeToken<T> type, boolean allowReflective)

following the sketch by @Marcono1234. The lookup walks the factories in order:

  • Factories before ReflectiveTypeAdapterFactory run normally: their adapters are returned and cached like getAdapter does (typeTokenCache plus the FutureTypeAdapter recursion guard), and failures of user-registered factories for the runtime type keep propagating untouched, since such an adapter would be the first preference and its error must not be silenced.
  • When the reflective factory is reached: with allowReflective == false the lookup stops and returns null without ever calling ReflectiveTypeAdapterFactory.create, and the wrapper keeps the delegate for the declared type. With allowReflective == true (the delegate itself is reflective) the reflective adapter is created as before and its creation failures keep failing fast.

The method is exposed to com.google.gson.internal.bind through a new GsonInternalAccess bridge modeled after JsonReaderInternalAccess, so it stays out of the public API.

This supersedes #3123 by @jaideeppyne, which caught JsonIOException around the lookup instead; the discussion on the issue established that catching cannot distinguish the reflective factory's own failure from a failure of a user factory that should have won.

What does not change

Both are covered by testUserFactoryFailureForRuntimeTypePropagates and testReflectiveCreationFailureStillPropagatesWhenDelegateIsReflective in TypeAdapterRuntimeTypeWrapperTest.

Verification

  • RED on master 33f760f: mvn -pl :gson test -Dtest=TypeAdapterRuntimeTypeWrapperTest — Tests run: 11, Errors: 2 (testCustomAdapterForDeclaredTypeUsedForArrayElements and testCustomAdapterForDeclaredTypeUsedForCollectionElements, both failing with JsonIOException: Failed making field 'sun.reflect.generics.reflectiveObjects.WildcardTypeImpl#upperBounds' accessible).
  • GREEN after the fix: same class, Tests run: 11, Failures: 0, Errors: 0.
  • Full gson module mvn -pl :gson verify (includes the spotless check): Tests run: 4761, Failures: 0, Errors: 0, Skipped: 23 (pre-existing skips). Temurin 21.

TypeAdapterRuntimeTypeWrapper.write resolved the runtime type adapter unconditionally, so when only a reflective adapter could be created for the runtime type and its creation failed (for example a JDK-internal class in a package java.base does not open), the failure escaped to the caller even though the wrapper would have discarded that adapter and used the adapter for the declared type.

The lookup now goes through the new internal Gson.getOptionallyReflectiveAdapter (exposed like JsonReaderInternalAccess via GsonInternalAccess), which walks the factories up to the reflective one: adapters selected before it are returned with the usual caching, and if only the reflective factory could handle the type it returns null without creating the adapter, unless allowReflective is set, in which case behavior is unchanged. Failures of user-registered factories for the runtime type keep propagating, since that adapter is the first preference.

This branch has not been deployed

No deployments
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.

TypeAdapterRuntimeTypeWrapper eagerly resolves the runtime-type adapter, so a failure building an adapter it would have discarded escapes to the caller

1 participant