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

8

75

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?

Nakasuji answered 19/2, 2019 at 21:33 Comment(6)
An exception from the compiler is a bug. Write up a test case and submit the bug.Pothole
I confirm this bug as wellLeucoma
I am not able to build the project while my other team mates are able to build it . I tried to delete my repo . no luck .Hedgepeth
This is a bug in any version older than 11.0.8. Try upgrading your JDK to 11.0.8 or newerLanda
JDK 11.0.14 works, haven't tried earlier versions.Tobytobye
JDK 11.0.15 also worked for meCorruption
N
66

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.

Nakasuji answered 19/2, 2019 at 23:38 Comment(7)
Does "fixed in version 12" mean that I need to install JDK 12 to get the fix? Will JDK 11 remain broken?Bayonne
@TroyDaniels yes fix is included and scheduled to be released as part of JDK 12. openjdk.java.net/projects/jdk/12Nakasuji
what about JDK11 which "supposed" to be LTE?Leucoma
@MirceaStanciu fix has been backported and will be available in JDK 11.0.4. Updated the answer.Nakasuji
java 11.0.6 has the same problemMaureenmaureene
Some consusion about versions: in openjdk it is fixed 11.0.4 onwards, oracle jdk 11.0.8. See original bug report link.Swell
thanks this post saved me,was unable to figure out wats wrong,literally no stack trace pointing to this changeSami
H
10

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.

Heartrending answered 9/7, 2020 at 11:54 Comment(2)
OpenJDK 11.0.8 has the bug tooDariusdarjeeling
I did with 11.0.12 and it works.Piteous
D
9

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() { 
});
Dunkle answered 25/6, 2019 at 8:37 Comment(0)
U
5

Java 11.0.7 has the same problem.

This changed from:

new ParameterizedTypeReference<>() {
})

to this:

new ParameterizedTypeReference<HashMap<String, MyClass>>() {
})
Untinged answered 30/6, 2020 at 8:2 Comment(0)
K
2

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

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

Kopple answered 10/8, 2020 at 12:30 Comment(0)
S
0

Most of the times, this error doesn't tell you the originating class so it's very hard to intentionally reproduce the problem. After trying a lot, I finally updated project JDK to 11.0.16 (from 11.0.7) and it worked!

Sudduth answered 14/10, 2022 at 9:41 Comment(0)
B
0

In case anyone else faces this issue: we had to change the java compiler from javac to eclipse (anything else that's not javac) to get our code to compile. Upgrading/downgrading jdk didn't fix the issue for us.

settings on intellij idea

Bills answered 13/6, 2023 at 21:43 Comment(0)
A
0

In my case I intalled latest java 11 from oracle (Java SE Development Kit 11.0.19) and it solved problem. You can download latest version here : https://www.oracle.com/pl/java/technologies/javase/jdk11-archive-downloads.html

Ayers answered 21/9, 2023 at 9:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.