Extending Java with Yield

The Yielder library by infomancers (a.k.a. Aviad Ben Dov of the Chaotic Java weblog) is a library providing Java with Ruby/Python/C#-like yield-ing for iterators.

It works by using Java 1.5's facility for hooking user-defined class inspectors/transformers into the classloader, and rewriting the bytecode of a yieldNextCore() method containing calls to yieldReturn(foo) so that its local variables are promoted to members of an anonymous inner class, which also maintains the state of the iterator/generator.

Examples, rationale and implementation are discussed in detail at Chaotic Java. There is also a collections library built on top of Yielder, with tree traversal iterators and map/filter-like functionality.

From a PL perspective, this ability to hook into the classloader and do bytecode manipulation on plain java classes provides an interesting mechanism for prototyping new language features in libraries. It could equally well support an aspect framework (via annotations), and perhaps even something like Terracotta's distributed heap.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Shouldn't Java be able to modify itself?

Isn't self modification the feature that opens the door for all other features? Java could easily provide hooks to allow user code to modify the bytecode (coupled with the appropriate security model, of course).