是否有可能解决“为可变参数参数创建T的通用数组”编译器警告? [英] Is it possible to solve the "A generic array of T is created for a varargs parameter" compiler warning?
问题描述
这是所讨论代码的简化版本,一个泛型类使用另一个具有泛型类型参数的类,并需要将一个泛型类型传递给具有可变参数参数的方法:
class Assembler< X,Y> {
void assemble(X container,Y ... args){...}
}
class Component< T> {
void useAssembler(T something){
汇编器< String,T> assembler = new Assembler< String,T>();
//产生警告:
//类型安全性:T的通用数组是
//为可变参数参数创建
assembler.assemble(hello ,某事);
}
}
有没有正确的方法可以将泛型参数传递给varargs方法而不会遇到此警告?
当然,像
assembler.assemble(hello,new T [] {something});
不起作用,因为您无法创建泛型数组。
这份错误报告有更多的信息,但归结为不喜欢数组类型的编译器。
This is a simplified version of the code in question, one generic class uses another class with generic type parameters and needs to pass one of the generic types to a method with varargs parameters:
class Assembler<X, Y> {
void assemble(X container, Y... args) { ... }
}
class Component<T> {
void useAssembler(T something) {
Assembler<String, T> assembler = new Assembler<String, T>();
//generates warning:
// Type safety : A generic array of T is
// created for a varargs parameter
assembler.assemble("hello", something);
}
}
Is there any correct way to pass along the generic parameter to a varargs method without encountering this warning?
Of course something like
assembler.assemble("hello", new T[] { something });
does not work since you cannot create generic arrays.
Other than adding @SuppressWarnings("unchecked")
, I don't think so.
This bug report has more information but it boils down to the compiler not liking arrays of generic types.
这篇关于是否有可能解决“为可变参数参数创建T的通用数组”编译器警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!