FULL PRODUCT VERSION :
java version "1.8.0_31"
Java(TM) SE Runtime Environment (build 1.8.0_31-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.31-b07, mixed mode)
ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 6.1.7601]
A DESCRIPTION OF THE PROBLEM :
Constructor reference doesn't work with non-static inner class. Trying to do that will cause a java.lang.BootstrapMethodError. See the sample code.
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile and run the sample code.
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
"OK" is printed.
ACTUAL -
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 zhyi.Test$Inner.call(Test.java:18)
at zhyi.Test$Inner.access$100(Test.java:16)
at zhyi.Test.main(Test.java:9)
Caused by: java.lang.invoke.LambdaConversionException: Type mismatch in captured lambda parameter 0: expecting class zhyi.Test$Inner, found class zhyi.Test
at java.lang.invoke.AbstractValidatingLambdaMetafactory.validateMetafactoryArgs(AbstractValidatingLambdaMetafactory.java:256)
at java.lang.invoke.LambdaMetafactory.metafactory(LambdaMetafactory.java:303)
at java.lang.invoke.CallSite.makeSite(CallSite.java:289)
... 4 more
Java Result: 1
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
package zhyi;
import java.util.function.Supplier;
public class Test {
private Inner inner = new Inner();
public static void main(String[] args) {
new Test().inner.call();
}
private void print(Supplier<?> s) {
System.out.println(s.get());
}
private class Inner {
private void call() {
print(Bean::new); // Error!
// print(() -> new Bean()); // Works fine.
}
}
private class Bean {
@Override
public String toString() {
return "OK";
}
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Use normal lambda expression instead of constructor reference.