Use declared parameter types for Whitebox.invokeMethod reflection#945
Merged
Use declared parameter types for Whitebox.invokeMethod reflection#945
Conversation
Resolve the target method's declared parameter types from the AST instead of using arg.getClass() which returns the runtime concrete class and fails when the method parameter is an interface/parent type. Three-tier resolution strategy: 1. Resolve the target method declaration and use its parameter types 2. Fall back to the argument's compile-time type 3. Fall back to arg.getClass() when no type info is available
timtebeek
approved these changes
Mar 27, 2026
Member
|
Thanks for picking this up still! |
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
.getClass()for resolving parameter types in theinvokeMethodrewrite.The previous implementation used
arg.getClass()to determine parameter types forgetDeclaredMethod(). This returns the runtime concrete class, which fails when the method's declared parameter type is an interface or parent class (e.g., method expectsListbutitems.getClass()returnsArrayList).Resolution strategy (three tiers)
getMethods(), finds a unique match by name + arity, and uses the method's declared parameter types. Handles subclass/interface mismatches correctly. Adds necessary imports for parameter types not already in the file.arg.getClass()— when no type info is available at all, preserves the original runtime behavior.Use cases now covered by tests
String.classinstead of"World".getClass()int) — generatesint.classinstead of autoboxedInteger.classList<String> items = new ArrayList<>()) — resolves toList.classArrayList<String> items, method expectsList) — resolves toList.classvia method declaration lookup, addsimport java.util.List