This line
((UnaryOperator<Integer>)o->o).toString();
written anywhere in a class, and compiled with Eclipse Kepler, will cause a failure when that line is reached in execution:
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 test.Test.main(Test.java:7)
Caused by: java.lang.ClassFormatError: Duplicate method name&signature in class file test/Test$$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:474)
at java.lang.invoke.CallSite.makeSite(CallSite.java:301)
... 2 more
On its own this is nothing very noteworthy, yet another bug in Eclipse's Java 8 compiler. However, I am intrigued by the details of the failure. If we enable the jdk.internal.lambda.dumpProxyClasses system property and retrieve the generated lambda class code, parsing it with javap will reveal that the class has two identical apply methods defined, one of them flagged as a bridge method:
{
public java.lang.Object apply(java.lang.Object);
descriptor: (Ljava/lang/Object;)Ljava/lang/Object;
flags: ACC_PUBLIC
Code:
stack=1, locals=2, args_size=2
0: aload_1
1: checkcast #14 // class java/lang/Integer
4: invokestatic #20 // Method test/Test.lambda$0:(Ljava/lang/Integer;)Ljava/lang/Integer;
7: areturn
public java.lang.Object apply(java.lang.Object);
descriptor: (Ljava/lang/Object;)Ljava/lang/Object;
flags: ACC_PUBLIC, ACC_BRIDGE
Code:
stack=1, locals=2, args_size=2
0: aload_1
1: checkcast #14 // class java/lang/Integer
4: invokestatic #20 // Method test/Test.lambda$0:(Ljava/lang/Integer;)Ljava/lang/Integer;
7: areturn
}
I understand that bridge methods are needed with Generics in order to maintain backwards compatibility; however, I cannot understand how a bug in Eclipse could force the JDK into synthesizing a defective method pair.
For comparison, if we slightly change our Java line to:
((Object)((UnaryOperator<Integer>)o->o)).toString();
then we get just one, non-bridge method, and no failures:
{
public java.lang.Object apply(java.lang.Object);
descriptor: (Ljava/lang/Object;)Ljava/lang/Object;
flags: ACC_PUBLIC
Code:
stack=1, locals=2, args_size=2
0: aload_1
1: checkcast #14 // class java/lang/Integer
4: invokestatic #20 // Method test/Test.lambda$0:(Ljava/lang/Integer;)Ljava/lang/Integer;
7: areturn
}
Could it be that this is in fact a bug in the JDK, but it isn't provoked by javac?
I am using javac 1.8.0_20 on OS X and Eclipse Kepler SR2 with the Java 8 patch.
Update: bootstrap method invocation
Eclipse compiler is responsible for emitting the correct invokedynamic bootstrap method invocation (the lambda metafactory). This is how the bootstrap method arguments look like for the failing case:
BootstrapMethods:
0: #39 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;
Method arguments:
#41 (Ljava/lang/Object;)Ljava/lang/Object;
#44 invokestatic test/Test.lambda$0:(Ljava/lang/Integer;)Ljava/lang/Integer;
#45 (Ljava/lang/Integer;)Ljava/lang/Integer;
#46 4
#47 1
#48 (Ljava/lang/Object;)Ljava/lang/Object;
Owing to Brian's help it is now clear to me that the last two lines above inflict the error:
- number 1 on
#47means "there is one bridge method"; (Ljava/lang/Object;)Ljava/lang/Object;on#48describes the bridge method's signature, which is obviously the same as the main signature.
For comparison, this is the working case:
BootstrapMethods:
0: #53 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:
#55 (Ljava/lang/Object;)Ljava/lang/Object;
#58 invokestatic test/Test.lambda$0:(Ljava/lang/Integer;)Ljava/lang/Integer;
#59 (Ljava/lang/Integer;)Ljava/lang/Integer;
Here the simpler metafactory method is used and no bridge methods are created.