RFR: 8299505: findVirtual on array classes incorrectly restricts the receiver type [v2]

Mandy Chung mchung at openjdk.org
Wed May 31 04:09:56 UTC 2023


On Wed, 31 May 2023 03:43:25 GMT, Chen Liang <liach at openjdk.org> wrote:

>> test/jdk/java/lang/invoke/findVirtual/FindVirtualArrayCloneTest.java line 70:
>> 
>>> 68:         var cloneMh = MethodHandles.lookup()
>>> 69:                 .findVirtual(String[].class, "clone", MethodType.methodType(Object.class));
>>> 70:         var mhClone = (String[]) (Object) cloneMh.invokeExact((String[]) array);
>> 
>> Suggestion:
>> 
>>         var mhClone = (String[]) cloneMh.invokeExact((String[]) array);
>
> the clone Method handle will have a type of `(String[])Object`, as `findVirtual` for `clone` uses the Object return method type, and there is no covariant override in array classes (which don't declare any methods)

You're right.  Alternatively (perhaps easier to read):


-                .findVirtual(String[].class, "clone", MethodType.methodType(Object.class));
-        var mhClone = (String[]) (Object) cloneMh.invokeExact((String[]) array);
+                .findVirtual(String[].class, "clone", MethodType.methodType(Object.class))
+                .asType(MethodType.methodType(String[].class, String[].class));
+        var mhClone = (String[]) cloneMh.invokeExact((String[]) array);

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

PR Review Comment: https://git.openjdk.org/jdk/pull/13855#discussion_r1211064561


More information about the core-libs-dev mailing list