以下测试之一无效。为什么呢?
public class SortedInterfacesTest {
private static final Logger log = LoggerFactory.getLogger(SortedInterfacesTest.class);
<T> void forAll(Consumer<T> consumer, T... values) { }
@Test
public void firstTest() {
forAll(Shape::draw, new MyShape(), new Universal());
}
@Test
public void secondTest() {
log.info("\njava.version {}", System.getProperty("java.version"));
forAll(Picture::draw, new MyPicture(), new Universal());
}
interface Shape { void draw(); }
interface Marker { }
interface Picture { void draw(); }
class MyShape implements Marker, Shape { public void draw() {} }
class MyPicture implements Marker, Picture { public void draw() {} }
class Universal implements Marker, Picture, Shape { public void draw() {} }
}
毕竟,一对Shape和MyShape是一对对称的Picture和MyPicture。
第二次测试失败并出现以下错误(使用cli:mvn.exe测试):
-------------------------------------------------------
T E S T S (Java.version: 1.8.0_102)
-------------------------------------------------------
Running SortedInterfacesTest
01:36:28.234 [main] INFO SortedInterfacesTest -
java.version 1.8.0_102
Tests run: 2, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.223 sec <<< FAILURE!
secondTest(SortedInterfacesTest) Time elapsed: 0.009 sec <<< ERROR!
java.lang.BootstrapMethodError: call site initialization exception
at java.lang.invoke.CallSite.makeSite(Unknown Source)
at java.lang.invoke.MethodHandleNatives.linkCallSiteImpl(Unknown Source)
at java.lang.invoke.MethodHandleNatives.linkCallSite(Unknown Source)
at SortedInterfacesTest.secondTest(SortedInterfacesTest.java:18)
Java 9 + 181上的Stacktrace:
java.lang.BootstrapMethodError: call site initialization exception
at java.base/java.lang.invoke.CallSite.makeSite(CallSite.java:385)
at java.base/java.lang.invoke.MethodHandleNatives.linkCallSiteImpl(MethodHandleNatives.java:250)
at java.base/java.lang.invoke.MethodHandleNatives.linkCallSite(MethodHandleNatives.java:240)
at play.Play.firstTest(Play.java:12)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)
at com.intellij.rt.execution.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:47)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)
Caused by: java.lang.invoke.LambdaConversionException: Invalid receiver type interface play.Play$Marker; not a subtype of implementation type interface play.Play$Shape
at java.base/java.lang.invoke.AbstractValidatingLambdaMetafactory.validateMetafactoryArgs(AbstractValidatingLambdaMetafactory.java:254)
at java.base/java.lang.invoke.LambdaMetafactory.metafactory(LambdaMetafactory.java:311)
at java.base/java.lang.invoke.CallSite.makeSite(CallSite.java:332)
... 25 more
答案 0 :(得分:3)
新错误已注册为JDK-8191655。 解决方法:使用普通的lambda表达式而不是方法引用。