Annotation questions + class member order

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 it TagValidator.autoGenerate()uses markup analysis + annotations to get all the members of my Foobar class that have been tagged @Tagand do some rude painstaking boring things that I will need to do for several classes.

My question is: will these members be seen in the order in which they are declared? I agree that 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 feel lazy :-)

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

+3
source share
3 answers

To quote JavaDocs on Class.getDeclaredFields()both andClass.getFields()

.

, , .

, , . .

+2

, TagValidator Class.getFields, javadoc :

, , , . .

. , , , , .

+1

I am sure that they maintain the declaration procedure. I say this because I saw that other projects use the same technique: http://preon.flotsam.nl/ Preon associates java fields with binary layouts, and this depends on the order in which the field is declared.

0
source

Source: https://habr.com/ru/post/1713235/


All Articles