Fix assertions being generated for bridge methods instead of actual getter method#320
Open
instantapfel wants to merge 3 commits into
Open
Fix assertions being generated for bridge methods instead of actual getter method#320instantapfel wants to merge 3 commits into
instantapfel wants to merge 3 commits into
Conversation
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.
Problem
When generating assertions for getter methods, the AssertJ generator creates a single
GetterDescriptionper getter name. The description is currently derived from the first method returned byClass#getMethods()orClass#getDeclaredMethods().In most cases this works as expected. However, it becomes problematic for classes that contain compiler-generated bridge methods, for example:
For
ConcreteClass, the compiler generates two methods namedgetValue():String getValue()(the actual implementation)Object getValue()(a synthetic bridge method)The Java Reflection API does not guarantee the order of methods returned by
Class#getDeclaredMethods()orClass#getMethods(). As a result, the generator may pick either method when building theGetterDescription.This can lead to non-deterministic output, generating either
hasValue(String value)orhasValue(Object value)depending on the method order returned at runtime. Consequently, the generated assertion files may change between executions of the generator even when the source code itself has not changed.Solution
This PR excludes bridge methods when collecting getter descriptions. This ensures that the generator consistently uses the actual getter implementation and produces deterministic assertion methods.