Generic annoyances in java

For some reason, the type variable <O> cannot flow through from an external caller through a callee to another called method.

This compiles just fine in eclipse, but once again sun’s javac is temperamental.

The three solutions seem to be:

  1. pass an object of type <O> to the subcallee method (m0(), p0() )
  2. use wildcard instead of <O> – which is o.k. if O is not needed in the body of the method
  3. assign to a variable with no generic information (universal backup solution to java generic wierdness)

I filed a bug with Sun.

(Technical details: javac 1.6.0_17 )

public class Foo {
    public > I m(I i) {
/*line3*/        R r = (R) p(i);
        return i;
    }
    public > R p(I i) {
        return null;
    }

    public > I m0(O o) {
        K r= (K) p0(o);
        return o;
    }
    public > K p0(O o) {
        return null;
    }

    public > I m1(I i) {
        K r= (K) p1(i);
        return i;
    }
    public > K p1(I i) {
        return null;
    }

    public > I m2(I i) {
/*line 27*/        K r= (K) p2(i);
        return i;
    }
    public > K p2(I i) {
        return null;
    }
    interface FooInterface {

    }
}

Foo.java:3: incompatible types; inferred type argument(s) I,java.lang.Object do not conform to bounds of type variable(s) O,R
found : R
required: java.lang.Object
/*line3*/ R r = (R) p(i);
^
Foo.java:27: incompatible types; inferred type argument(s) I,java.lang.Object do not conform to bounds of type variable(s) O,K
found : K
required: java.lang.Object
K r= (K) p2(i);
^

This entry was posted in help notes, technical. Bookmark the permalink.

1 Response to Generic annoyances in java

  1. Pingback: Woot! I crashed the javac (and javadoc as well!) « Just wondering….

Leave a Reply

Your email address will not be published. Required fields are marked *