我想注释 Vaadin 的 HasComponents 类,它扩展了 Iterable。我可以像这样注释 iterator() 方法:
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 ... });
关于java - 有没有办法在类的通用参数上使用 Eclipse 外部注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53066791/