[JSR308] Enhanced for loops

Artemus Harper subanark at gmail.com
Mon Aug 4 23:44:11 EDT 2008

Short description:
1. My annotations are morphed based on use
2. I don't annotate the type parameters directly.

Long description:
In my case, annotations may look differently from the caller than how they
are declared and used by the callee.

In my test case I have:

        //Create a list of unique elements
        @InnerUnique({"T"}) UniqueList<String> list = new
        @Unique String s1 = new String("s1"); //cannot get references to
string literals
        list.add( (@Unique String) s1); //give up control of the reference
to the list
        list.add(new String("s2")); //no cast needed here

        @InnerUnique({"T"}) UniqueList<String> list2 = new
        @InnerUnique({"T"}) Iterable<String> iterator = list.drainToItr();
        for(@Unique String s : iterator) //get an iterator that removes
elements from the list
            list2.add((@Unique String) s); //add the unique reference to
another list

The UniqueList.add method looks like (where items is an
public void add(@Unique({"T"}) T item) @NoEscape
        ensureCapasity(size + 1);
        //We need to cast here to give ownership to the array. After this
        //item is no longer referencable in the normal manner.
        items.set(size++, (@Unique({"T"}) T) item);

What @Unique({"T"}) means is that item is unique if this UniqueList instance
is declared as InnerUnique({"T"}). The callee doesn't know if its elements
are unique, only the caller knows.

More information about the JSR308 mailing list