aro_tech aro_tech - 10 months ago 52
Java Question

Incorrect output with getGenericReturnType on Java 8

I get a strange result for a specific reflection call using Java 8 (JDK 1.8.0_71) and Mockito (version 2.0.44-beta).

Calling the following:

BDDMockito.class.getMethod("given", Object.class).getGenericReturnType().getTypeName()

gives this erroneous result:$BDDMyOngoingStubbing<T>

This does not look right (the package does not exist), and I have not seen this problem with other Java classes or with other methods of BDDMockito. I think the result should be:


Is there an explanation for the result I got? Is this a known issue or should I report it to Oracle?


Yes, this is a bug. It has been reported in the ticket JDK-8054213 and is currently unresolved for all versions (including the current JDK 9 beta).

Note that you can reproduce it with a simpler example. The problem comes with the usage of an nested class as a method return type. getGenericReturnType incorrectly repeats the class name in the output. This is reproducible with a static nested class or an inner class.

package parent;

public class Main {

    public static void main(String[] args) throws Exception {
        // prints "parent.Main.parent.Main$Bar<T>"

    public <T> Bar<T> bar() {
        return null;

    /*static*/ class Bar<T> {}