Fix dead code and side-effect bug in BeanPropertyWriter.toString() - #5821
Merged
Merged
Conversation
Co-authored-by: pjfanning <11783444+pjfanning@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix subtle bug in BeanPropertyWriter.toString() method
Fix dead code and side-effect bug in Mar 20, 2026
BeanPropertyWriter.toString()
pjfanning
marked this pull request as ready for review
March 20, 2026 22:04
cowtowncoder
approved these changes
Mar 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
BeanPropertyWriter.toString()had two bugs: a dead null-check on_accessor(afinalfield, never null) that made the"virtual"branch unreachable, and a call to_accessor.toString()that inadvertently triggered lazyMethodHandleinitialization — undesirable in a diagnostic method, especially beforefixAccess()is called.Changes
_accessor != nullwith_member == null— the correct semantic check for virtual properties (those with no backing field or method)_accessor.toString()call — instead describe the member directly from_member(field name or method name) without touching theMethodHandleWarning
Firewall rules blocked me from connecting to one or more addresses (expand for details)
I tried to connect to the following addresses, but was blocked by firewall rules:
central.sonatype.com/usr/lib/jvm/temurin-17-jdk-amd64/bin/java /usr/lib/jvm/temurin-17-jdk-amd64/bin/java --enable-native-access=ALL-UNNAMED -classpath /usr/share/apache-maven-3.9.13/boot/plexus-classworlds-2.9.0.jar -Dclassworlds.conf=/usr/share/apache-maven-3.9.13/bin/m2.conf -Dmaven.home=/usr/share/apache-maven-3.9.13 -Dlibrary.jansi.path=/usr/share/apache-maven-3.9.13/lib/jansi-native -Dmaven.multiModuleProjectDirectory=/home/REDACTED/work/jackson-databind/jackson-databind org.codehaus.plexus.classworlds.launcher.Launcher compile -q(dns block)If you need me to access, download, or install something from one of these locations, you can either:
Original prompt
Fix subtle bug in
BeanPropertyWriter.toString()Problem
In
BeanPropertyWriter.toString(), there is a dead code null-check on_accessorand a potentially problematic call to_accessor.toString():There are two bugs here:
Dead code null-check:
_accessoris declared asprotected final GetterHolder _accessor = new GetterHolder();— it is afinalfield initialized inline and can never benull. Theif (_accessor != null)branch is dead code, and theelsebranch (sb.append("virtual")) is therefore unreachable. Virtual properties (where_memberis null) are never correctly identified as "virtual" in the string output.Unintended side-effect in a diagnostic method:
_accessor.toString()delegates toUnreflectHandleSupplier.toString(), which callsget().toString()on the underlyingMethodHandle. This triggers lazy initialization of theMethodHandle— which is undesirable in a diagnostic/debugging method, especially iffixAccess()has not yet been called, potentially causingIllegalAccessExceptionor other unexpected behavior.Fix
The
toString()method should be updated to:_accessor != nullnull-check._memberto determine whether the property is "virtual" (i.e.,_member == null), which is the actual semantic distinction._accessor.toString()(which triggersMethodHandleinitialization). Instead, describe the accessor based on_membertype (e.g., field name or method signature) without touching theMethodHandle.Location
File:
src/main/java/tools/jackson/databind/ser/BeanPropertyWriter.javaThe
toString()method currently reads:It should be fixed to properly distinguish virtual properties (where
_member == null) from real field/method-backed properties, and must not triggerMethodHandleinitialization as a side-effect of being called.The following is the prior conversation context from the user's chat exploration (may be truncated):
User: try again
Assistant: Here's a walkthrough of the key parts of
BeanPropertyWriter.javaand how they fit together:BeanPropertyWriter— Key Concepts1. Core Purpose
BeanPropertyWriteris the workhorse for serializing a single named property of a Java bean. It holds all configuration — name, type info, serializers, filtering rules — and drives the actual JSON output for that property.2.
GetterHolder— LazyMethodHandlefor Property AccessGetterHolderextendsUnreflectHandleSupplier, which is a lazy, memoized, thread-safe wrapper for aMethodHandle: