我正在玩Java 8 lambdas,方法引用和接口默认方法来探索奇妙的currying世界,然后我得到了这个我无法理解的Java错误。
以下是代码:
public class Main {
public interface CurryBiConsumer<T, U> extends BiConsumer<T, U> {
default public CurryConsumer<U> curryFirst(T t) {
return (u) -> accept(t, u);
}
default public CurryConsumer<T> currySecond(U u) {
return (t) -> accept(t, u);
}
}
public interface CurryConsumer<T> extends Consumer<T> {
default public Runnable curry(T t) {
return () -> accept(t);
}
}
static void execute(Runnable r) {
System.out.println("BEFORE");
r.run();
System.out.println("AFTER");
}
static void display(String str, int count) {
System.out.println("DISP: " + str + " " + count);
}
public static void main(String[] args) {
CurryBiConsumer<String, Integer> bc = Main::display;
execute(bc.curryFirst("Salomon").curry(42));
}
}
Eclipse没有给我任何错误但是当我运行它时,我得到了这个运行时错误:
Exception in thread "main" java.lang.BootstrapMethodError: call site initialization exception
at java.lang.invoke.CallSite.makeSite(CallSite.java:328)
at java.lang.invoke.MethodHandleNatives.linkCallSite(MethodHandleNatives.java:296)
at com.test8.Main.main(Main.java:34)
Caused by: java.lang.ClassFormatError: Duplicate method name&signature in class file com/test8/Main$$Lambda$1
at sun.misc.Unsafe.defineAnonymousClass(Native Method)
at java.lang.invoke.InnerClassLambdaMetafactory.spinInnerClass(InnerClassLambdaMetafactory.java:324)
at java.lang.invoke.InnerClassLambdaMetafactory.buildCallSite(InnerClassLambdaMetafactory.java:194)
at java.lang.invoke.LambdaMetafactory.altMetafactory(LambdaMetafactory.java:473)
at java.lang.invoke.CallSite.makeSite(CallSite.java:301)
... 2 more
有人可以解释这是什么错误,它为什么会发生以及如何绕过它?
谢谢:)
答案 0 :(得分:13)
将系统属性jdk.internal.lambda.dumpProxyClasses
设置为指向文件系统上的目录。您将获取转储到该位置的合成匿名类com/test8/Main$$Lambda$1
的字节码。使用javap
打开该类文件,看看发生了什么。你应该在那里找到两个相同方法的声明。
当使用Eclipse编译时,这是上述过程产生的javap
输出:
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 }
这就是它应该是什么,以及javac
做了什么:
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 }
结论:这是Eclipse JDT编译器的一个问题。有人应该报告:)
自Eclipse Luna开始,此错误已得到修复。