Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-9320

Support serializable lambda expression

    XMLWordPrintableJSON

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 3.0.0-rc-2
    • Component/s: None
    • Labels:
      None

      Description

      Here is a word count example for Flink.

      As groovy's lambda expression is not serializable, the following code has to be replaced with sum(1) as workaround, or java.io.NotSerializableException: Non-serializable lambda will be raised.

      https://github.com/danielsun1106/flink-wordcount/blob/0.1/src/main/groovy/me/sunlan/flinklabs/wordcount/WordCount.groovy#L49

      We need generate a synthetic method $deserializeLambda$ in bytecode like the following:

        private static synthetic $deserializeLambda$(Ljava/lang/invoke/SerializedLambda;)Ljava/lang/Object;
         L0
          LINENUMBER 3 L0
          ALOAD 0
          INVOKEVIRTUAL java/lang/invoke/SerializedLambda.getImplMethodName ()Ljava/lang/String;
          ASTORE 1
          ICONST_M1
          ISTORE 2
          ALOAD 1
          INVOKEVIRTUAL java/lang/String.hashCode ()I
          LOOKUPSWITCH
            -50212388: L1
            default: L2
         L1
         FRAME APPEND [java/lang/String I]
          ALOAD 1
          LDC "lambda$main$9f099bdf$1"
          INVOKEVIRTUAL java/lang/String.equals (Ljava/lang/Object;)Z
          IFEQ L2
          ICONST_0
          ISTORE 2
         L2
         FRAME SAME
          ILOAD 2
          LOOKUPSWITCH
            0: L3
            default: L4
         L3
         FRAME SAME
          ALOAD 0
          INVOKEVIRTUAL java/lang/invoke/SerializedLambda.getImplMethodKind ()I
          BIPUSH 6
          IF_ICMPNE L4
          ALOAD 0
          INVOKEVIRTUAL java/lang/invoke/SerializedLambda.getFunctionalInterfaceClass ()Ljava/lang/String;
          LDC "java/lang/Runnable"
          INVOKEVIRTUAL java/lang/Object.equals (Ljava/lang/Object;)Z
          IFEQ L4
          ALOAD 0
          INVOKEVIRTUAL java/lang/invoke/SerializedLambda.getFunctionalInterfaceMethodName ()Ljava/lang/String;
          LDC "run"
          INVOKEVIRTUAL java/lang/Object.equals (Ljava/lang/Object;)Z
          IFEQ L4
          ALOAD 0
          INVOKEVIRTUAL java/lang/invoke/SerializedLambda.getFunctionalInterfaceMethodSignature ()Ljava/lang/String;
          LDC "()V"
          INVOKEVIRTUAL java/lang/Object.equals (Ljava/lang/Object;)Z
          IFEQ L4
          ALOAD 0
          INVOKEVIRTUAL java/lang/invoke/SerializedLambda.getImplClass ()Ljava/lang/String;
          LDC "Test"
          INVOKEVIRTUAL java/lang/Object.equals (Ljava/lang/Object;)Z
          IFEQ L4
          ALOAD 0
          INVOKEVIRTUAL java/lang/invoke/SerializedLambda.getImplMethodSignature ()Ljava/lang/String;
          LDC "()V"
          INVOKEVIRTUAL java/lang/Object.equals (Ljava/lang/Object;)Z
          IFEQ L4
          INVOKEDYNAMIC run()Ljava/lang/Runnable; [
            // handle kind 0x6 : INVOKESTATIC
            java/lang/invoke/LambdaMetafactory.altMetafactory(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;[Ljava/lang/Object;)Ljava/lang/invoke/CallSite;
            // arguments:
            ()V, 
            // handle kind 0x6 : INVOKESTATIC
            Test.lambda$main$9f099bdf$1()V, 
            ()V, 
            5, 
            0
          ]
          ARETURN
         L4
         FRAME CHOP 2
          NEW java/lang/IllegalArgumentException
          DUP
          LDC "Invalid lambda deserialization"
          INVOKESPECIAL java/lang/IllegalArgumentException.<init> (Ljava/lang/String;)V
          ATHROW
          MAXSTACK = 3
          MAXLOCALS = 3
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                daniel_sun Daniel Sun
                Reporter:
                daniel_sun Daniel Sun
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:

                  Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 40m
                  40m