Question

Recently I encounter a weird problem with respect to Java generics. I simplified the problem with snippet below :

public static void main(String[] args) {
        String s = "Hello";
        System.out.println(blindlyReturnGetObject());
    }

    private static <T> T getObject() {
        return (T) new Object();
    }

    private static <T> T blindlyReturnGetObject() {
        return getObject();
    }

In case of JDK 1.6.0_03 and earlier versions, we were getting infamous compilation error

type parameters of <T>T cannot be determined; no unique maximal instance exists for type variable T with upper bounds T,java.lang.Object

whereas this code works perfectly in JDK 1.6.0_26 and later versions.

Is there anyway to get rid of this issue for earlier version of jdk 1.6 since our build servers are still running in earlier version of jdk 1.6?

Was it helpful?

Solution

While googling related to this issue, I came across a bug raised in sun related to this generics type inference issue.

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6302954

So this is found to be fixed in later versions of jdk 1.6.0_20 and hence it was working in jdk 1.6.0_26

Thought its worth sharing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top