Java 8: Duplicate method name&signature lambda

后端 未结 1 1692
清歌不尽
清歌不尽 2021-02-14 14:06

I was playing around with Java 8 lambdas, method references and interface default methods to explore the wonderful world of currying, and then I got this Java error I cannot und

相关标签:
1条回答
  • 2021-02-14 14:58

    Set the system property jdk.internal.lambda.dumpProxyClasses to point to a directory on your filesystem. You'll get the bytecode of the synthesized anonymous class com/test8/Main$$Lambda$1 dumped to that location. Open that class file with javap to see what has happened. You should find two declarations of the same method there.

    Update

    This is the javap output produced by the above procedure, when compiled with Eclipse:

    final class test.Main$$Lambda$1 implements test.Main$CurryBiConsumer {
      public void accept(java.lang.Object, java.lang.Object);
        Code:
           0: aload_1       
           1: checkcast     #14                 // class java/lang/String
           4: aload_2       
           5: checkcast     #16                 // class java/lang/Integer
           8: invokestatic  #22                 // Method test/Main.lambda$0:(Ljava/lang/String;Ljava/lang/Integer;)V
          11: return        
    
      public void accept(java.lang.Object, java.lang.Object);
        Code:
           0: aload_1       
           1: checkcast     #14                 // class java/lang/String
           4: aload_2       
           5: checkcast     #16                 // class java/lang/Integer
           8: invokestatic  #22                 // Method test/Main.lambda$0:(Ljava/lang/String;Ljava/lang/Integer;)V
          11: return        
    }

    And this is how it should be, and what javac does:

    final class test.Main$$Lambda$1 implements test.Main$CurryBiConsumer {
      public void accept(java.lang.Object, java.lang.Object);
        Code:
           0: aload_1       
           1: checkcast     #14                 // class java/lang/String
           4: aload_2       
           5: checkcast     #16                 // class java/lang/Integer
           8: invokevirtual #20                 // Method java/lang/Integer.intValue:()I
          11: invokestatic  #26                 // Method test/Main.display:(Ljava/lang/String;I)V
          14: return        
    }

    Conclusion: this is an issue with the Eclipse JDT compiler. Someone should report it :)

    Update

    As of Eclipse Luna this bug has been fixed.

    0 讨论(0)
提交回复
热议问题