We're happy to announce that we have implemented @Yield annotation both in javac and in eclipse compilers.
@Yield
This way you get built-in IDE support for the feature!
To download yield annotation processor please use the following link: Yield.zip
It contains both yield annotation processor, and a test project.
If you do not want to compile the sources, you can download Yield.jar
We would like to reiterate on how @Yield annotation works:
Iterator<T>
Iterable<T>
ArrayList<T> items = new ArrayList<T>();
items.add(...);
return items;
return items.iterator();
YieldProcessor
The following is an example of such a method:
@Yield public static Iterable<Integer> generate(final int from, final int to) { ArrayList<Integer> items = new ArrayList<Integer>(); for(int i = from; i < to; ++i) { items.add(i); } return items; }
The use is like this:
for(int value: generate(7, 20)) { System.out.println("generator: " + value); }
Notice that method's implementation still will be correct in absence of YieldProcessor.
Other important feature is that the state machine returned after the yield processor is closeable.
This means that if you're breaking the iteration before the end is reached you can release resources acquired during the iteration.
Consider the example where break exits iteration:
@Yield public static Iterable<String> resourceIteration() { ArrayList<String> items = new ArrayList<String>(); acquire(); try { for(int i = 0; i < 100; ++i) { items.add(String.valueOf(i)); } } finally { release(); } return items; }
and the use
int i = 0; Iterable<String> iterator = resourceIteration(); try { for(String item: iterator) { System.out.println("item " + i + ":" + item); if (i++ > 30) { break; } } } finally { close(iterator); } ... private static <T> void close(T value) throws IOException { if (value instanceof Closeable) { Closeable closeable = (Closeable)value; closeable.close(); } }
Close will execute all required finally blocks. This way resources will be released.
To configure yield processor a developer needs to refer Yield.jar in build path, as it contains @Yield annotation. For javac it's enough, as compiler will find annotation processor automatically.
Eclipse users need to open project properties and:
At the end we want to point that @Yield annotation is a syntactic suggar, but it's important the way the foreach statement is important, as it helps to write concise and an error free code.
See also Yield feature in java implemented! Yield feature in java
Remember Me
a@href@title, b, blockquote@cite, em, i, strike, strong, sub, super, u