JDK 11.0.2 compilation fails with javac NPE on anonymous parameterized class type inference

JavaJavacType InferenceJava 11Compiler Bug

Java Problem Overview


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 (expected output)

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

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

OracleJDK 11.0.2 (not expected output)

> 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<>(){} to new ParameterizedTypeReference<Object>(){}

then the code compiles successfully on both JDKs.

Is it a known compiler bug?

Java Solutions


Solution 1 - Java

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) {
    }
}

Some Details.


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

Available starting JDK 11.0.4.

Solution 2 - Java

I Had the same error,you need to create a function :

ParameterizedTypeReference<Object> createParameterizedTypeReference(){ return new ParameterizedTypeReference<>(){}; }

and call it :

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

Solution 3 - Java

As pointed out in previous comments, the problem is with parametrized anonymous classes, e.g. when using TypeToken from Guava, this does NOT work:

public List<SomeClass> list() {
    return getData(new TypeToken<>() { });
}

But this DOES work:

public List<SomeClass> list() {
    return getData(new TypeToken<List<SomeClass>>() { });
}

I tried that in versions 11.0.3 - 11.0.7 versions and all contains the bug.

Solution 4 - Java

Java 11.0.7 has the same problem.

This changed from:

new ParameterizedTypeReference<>() {
})

to this:

new ParameterizedTypeReference<HashMap<String, MyClass>>() {
})

Solution 5 - Java

I've fixed this problem on my machine by upgrading to AdoptOpenJDK 11.0.8.

https://adoptopenjdk.net/installation.html#x64_mac-jdk

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMikhail KholodkovView Question on Stackoverflow
Solution 1 - JavaMikhail KholodkovView Answer on Stackoverflow
Solution 2 - JavaOtmane el alami el hassaniView Answer on Stackoverflow
Solution 3 - JavaMiriView Answer on Stackoverflow
Solution 4 - JavaMartin LangvatnView Answer on Stackoverflow
Solution 5 - JavabogdancepView Answer on Stackoverflow