java warning: Varargs method could cause heap pollution from non-reifiable varargs parameter

前端 未结 5 1677
灰色年华
灰色年华 2021-01-17 17:32

I am using IntelliJ IDEA with javac on JDK 1.8. I have the following code:

class Test
{
    @SafeVarargs
    final void varargsMe         


        
5条回答
  •  萌比男神i
    2021-01-17 18:17

    Assuming that I know what I am doing

    I refuse to assume this since this seems incredibly wrong.

    The situation you're running into here is that, because generics are not reifiable, you're declaring a generic array, which is generally frowned upon. Generics and arrays don't mix very well, given that an array is covariant (String[] is an Object[] in the same way that a String is an Object), whereas generics are invariant (List is not a List, even though a String is an Object).

    If you want a collection of collections...just pass one. It's safer than mingling arrays and generics.

    final void varargsMethod(Collection<> collections) { }
    

    提交回复
    热议问题