美文网首页
JDK8遇到LambdaConversionException的

JDK8遇到LambdaConversionException的

作者: fzhyzamt | 来源:发表于2020-03-10 15:26 被阅读0次

    示例代码

    @Test
    public void testJdkSupportGenericType() {
        dummy("");
    }
    <T extends Serializable & CharSequence> int dummy(T value) {
        return Optional.ofNullable(value).map(CharSequence::length).orElse(0);
    }
    

    在OpenJDK-8u172-b11下
    将会抛出异常

    java.lang.BootstrapMethodError: call site initialization exception
    
        at java.lang.invoke.CallSite.makeSite(CallSite.java:341)
        at java.lang.invoke.MethodHandleNatives.linkCallSiteImpl(MethodHandleNatives.java:307)
        at java.lang.invoke.MethodHandleNatives.linkCallSite(MethodHandleNatives.java:297)
        at com.example.ExampleTest.dummy(ExampleTest.java:34)
        at com.example.ExampleTest.testJdkSupportGenericType(ExampleTest.java:30)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:498)
        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 java.io.Serializable; not a subtype of implementation type interface java.lang.CharSequence
        at java.lang.invoke.AbstractValidatingLambdaMetafactory.validateMetafactoryArgs(AbstractValidatingLambdaMetafactory.java:233)
        at java.lang.invoke.LambdaMetafactory.metafactory(LambdaMetafactory.java:303)
        at java.lang.invoke.CallSite.makeSite(CallSite.java:302)
        ... 10 more
    

    解决方法

    • 使用s -> s.length() 替代 CharSequence::length
    • 将要用到的泛型放到前边
    • 升级到修复了这个问题的JDK

    相关问题

    相关文章

      网友评论

          本文标题:JDK8遇到LambdaConversionException的

          本文链接:https://www.haomeiwen.com/subject/gqgwdhtx.html