【发布时间】:2014-11-30 00:03:41
【问题描述】:
根据documentation of SafeVarargs,@SafeVarargs 注释只能应用于static 或final 的构造函数或变量arity 方法。这是I have read,用于消除注释继承问题;也就是说,只有在方法不能被覆盖的情况下才允许对方法进行注释。显然,构造函数、static 方法和final 方法不能被覆盖。但是,private 方法或 final class 中的方法也不能。 Someone has complained 关于无法指定 @SafeVarargs private 方法,但 neither of these issues has been addressed。一般来说,no one really seems to care。我错过了什么吗?我在抱怨没有任何实际应用的东西吗?还是……?
【问题讨论】:
-
嗯,如果在方法中使用可变参数是安全的,我倾向于使用 SuppressWarnings 注释来使编译器停止抱怨。但我不确定为什么不能在有效的最终方法上使用此注释。
-
@jjm 如果我没记错的话,在 varargs 方法上使用
@SuppressWarnings("unchecked")会抑制该位置的警告,但在使用该方法的地方仍然会出现警告。因此,@SafeVarargs的存在理由。
标签: java annotations overriding variadic-functions type-erasure