fix: ReferenceBuilder.getTypeReference can handle complex type-params in generics #6176
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.
Fix #6068
The code is similar to and inspired from the local fix that was submitted in the Github issue/bug-report itself by @josple.
I have also added a total of 5 test-cases: 1 of which (
testGetTypeReferenceComplexGenericsCase
) failed with the original code, but passes with the new version of the code.Included in the test cases are also 3 "sanity check" tests (the last 3) for some of the other things that
getTypeReference
should be doing based on my understanding, though they might not be strictly related to this change.Apart from the
null
return check (when the argument is not a type) and thearray
case, I think there should be sanity checks for the other cases of input forgetReferenceType
here (more on why these 2 are absent in the Notes below). If they're not needed in this PR, let me know and I will remove :)Some Notes
,
with the following code:final String[] split = m.group(2).split(",");
. We also seem to be doing that exact same thing in thegetTypeParameterReference
method (here). I didn't look in to this and not entirely sure if this is also an issue (wanted to keep the scope of this fix limited), but just wanted to call out in case anyone else might know more of it and if that is something which should be fixed too.getTypeReference
is public, since its only usages seem to be inReferenceBuilder
itself (as seen below, the extra 5 were the tests that I've added). I made this fix (and added the tests) assuming we want to keepgetTypeReference
public, but if we think this should be a private method, then another way to fix this could be to make the newgetStringTypeParameters
function I've added a static function in a utility class (and convert these tests to tests of that utility function). Let me know if that's a better approach!ReferenceBuilder.getTypeReference
, I noticed that for the casegetTypeReference("notType<Integer>")
, the function actually returned aCtTypeReference
instead ofnull
. I thoughtnull
was supposed to be returned based on the documentation sincenotType
won't be a name of an actual type (since it does not start with an upper-case letter)? Or does that only apply to the case when the type is not a generic type? In any case, there is no test which checks for thenull
return here.setPackageOrDeclaringType
method. I tried a string with an array of a custom generic type first, then aHashMap
array, then a simpleString
array, but in all cases I got an error similar to this. Eventually decided against digging deep in to this since that could have added more setup and bloated this PR even more.@algomaster99 @monperrus @I-Al-Istannen would appreciate a peek whenever you have the time! 😄 🙏🏽