简体   繁体   中英

Is there a way to use Eclipse external annotations on a class' generic parameters?

I would like to annotate Vaadin's HasComponents class, which extends Iterable. I can annotate the iterator()-Method like this:

class com/vaadin/ui/HasComponents
iterator
 ()Ljava/util/Iterator<Lcom/vaadin/ui/Component;>;
 ()L1java/util/Iterator<L1com/vaadin/ui/Component;>;

so that I can iterate with a classic for-loop

for (Iterator<Component> it = content.iterator(); it.hasNext();) {
  Component c = it.next();
  doSmoething(c);
}

but when I try a foreach-loop like

for(Component c : content) {
  doSomething(c);
}

I get a warning from Eclipse:

Null type safety (type annotations): The expression of type 'Component' needs unchecked conversion to conform to '@NonNull Component'

presumably because it should be

HasComponents extends Component, Iterable<@NonNull Component> 

Is there any way to add this annotation via external annotations or is there another way?

如果您的最终目标是简化循环,并且您正在使用Java 8或更高版本,则可以通过Streams和Lambda通过以下方式将forEach(..)与HasComponents结合使用:

layout.forEach(component -> { ... do something with component ... });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM