RFR: 8273656: Improve java.lang.invoke.MethodType.parameterList() and its usage

Claes Redestad redestad at openjdk.java.net
Wed Sep 22 13:17:59 UTC 2021


On Wed, 22 Sep 2021 00:21:01 GMT, Claes Redestad <redestad at openjdk.org> wrote:

>> Currently the method is implemented like
>> 
>> public List<Class<?>> parameterList() {
>>   return Collections.unmodifiableList(Arrays.asList(ptypes.clone()));
>> }
>> 
>> This seems to be excessive, as three objects are allocated here. Instead we can use `List.of(ptypes)` which doesn't allocate anything for empty array and for one of length 1 and 2 it allocates lightweight objects with 2 fields, still copying longer arrays. This is likely to be fruitful as most of methods have 0-2 parameters.
>> 
>> Also there is a couple of cases when `MethodType.parameterLis()` is called to get its size, which is excessive either as we can use `MethodType.parameterCount()` instead.
>
> This change introduces a subtle behavior change in that `List.of` produce a `null`-hostile list, for example `list.contains(null)` will throw NPE. Does this need to be spelled out? (FTR I think such null-hostile behavior should be reconsidered)

> @cl4es null-issue was the one I was afraid most of all in this case. I think that the initial array `ptypes` never contains null elements due to it's business meaning (we cannot have a param without type). Also we do explicit null-check in `checkPtypes()` method, called from `makeImpl()`.

No, on the producer side there's no issue since a parameter type can't be `null`. The compatibility issue I have in mind is only the (very minor) issue that the returned list would now throw NPE if you call `mt.parameterList().contains(null)` where it before would just return `false`. I don't think this is a real issue, but the subtle behavior change might warrant a CSR to document the stricter behavior.

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

PR: https://git.openjdk.java.net/jdk/pull/5489


More information about the core-libs-dev mailing list