Fix field cache to let child class fields take precedence over parent - #12626
Open
gnodet wants to merge 1 commit into
Open
Fix field cache to let child class fields take precedence over parent#12626gnodet wants to merge 1 commit into
gnodet wants to merge 1 commit into
Conversation
…n over parent buildFieldCache() used put() which let parent fields overwrite child fields with the same name, because DeclaredMembers iterates child-first then parent. This broke Mojo parameter injection for shadowed fields (e.g. the 'skip' field in TestResourcesMojo vs ResourcesMojo). Switch to putIfAbsent() — matching the method cache — so the child's field takes precedence. Reported-in: apache/maven-resources-plugin#497 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
gnodet
commented
Jul 30, 2026
gnodet
left a comment
Contributor
Author
There was a problem hiding this comment.
LGTM ✅
Clean one-line bug fix that changes put() to putIfAbsent() in buildFieldCache(), making field cache resolution consistent with the existing method cache behavior. The fix correctly ensures child class fields take precedence over shadowed parent fields when DeclaredMembers iterates child-first.
Details:
- The fix is verified correct: Sisu's
DeclaredMembers/MemberIteratorstarts with the given class and walks togetSuperclass(), confirming child-first iteration order. Withput(), the parent's field entry overwrites the child's; withputIfAbsent(), the child's field (seen first) is correctly preserved. - The method cache at line 198 already uses
putIfAbsent(), so this fix resolves an asymmetry between the two caches introduced in the originald3bd71bbdfcommit ("perf: optimize CompositeBeanHelper with reflection caching"). - The test bean hierarchy (
ParentBean/ChildBeanboth declaringprivate boolean skip) accurately mirrors the real-world scenario in apache/maven-resources-plugin#497 whereResourcesMojoandTestResourcesMojoboth declareskip. - Well-tested with two new tests covering the direct fix and cache consistency.
This review was generated by an AI agent and may contain inaccuracies. Please verify all suggestions before applying.
Claude Code on behalf of gnodet
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.
Summary
EnhancedCompositeBeanHelper.buildFieldCache()to useputIfAbsent()instead ofput(), so child class fields take precedence over parent fields with the same nameParentBean/ChildBeanwith sameskipfield)Problem
When a child Mojo class shadows a parent's field (e.g. both
ResourcesMojoandTestResourcesMojodeclareprivate boolean skip), the field cache incorrectly resolves to the parent's field. This causes POM<configuration><skip>true</skip></configuration>to inject into the parent'sskipfield but not the child's — breakingTestResourcesMojo's skip behavior.Root cause:
DeclaredMembersiterates child-first then parent. The field cache usedput()which overwrites the child entry with the parent's. The method cache already correctly usesputIfAbsent()— this was an asymmetry.Fix
One-line change:
fieldMap.put(...)→fieldMap.putIfAbsent(...)inbuildFieldCache().This matches the method cache behavior and ensures the first-seen field (from the child class) wins.
Reported in: apache/maven-resources-plugin#497
Test plan
testSetPropertyOnShadowedFieldInjectsIntoChildField— verifies child field receives the injected valuetestShadowedFieldCacheConsistency— verifies cache reuse preserves correct field resolutionEnhancedCompositeBeanHelperTesttests pass🤖 Generated with Claude Code