RFR: 8180892: Correct handling of annotations on parameters [v2]

Chen Liang liach at openjdk.org
Fri Jun 9 08:37:43 UTC 2023


On Wed, 26 Apr 2023 14:46:53 GMT, Chen Liang <liach at openjdk.org> wrote:

>> This patch aims to correct handling of annotations on parameters with the help of `MethodParameters` attribute, which will be always available once #9862 is integrated.
>> 
>> It utilizes and expands upon the existing parameter matching logic present in `Executable::getAllGenericParameterTypes`, and delegate parameter parameterized types, parameter annotation, and parameter type annotation accesses through the matched results, if matching is available. If matching failed, it falls back to existing heuristics that works without `MethodParameters` attributes for annotations, in `Executable::handleParameterNumberMismatch` and `TypeAnnotationParser::buildAnnotatedTypes` (renamed `buildAnnotatedTypesWithHeuristics` in this patch)
>> 
>> `ParameterMappingTest` covers these scenarios with class files that have `MethodParameters` or `Signature` attributes stripped or preserved to ensure the new Reflection API implementation works for both class files generated before #9862 and after its integration.
>> 
>> Also special thanks to Joe Darcy for reviewing 8304918 (#13183); this brings much convenience to this patch.
>
> Chen Liang has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Fix assuming match without MethodParameters for type annotations, move implementation related to getAnnotatedParameterTypes to around it

Now that JDK 21 is RDP 1, should we take a look at this now?
This patch makes use of MethodParameters information to map (explicit) parameter annotations to formal parameters. In case that information is not available, the code falls back to the existing heuristic that guesses the offset for inner classes.

Two more potential issues with core reflection and annotations/generics include:
1. `getGenericParameterTypes()[i]` usages should be replaced by `getParameters()[i].getParameterizedType()`, which returns the correct generic type if MethodParameters attribute is present
2. There is some issues with `Constructor.getAnnotatedReceiverType()` due to some effectively-static local and anonymous classes (like ones in static methods and initializers); this is tracked at [JDK-8162500](https://bugs.openjdk.org/browse/JDK-8162500). Those classes don't carry enough information about whether they should have an mandated outer instance parameter, such as through a static flag in InnerClasses attribute; might need Javac tweaks to fix that.

-------------

PR Comment: https://git.openjdk.org/jdk/pull/13664#issuecomment-1584187625


More information about the core-libs-dev mailing list