Preserving the order of annotations

Posted by Ragunath Jawahar on Stack Overflow See other posts from Stack Overflow or by Ragunath Jawahar
Published on 2012-10-22T10:06:43Z Indexed on 2012/10/22 11:00 UTC
Read the original article Hit count: 150

Filed under:
|
|

When obtaining the list of fields using getFields() and getDeclaredFields(), the order of the fields in a Java class is undefined.

I need this order in my validation library. Since the order is not preserved (though some claim that the order is preserved in JDK 6 and above but is not guaranteed across VMs). I cannot speculate on this because the order of annotations across fields is absolutely essential for the library.

One way to get around this is to have an order or an index attribute in my Annotation.

What worries me is that the code could become a bit cumbersome for maintaining in the following case.

  1. If the use wants to insert a new annotated field then, he might have to renumber all the other annotations in the class.
  2. I could have the order or index as a floating point number - float or double but , it wouldn't look good to have order such as 1, 1.5, 2, etc.,

What would be an elegant solution for this problem?

Here is a example code so that you can get an idea about the problem:

@Required
@TextRule (minLength = 6, message = "You need at least 6 characters.")
private EditText usernameEditText;

@Password
private EditText passwordEditText;

@ConfirmPassword
private EditText confirmPasswordEditText;

@Email
private EditText emailEditText;

© Stack Overflow or respective owner

Related posts about java

Related posts about android