将泛型与雅加达公共集合缓冲区一起使用
这段代码在 Java <= 1.4 中编译得很好。 Java 1.6 抱怨并抱怨警告:
“方法 add(Object) 属于原始类型 Collection。对泛型类型 Collection 的引用应该参数化”
import org.apache.commons.collections.Buffer;
import org.apache.commons.collections.BufferUtils;
import org.apache.commons.collections.buffer.UnboundedFifoBuffer;
private Buffer connectqueue = BufferUtils.blockingBuffer(new UnboundedFifoBuffer());
...
connectqueue.add(new Conn(this, address, port));
如何调整代码以使该警告消失而不添加 @SupressWarnings 指令?
问题是 Jakarta Commons Collections Buffer 不是通用的,而是扩展了通用 java.util.Collection 接口。
This code compiles fine in Java <= 1.4. Java 1.6 bitches and moans with the warning:
"The method add(Object) belongs to the raw type Collection. References to generic type Collection should be parameterized"
import org.apache.commons.collections.Buffer;
import org.apache.commons.collections.BufferUtils;
import org.apache.commons.collections.buffer.UnboundedFifoBuffer;
private Buffer connectqueue = BufferUtils.blockingBuffer(new UnboundedFifoBuffer());
...
connectqueue.add(new Conn(this, address, port));
How do I tweak the code to make that warning go away without adding a @SupressWarnings directive ?
The problem is Jakarta Commons Collections Buffer is non-generic, but extends the generic java.util.Collection interface.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能。 在 Jakarta Commons 支持泛型之前(他们可能不会,因为他们也希望能够在较旧的 Java 版本上构建),您需要抑制(或接受)该警告。
作为替代方案,有一个支持泛型的 Commons Collections 分支,以及 Google 还有一个集合库。 不过,我还没有检查它们是否有缓冲区,这需要您切换 API。
如果您的代码没有使用 1.4 后的语言功能,您可以将编译器的语言级别设置为“1.4”,但这似乎更不可行(或不可取)。
可能只是坚持@SupressWarnings。
You cannot. Until Jakarta Commons supports generics (which they will probaby not, because they want to be able to build on older Java versions as well), you need to suppress (or live with) the warning.
As an alternative, there is a fork of Commons Collections that supports generics, and Google also has a Collections library. I have not checked if either of them has a Buffer, though, and it would require you to switch APIs.
If none of your code uses post-1.4-language features, you could set the compiler's language level to "1.4", but that seems even less feasible (or desirable).
Probably just stick with @SupressWarnings.
如上所述,您可以使用 Jakarta Collections 的 fork,它将为您提供一个使用泛型的缓冲区类,并且不会向您发出警告
http://collections.sourceforge.net/api/org/ apache/commons/collections/Buffer.html
As mentioned above you could use the fork of Jakarta collections which will give you a buffer class that uses generics and won't give you warnings
http://collections.sourceforge.net/api/org/apache/commons/collections/Buffer.html