Java Casting:Java 11 抛出 LambdaConversionException,而 1.8 不抛出
以下代码在 Java 1.8 VM 中运行良好,但在 Java 11 VM 中执行时会生成一个。区别在哪里,为什么它表现得这样?LambdaConversionException
法典:
public void addSomeListener(Component comp){
if(comp instanceof HasValue) {
((HasValue<?,?>) comp).addValueChangeListener(evt -> {
//do sth with evt
});
}
}
异常(仅限 V11):
Caused by: java.lang.invoke.LambdaConversionException: Type mismatch
for instantiated parameter 0: class java.lang.Object is not a subtype
of interface com.vaadin.flow.component.HasValue$ValueChangeEvent
at java.base/java.lang.invoke.AbstractValidatingLambdaMetafactory.checkDescriptor(AbstractValidatingLambdaMetafactory.java:308)
at java.base/java.lang.invoke.AbstractValidatingLambdaMetafactory.validateMetafactoryArgs(AbstractValidatingLambdaMetafactory.java:294)
at java.base/java.lang.invoke.LambdaMetafactory.altMetafactory(LambdaMetafactory.java:503)
at java.base/java.lang.invoke.BootstrapMethodInvoker.invoke(BootstrapMethodInvoker.java:138)
... 73 more
解决方法:
ValueChangeListener<ValueChangeEvent<?>> listener = evt -> {
// do sth with evt
};
((HasValue<?,?>) comp).addValueChangeListener(listener);
System:
OS: Windows 10
IDE: Eclipse 2018-12 (4.10.0)
Java (Compile): ecj
Java (Webserver): JDK 11.0.2
Webserver: Wildfly 15