JDK 11.0.2 编译因匿名参数化类类型推断上的 javac NPE 而失败 [英] JDK 11.0.2 compilation fails with javac NPE on anonymous parameterized class type inference

查看:68
本文介绍了JDK 11.0.2 编译因匿名参数化类类型推断上的 javac NPE 而失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码(spring-web 5.1.2)

Code (spring-web 5.1.2)

public static void main(String[] args) {
    RestTemplate restTemplate = new RestTemplate();

    HttpHeaders headers = new HttpHeaders();
    headers.set(HttpHeaders.AUTHORIZATION, "token");
    HttpEntity<Object> requestEntity = new HttpEntity<>(headers);

    ResponseEntity<Object> test = restTemplate.exchange(
            "https://example.com",
            HttpMethod.GET,
            new HttpEntity<>(headers),
            new ParameterizedTypeReference<>() { // fails here
            });
}

OracleJDK 1.8(预期输出)

无法推断类型参数org.springframework.core.ParameterizedTypeReference

cannot infer type arguments for org.springframework.core.ParameterizedTypeReference

原因:不能对匿名内部类使用<>"

reason: cannot use '<>' with anonymous inner classes

OracleJDK 11.0.2(非预期输出)

编译器消息文件损坏:key=compiler.misc.msg.bug参数=11.0.2、{1}、{2}、{3}、{4}、{5}、{6}、{7}java.lang.NullPointerException 在jdk.compiler/com.sun.tools.javac.comp.Flow$FlowAnalyzer.visitApply(Flow.java:1235)在jdk.compiler/com.sun.tools.javac.tree.JCTree$JCMethodInvocation.accept(JCTree.java:1634)在jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49)在jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:398)在jdk.compiler/com.sun.tools.javac.comp.Flow$FlowAnalyzer.visitVarDef(Flow.java:989)
...

compiler message file broken: key=compiler.misc.msg.bug arguments=11.0.2, {1}, {2}, {3}, {4}, {5}, {6}, {7} java.lang.NullPointerException at jdk.compiler/com.sun.tools.javac.comp.Flow$FlowAnalyzer.visitApply(Flow.java:1235) at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCMethodInvocation.accept(JCTree.java:1634) at jdk.compiler/com.sun.tools.javac.tree.TreeScanner.scan(TreeScanner.java:49) at jdk.compiler/com.sun.tools.javac.comp.Flow$BaseAnalyzer.scan(Flow.java:398) at jdk.compiler/com.sun.tools.javac.comp.Flow$FlowAnalyzer.visitVarDef(Flow.java:989)
...

如果我将菱形运算符更改为显式类型

If I change diamond operator to explicit type

new ParameterizedTypeReference<>(){}new ParameterizedTypeReference(){}

然后代码在两个 JDK 上都编译成功.

then the code compiles successfully on both JDKs.

这是一个已知的编译器错误吗?

Is it a known compiler bug?

推荐答案

Bug (JDK-8212586) 已经提交并修复版本12.

Bug (JDK-8212586) has been already submitted and fixed in version 12.

最小的、可验证的例子:

Minimal, verifiable example:

public static void main(String[] args) {
    new Bug<>(){};
}

static class Bug<T> {

    Bug() {
        test(new ParameterizedTypeReference<>(){});
    }

    void test(ParameterizedTypeReference<T> typeReference) {
    }
}

一些细节.

Fix 也已向后移植到 JDK 11 - https://bugs.openjdk.java.net/browse/JDK-8220578.

Fix has been also backported to JDK 11 - https://bugs.openjdk.java.net/browse/JDK-8220578.

可用的起始 JDK 11.0.4.

Available starting JDK 11.0.4.

这篇关于JDK 11.0.2 编译因匿名参数化类类型推断上的 javac NPE 而失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆