[JSR308] Readonly states of an object

Artemus Harper subanark at gmail.com
Fri Aug 3 00:01:09 EDT 2007


I am somewhat confused on how to declare references with the 4 possible
states a reference can be at with respect to the ReadOnly attribute. Those
being:

1. The object is not readonly. It can be changed. The syntax seems to merely
be the absence of the @Readonly attribute:
int[] values;

2. The object is readonly. It cannot and will not be changed. Syntax seems
to be the inclusion of the @Readonly attribute:
@ReadOnly int[] values;

3. The object may be readonly or maybe be changeable. We won't change it,
but it might change by an external source. Not sure what the syntax would be
here. You could see this in a sum method:
public static void sum(@?ReadOnly int[] values)

4. The object is not initialized yet, it might be readonly or not in the
future. This would be the state of the object in its constructor. Does this
mean there will have to be different constructors for a readonly and a
non-readonly object? Can the constructor call helper methods to modify the
object? What if the helper method modifies the object after it has been
called? Can readonly objects only be changed in the constructor? What would
be the syntax to create a @Readonly int[] that has the values 1 to 100?

In order to address the problems of #4 you would need to have a rule stating
that an object can only change its annotations while the current scope
controls the reference (In other words, the only references to the object
are on the stack of the current thread).

Without addressing these concerns you will not have a safe readonly object.

E.g. Here is a simple example of the creation of a customized readonly
int[]:
public static void main(String[] args)
{
   @Internal int[] values = createValues(5); //get an internal array. This
reference is the only one.
   sort(values); //sort the array. After this method exits we still own the
sole reference to values.
   @ReadOnly int[] values2 = values; //lock down the array preventing
further changes.
   storeNamedArray("MyArray",values2); //values2 is readonly now, we can
call this method.
   int len = values.length; //Error, values is no longer a safe reference.
}

//Creates an int[] that the caller will have exclusive access to
public static @Internal int[] createValues(int size)
{
   @Internal int[] values = new int[size];
   for(int i = 0; i < values.length;i++)
   {
      values[i] = i;
   }
   return values;
}

//We get an int[] that could be internal. The values parameter will not
escape the scope of this method.
public static void sort(@?Internal int[] values)
{
   //do sorting of the array
}

//Store an int array in a table with a name that can be retrieved later.
Since we add it to a table the array will no longer be internal.
public static void storeNamedArray(@NotNull String name, @ReadOnly int[]
array)
{
   //store array in cache.
}
-- 
Artemus Harper
-------------- next part --------------
An HTML attachment was scrubbed...
URL: https://lists.csail.mit.edu/pipermail/jsr308/attachments/20070802/9b3677e4/attachment.html


More information about the JSR308 mailing list