Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8284239

Consider adding `array::<new>(…)` and using it to compile `T[]::new` method references

    XMLWordPrintable

Details

    • generic
    • generic

    Description

      A DESCRIPTION OF THE PROBLEM :
      Valhalla intends to add the static `<new>` factory method, which would initially be used in place of the special `<init>` method for primitive and value classes.

      I propose to add the same static `<new>` factory method for arrays:
      - `static [Z."<new>":(I)[Z` for `boolean[]::new`, performing `iload_0; newarray 4; areturn;`.
      - `static [C."<new>":(I)[C` for `char[]::new`, performing `iload_0; newarray 5; areturn;`.
      - `static [F."<new>":(I)[F` for `float[]::new`, performing `iload_0; newarray 6; areturn;`.
      - `static [D."<new>":(I)[D` for `double[]::new`, performing `iload_0; newarray 7; areturn;`.
      - `static [B."<new>":(I)[B` for `byte[]::new`, performing `iload_0; newarray 8; areturn;`.
      - `static [S."<new>":(I)[S` for `short[]::new`, performing `iload_0; newarray 9; areturn;`.
      - `static [I."<new>":(I)[I` for `int[]::new`, performing `iload_0; newarray 10; areturn;`.
      - `static [J."<new>":(I)[J` for `long[]::new`, performing `iload_0; newarray 11; areturn;`.
      - `static [Ljava/lang/Object;."<new>":(I)[Ljava/lang/Object;` for `Object[]::new`, performing `iload_0; anewarray java/lang/Object; areturn;`, and similar for all other reference array classes.

      Multi-dimensional arrays will have static methods taking `I` arguments from 1 to the number of dimensions, corresponding to all valid invocations of the `multianewarray` instruction, e.g.:
      the class for `[[[[[[[I` would have the following static `<new>` factory methods:
      - `static [[[[[[[I."<new>":(I)[[[[[[[I`, performing `iload_0; anewarray [[[[[[I; areturn;`.
      - `static [[[[[[[I."<new>":(II)[[[[[[[I`, performing `iload_0; iload_1; multianewarray [[[[[[[I 2; areturn;`.
      - `static [[[[[[[I."<new>":(III)[[[[[[[I`, performing `iload_0; iload_1; iload_2; multianewarray [[[[[[[I 3; areturn;`.
      - `static [[[[[[[I."<new>":(IIII)[[[[[[[I`, performing `iload_0; iload_1; iload_2; iload_3; multianewarray [[[[[[[I 4; areturn;`.
      - `static [[[[[[[I."<new>":(IIIII)[[[[[[[I`, performing `iload_0; iload_1; iload_2; iload_3; iload 4; multianewarray [[[[[[[I 5; areturn;`.
      - `static [[[[[[[I."<new>":(IIIIII)[[[[[[[I`, performing `iload_0; iload_1; iload_2; iload_3; iload 4; iload 5; multianewarray [[[[[[[I 6; areturn;`.
      - and `static [[[[[[[I."<new>":(IIIIIII)[[[[[[[I`, performing `iload_0; iload_1; iload_2; iload_3; iload 4; iload 5; iload 6; multianewarray [[[[[[[I 7; areturn;`.

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

      This would the method reference `Object[]::new` to be compiled as:
      ```
      BootstrapMethods:
        0: #1 REF_invokeStatic java/lang/invoke/LambdaMetafactory.metafactory:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;
          Method arguments:
            #2 (I)Ljava/lang/Object;
            #3 REF_invokeStatic [Ljava/lang/Object;."<new>":(I)[Ljava/lang/Object;
            #4 (I)[Ljava/lang/Object;
      ```

      instead of the current:
      ```
      BootstrapMethods:
        0: #1 REF_invokeStatic java/lang/invoke/LambdaMetafactory.metafactory:(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;
          Method arguments:
            #2 (I)Ljava/lang/Object;
            #3 REF_invokeStatic Foo.lambda$methodName$0:(I)[Ljava/lang/Object;
            #4 (I)[Ljava/lang/Object;
      ```

      Doing this most likely depends on Valhalla introducing the `<new>` static factory method, unless we special‑case the `invokestatic` instruction to allow a `CONSTANT_Methodref` that points to a method named `<new>` if the `CONSTANT_Class` points to an array class, ahead of Valhalla introducing the general‑purpose static factory method for all classes.


      Attachments

        Activity

          People

            Unassigned Unassigned
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

              Created:
              Updated: