Generic types java : Type Safety warning

104 Views Asked by At

I'm getting a type safety warning when I want to use the class of a generic class in a generic class, the code bellow will better explain :

class A<U, V> {

    private class B<W, X> {
        X x;
        W w;
    }

    public A() {
        B<U, V> c = C.someMethod(B.class); // warning here
    }

    static class C {
        public static <T> T someMethod(Class<T> clazz) {
            return null;
        }
    }

}

Any idea to solve this ?

Please note that B class is private.

1

There are 1 best solutions below

1
On

Your work is not correct when set a type that is not important for compiler in this line:

B<U, V> c = C.someMethod(B.class);

To compiler, just B is a sign of panic. so you should write.

B<?, ?> c = C.someMethod(B.class);