SafeVarargs classfile encoding

Rémi Forax forax at univ-mlv.fr
Fri Jan 21 11:22:30 PST 2011


On 01/21/2011 07:50 PM, Joe Darcy wrote:
> Hi Rémi.
>
> A few comments, in the entirety of the "jdk" repository, there were a 
> total of five methods where it was appropriate to add this annotation, 
> which is not a very high density!  For Coin, to keep the features 
> small, we're avoiding JVM changes and defining a new attribute is a 
> JVM changes of sorts.

Here is the JVM spec change:

The SafeVarargs attribute is a fixed-length attribute in the 
|attributes| table of |||||method_info| (§4.6) 
<http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html#1513> 
structures. SafeVarargs is an attribute corresponding to the annotation 
SafeVarargs that the body of the annotated method or constructor does 
not perform potentially unsafe operations on its varargs parameter.

The SafeVarargs attribute has the following format:

    SafeVarargs|_attribute {
|     |	u2 attribute_name_index;
|     |	u4 attribute_length;
|    |}
|

The items of the SafeVarargs|| structure are as follows:

|attribute_name_index|

    The value of the |attribute_name_index| item must be a valid index
    into the |constant_pool| table. The |constant_pool| entry at that
    index must be a |CONSTANT_Utf8_info| (§4.4.7)
    <http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html#7963>
    structure representing the string |"|SafeVarargs|"|.
|attribute_length|
    The value of the |attribute_length| item is zero.


>
> The SafeVarargs information is needed in the class file in some 
> fashion to so that compilers can know to omit unchecked warnings that 
> would otherwise have to be emitted.  Additionally, having the 
> information in the class file enables tools like findbugs to do 
> additional checking on the operations of the SafeVarargs methods.

findbugs crawle the bytecode, so encoding SafeVarargs as an annotation 
or an attribute doesn't matter.

>
> Using an annotation to store this information avoids the need to add a 
> new core reflection API to access it, Method.isSafeVarargs, etc.

Why SafeVarargs need to be visible using reflection API ?
By example, Synthetic attribute is not visible using reflection API.

>
> -Joe

Rémi

>
> Rémi Forax wrote:
>> I wonder if encoding @SafeVarargs as a runtime annotation
>> in the classfile is a good idea. A runtime visible annotation is fat.
>>
>> In my opinion, @SafeVarargs should be an annotation with
>> retention SOURCE, translated by the compiler to an empty SafeVarargs 
>> method attribute.
>>
>> Is there a use case where this annotation is needed at runtime ?
>>
>> Rémi
>>
>




More information about the coin-dev mailing list