Why does @SafeVarargs not suppress the warning?

Here is the code:

class Foo<T> {
  private final T[] array;
  @SafeVarargs
  Foo(T... items) {
    this.array = items;
  }
}

I get:

[WARNING] Varargs method could cause heap pollution 
from non-reifiable varargs parameter items

What happened to my assignment? How to fix it? @SuppressWarningsis not an option, as I want this constructor to be truly "safe".

$ javac -version
javac 1.8.0_40
+4
source share
3 answers

My problem was that I used the -Xlintcommand line option javac. I found out that I need to compile it with the following parameters:

javac -Werror -Xlint -Xlint:-varargs

What is it.

+1
source

Joshua Bloch explains this in Effective Java (chapter 25):

. , , ( . 29 ). , , varargs ( 42) . , , varargs, varargs. , . , ( 24), varargs API.

spec, , ( ArrayBuilder )

, SafeVarargs SuppressWarnings

@SafeVarargs
@SuppressWarnings( "varargs" )

+1

docs:

arity (vararg) ,

, @SafeVarargs :

  • varargs

@SafeVarargs T... items. this.array = items;.


JLS ยง9.6.4.7:

@SafeVarargs , , arity.


, @SuppressWarnings("unchecked") , , .

+1

Source: https://habr.com/ru/post/1679805/


All Articles