![](/img/trans.png)
[英]How to properly annotate methods with generic collections using Eclipse external Annotations
[英]Is there a way to use Eclipse external annotations on a class' generic parameters?
我想注釋Vaadin的HasComponents類,該類擴展了Iterable。 我可以這樣標注iterator()-Method:
class com/vaadin/ui/HasComponents
iterator
()Ljava/util/Iterator<Lcom/vaadin/ui/Component;>;
()L1java/util/Iterator<L1com/vaadin/ui/Component;>;
這樣我就可以使用經典的for循環進行迭代
for (Iterator<Component> it = content.iterator(); it.hasNext();) {
Component c = it.next();
doSmoething(c);
}
但是當我嘗試像這樣的foreach循環時
for(Component c : content) {
doSomething(c);
}
我從Eclipse得到警告:
Null type safety (type annotations): The expression of type 'Component' needs unchecked conversion to conform to '@NonNull Component'
大概是因為應該
HasComponents extends Component, Iterable<@NonNull Component>
有什么方法可以通過外部注釋添加此注釋,或者還有另一種方法?
如果您的最終目標是簡化循環,並且您正在使用Java 8或更高版本,則可以通過Streams和Lambda通過以下方式將forEach(..)與HasComponents結合使用:
layout.forEach(component -> { ... do something with component ... });
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.