@SuppressWarnings for final type parameter

前端 未结 1 1251
小蘑菇
小蘑菇 2021-01-20 19:16

In a place I have a method with a generic "VT extends String". Obviously this generates a warning: The type parameter VT should not be bounded by the final typ

相关标签:
1条回答
  • 2021-01-20 19:52

    Your code doesn't compile, but here's something similar, which I assume is what you want:

    class A<T>{
        T value;
        B<? super T> b;
        void method(){
            b.method(value);
        }
    }
    interface B<X>{
        <VT extends X> VT method(VT p);
    }
    // works fine
    class C implements B<Number>{
        public <VT extends Number> VT method(VT p){return p;}
    }
    // causes warning
    class D implements B<String>{
        public <VT extends String> VT method(VT p){return p;}
    }
    

    Seeing that you don't have a choice to saying extends String here, I'd say this is a bug in Eclipse. Furthermore, Eclipse can usually suggest an appropriate SuppressWarnings, but doesn't here. (Another bug?)

    What you can do is change the return and argument type to String and then suppress the (irrelevant) type safety warning it causes:

    // no warnings
    class D implements B<String>{
        @SuppressWarnings("unchecked")
        public String method(String p){return p;}
    }
    
    0 讨论(0)
提交回复
热议问题