jdk8 lambda的方法引用引起的编译器bug
来源:互联网 发布:郭靖 杨过 知乎 编辑:程序博客网 时间:2024/05/22 08:23
jdk8 lambda的方法引用引起的编译器bug
当使用class::method
这种引用方式的时候,如果receiver类有多个super type,在编译的时候没有问题,但是在运行的时候就会异常Invalid receiver type class...
。
解决方法是:
改为lambda表达式。
原文:
You ran into the same compiler bug that has been discussed in this question and that >question.
The problem occurs whenever an intersection type is involved and you are using a method >reference using a receiver type other than the first one (the first type is the one that will >remain after type erasure).
So when you replace the method reference with a lambda expression, you are not affected by >the bug anymore. If you remove the Serializable from the types instead, the inferred element >type of the Stream will be Fruit, i.e. not an intersection type, and again the problem does >not occur. But with the two element types implementing Fruit and Serializable, the compiler >will infer the element type Object&Fruit&Serializable and the raw type will be Object which >provokes the error when using a method reference with the receiver type Fruit. You can easily >work around this:
Stream.of(apples.stream(), oranges.stream())
.flatMap(Function.identity())
.map(Fruit::getPickingMonth) // no more exception on this line
.forEachOrdered(System.out::println);
The compiled code will be identical to your original, but the formal result type of the >flatMap operation will be Stream, ignoring all other artifacts of the inferred >intersection type. As a consequence the method reference Fruit::getPickingMonth will >implement the type Function
- jdk8 lambda的方法引用引起的编译器bug
- JDK8新特性☞方法构造方法的引用
- java8的lambda表达式以及方法引用
- Lambda的语法和方法引用
- Jdk8之lambda表达式的使用(一)
- Jdk8之lambda表达式的使用(二)
- Jdk8之lambda表达式的使用(一)
- Jdk8之lambda表达式的使用(二)
- JDK8中Lambda表达式的使用
- unsigned引起的bug
- typedef引起的bug
- c#编译器的bug
- VC6编译器的BUG
- 编译器也会出错--由VC优化引起的BUG
- jdk8-方法引用
- 缺jar引起的bug
- memcpy引起的一个bug
- 字符串拷贝引起的bug
- Java后台框架篇--Spring整合redia自动缓存管理
- 上班要打扮美美的
- 电路板书签
- hdu 1075(字典树)
- 转:通俗理解T检验与F检验的区别
- jdk8 lambda的方法引用引起的编译器bug
- java web开发中,如何查看JSP和Servlet版本
- 我的投资模型
- 顺序线性表的基本操作
- Ubuntu终端光标形状切换
- CentOS 7 源码安装 mysql5.7.12 完整教程
- Parameter 'uid' not found. Available parameters are [0, 1, param1, param2]
- linux 信号signal和sigaction理解
- Java后台框架篇--Spring Security保护bean方法