I have a bit of Java code (using the Guava ImmutableList
class):
我有一些Java代码(使用Guava ImmutableList类):
@Nonnull
public static <E extends Event> UserHistory<E> forUser(long id, E... events) {
List<E> list = ImmutableList.copyOf(events);
return new BasicUserHistory<E>(id, list);
}
I am getting the usual heap pollution warnings that come with a method like this. Since my method is not doing any modifications of events
, it cannot introduce a heap pollution. However, if (because of erasure) a client of this method calls it with a bad events
array, it seems that it can propagate a heap polution through itself.
我得到了通常的堆污染警告,这种警告来自这样的方法。由于我的方法没有对事件进行任何修改,因此不会引入堆污染。但是,如果(因为擦除)此方法的客户端使用错误的事件数组调用它,它似乎可以通过自身传播堆解决方案。
If I annotate it with @SafeVarargs
, I still get a warning in it (suppressable with @SuppressWarnings("varargs")
). But reading the Java documentation on heap pollution, I am a little unclear as to the correct set of annotations on this method.
如果我用@SafeVarargs注释它,我仍然会收到警告(可以使用@SuppressWarnings(“varargs”)抑制)。但是阅读关于堆污染的Java文档,我对这个方法的正确注释集有点不清楚。
I also note that ImmutableList.copyOf
is not marked as @SafeVarargs
(though this could just be a compatibility issue), but Arrays.asList
is.
我还注意到ImmutableList.copyOf没有标记为@SafeVarargs(虽然这可能只是兼容性问题),但是Arrays.asList是。
So, my question: is @SafeVarargs
an appropriate annotation for this method, given that it will not encounter a ClassCastException
, but might propagate an improperly-checked array through to the final parameterized type and allow a ClastCastException
in client code?
所以,我的问题是:@SafeVarargs是这个方法的适当注释,因为它不会遇到ClassCastException,但可能会将一个不正确检查的数组传播到最终的参数化类型,并在客户端代码中允许ClastCastException?
I believe, based on this answer, that it is safe, since the code does not do anything that depends on the type of events
itself, only on the type of its elements. Is that a correct application of the guidance?
我相信,基于这个答案,它是安全的,因为代码不会做任何取决于事件本身类型的事情,只取决于其元素的类型。这是指导的正确应用吗?
1 个解决方案
#1
Yes, @SafeVarargs
should be appropriate, because the only thing that is done with events
is to pass it to ImmutableList.copyOf()
, which (in my understanding of that method) does not depend on the runtime type of that array.
是的,@ SafeVarargs应该是合适的,因为事件所做的唯一事情是将它传递给ImmutableList.copyOf(),它(在我对该方法的理解中)不依赖于该数组的运行时类型。
ImmutableList.copyOf()
should be annotated with @SafeVarargs
, but it isn't (maybe for backwards compatibility or they haven't noticed it). When your non-reifiable varargs method passes the varargs parameter to another method that may potentially depend on the runtime type of the array, then (for reasons I don't fully understand, but is the subject of this question) it gives you a varargs warning for that call. This can be suppressed with @SuppressWarnings("varargs")
.
ImmutableList.copyOf()应该用@SafeVarargs注释,但它不是(可能是为了向后兼容,或者他们没有注意到它)。当你的不可恢复的varargs方法将varargs参数传递给另一个可能依赖于数组的运行时类型的方法时,那么(由于我不完全理解的原因,但这是这个问题的主题)它给你一个varargs警告该电话。这可以通过@SuppressWarnings(“varargs”)来抑制。
#1
Yes, @SafeVarargs
should be appropriate, because the only thing that is done with events
is to pass it to ImmutableList.copyOf()
, which (in my understanding of that method) does not depend on the runtime type of that array.
是的,@ SafeVarargs应该是合适的,因为事件所做的唯一事情是将它传递给ImmutableList.copyOf(),它(在我对该方法的理解中)不依赖于该数组的运行时类型。
ImmutableList.copyOf()
should be annotated with @SafeVarargs
, but it isn't (maybe for backwards compatibility or they haven't noticed it). When your non-reifiable varargs method passes the varargs parameter to another method that may potentially depend on the runtime type of the array, then (for reasons I don't fully understand, but is the subject of this question) it gives you a varargs warning for that call. This can be suppressed with @SuppressWarnings("varargs")
.
ImmutableList.copyOf()应该用@SafeVarargs注释,但它不是(可能是为了向后兼容,或者他们没有注意到它)。当你的不可恢复的varargs方法将varargs参数传递给另一个可能依赖于数组的运行时类型的方法时,那么(由于我不完全理解的原因,但这是这个问题的主题)它给你一个varargs警告该电话。这可以通过@SuppressWarnings(“varargs”)来抑制。