Invalid output using getGenericReturnType in Java 8

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

Call the following:

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

gives this erroneous result:

org.mockito.BDDMockito.org.mockito.BDDMockito$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 BDDMockito methods. I think the result should be:

org.mockito.BDDMockito$BDDMyOngoingStubbing<T>

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

+4
source share
1 answer

, . JDK-8054213 ( - JDK 9).

, . . getGenericReturnType . .

package parent;

public class Main {

    public static void main(String[] args) throws Exception {
        System.out.println(Main.class.getMethod("bar").getGenericReturnType());
        // prints "parent.Main.parent.Main$Bar<T>"
    }

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

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

}
+3

All Articles