views:

70

answers:

3

I would like to do something like this:

public class Foobar {
   @Tag final private int foo;
   @Tag final private int bar;
   @Tag final private int baz;
   @Tag final private int quux;

   static private final TagValidator validator = 
      TagValidator.autoGenerate(Foobar.class);

   public Foobar(Something something)
   {
       validator.validate(something);
       this.foo = something.method1();
       this.bar = something.anotherMethod();
       this.baz = something.someOtherMethod();
       this.quux = something.yetAnotherMethod();
   }
   ... other methods ...
}

where TagValidator.autoGenerate() uses reflection + annotation parsing to get all the members of my Foobar class that have been tagged with @Tag and do some grungy laborious boring stuff which I'm going to need to do for several classes.

My question is, is it going to see these members in the order they are declared? I agree it would be safer to do this:

public class Foobar {
   @Tag(0) final private int foo;
   @Tag(1) final private int bar;
   @Tag(2) final private int baz;
   @Tag(3) final private int quux;

   ...
}

but I'm feeling lazy :-)

Also, is it possible to have a class and an annotation with the same name?

A: 

I'm pretty sure they do retain the order of declaration. I'm saying that because I have seen other projects use the exact same technique: http://preon.flotsam.nl/ Preon binds java fields to binary layouts, and it depends on order of field declaration.

Trevor Harrison
There are implementations that come out with a different order. Just because other people do something, that doesn't stop it being a mistake.
Tom Hawtin - tackline
+1 for the Preon link -- that's the sort of thing I'm doing so maybe I can make use of it.
Jason S
"Just because other people do something..."Mom, is that you? :) ("if your friends jump off a bridge...")
Trevor Harrison
well, I suppose the Preon folks should put a big warning that it depends on JRE behavior that is unspecified.
Jason S
As "the Preon folks" I have to say that I *am* aware of the fact that Preon relies on some properties not guaranteed by the language specifciation. At the other hand, it's a piece of logic that is totally isolated, and I yet have to come across a VM that messes up the order. (Tried Rockit and Sun. IBM's implementation is essentially the same as Sun's implementation.)
Wilfred Springer
+1  A: 

Assuming your TagValidator uses Class.getFields, then the javadoc says:

Returns an array containing Field objects reflecting all the accessible public fields of the class or interface represented by this Class object. The elements in the array returned are not sorted and are not in any particular order.

So I wouldn't rely on it. In practice, though, this does always seem to come back in declaration order.

skaffman
+2  A: 

To quote the JavaDocs on both Class.getDeclaredFields() and Class.getFields()

The elements in the array returned are not sorted and are not in any particular order.

From a maintainability perspective if the order is important include it in your annotation or better still don't rely on the order.

Yes, it is possible to for a class and annotation to have the same name providing they are in a different package. Basically the same rules apply as classes and interfaces.

Nick Holt