On 03/27/2013 03:00 PM, Peter Levart wrote:
On 03/27/2013 02:00 PM, Joel Borggrén-Franck wrote:
Hi Peter,

On 03/27/2013 01:31 PM, Peter Levart wrote:

I also don't know
whether having LinkedHashMap instead of plain HashMap is necessary, since it
is initialized with defaults from plain HashMap (which is hashCode %
capacity ordered) and only some of defaults are overridden in parsing loop
in general. For example, having this annotation:

@interface Ann {
     String one();
     String two() default "2";
}

and usage:

@Ann(one = "1")

...toString will print: @Ann(two = "2", one = "1")


While there perhaps is no natural ordering between the sets of default
elements and non-default elements you probably want an order following
source as closely as possible among the non-default elements.

What is the order of elements in java class file (the bytes that the parser
interprets)? Is it taken from the declaration of annotation (the order of
methods in annotation interface) or from the use-site of the annotation? Is
this specified?


In general javac tries to keep source order on "things" when emitting to a class file. I believe this is often specified as natural order, but don't trust me on this. In this specific case I would guess element use order from the annotatio instance.

In the following small example I note that 'b' comes before 'a' in the const pool but the RuntimeInvisibleAnnotations 'a' is before 'b'. I don't think this is a coincidence.

anno-order$ cat T.java
@T(a="XYZ", b="DEF")
public @interface T {
    String b();
    String a();
}

anno-order$ javap -v T.class
Classfile /localhome/src/test/anno-order/T.class
  Last modified Mar 27, 2013; size 239 bytes
  MD5 checksum e1c7d8d76842d219c9853463af37830a
  Compiled from "T.java"
public interface T extends java.lang.annotation.Annotation
  SourceFile: "T.java"
  RuntimeInvisibleAnnotations:
    0: #10(#6=s#11,#4=s#12)
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_INTERFACE, ACC_ABSTRACT, ACC_ANNOTATION
Constant pool:
   #1 = Class              #13            //  T
   #2 = Class              #14            //  java/lang/Object
   #3 = Class              #15            //  java/lang/annotation/Annotation
   #4 = Utf8               b
   #5 = Utf8               ()Ljava/lang/String;
   #6 = Utf8               a
   #7 = Utf8               SourceFile
   #8 = Utf8               T.java
   #9 = Utf8               RuntimeInvisibleAnnotations
  #10 = Utf8               LT;
  #11 = Utf8               XYZ
  #12 = Utf8               DEF
  #13 = Utf8               T
  #14 = Utf8               java/lang/Object
  #15 = Utf8               java/lang/annotation/Annotation

For more examples of order discussions see the recent discussion on enhanced-metadata-spec-disc...@openjdk.java.net on annotation ordering in reflection and javax.lang.model.

cheers
/Joel

Reply via email to