Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sort fields for deterministic order #27

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.concurrent.atomic.AtomicReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -173,7 +175,13 @@ protected static MetaFieldInfo[] internalToMetaFieldInfoArray(Class type, Object
for (Class classType : hierarchy) {

// begin search for each field (variable)
for (Field f : classType.getDeclaredFields()) {
Field[] fields = classType.getDeclaredFields();
Arrays.sort(fields, new Comparator<Field>() {
public int compare(Field a, Field b) {
return a.getName().compareTo(b.getName());
}
});
for (Field f : fields) {

// is this field marked as a MetaField?
if (f.isAnnotationPresent(MetaField.class)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ public void toMetaFieldInfoArray() throws Exception {
Administrator emp = new Administrator();
MetaFieldInfo[] fields = MetaFieldUtil.toMetaFieldInfoArray(emp);
Assert.assertEquals(12, fields.length);
Assert.assertEquals("First Name", fields[0].name);
Assert.assertEquals("Last Name", fields[1].name);
Assert.assertEquals("email", fields[2].name);
Assert.assertEquals("ID", fields[3].name);
Assert.assertEquals("Active?", fields[4].name);
Assert.assertEquals("email", fields[0].name);
Assert.assertEquals("First Name", fields[1].name);
Assert.assertEquals("ID", fields[2].name);
Assert.assertEquals("Active?", fields[3].name);
Assert.assertEquals("Last Name", fields[4].name);
Assert.assertEquals("loginCounter", fields[5].name);
Assert.assertEquals("The first name of this user", fields[0].description);
Assert.assertEquals("", fields[1].description);
Assert.assertEquals("The first name of this user", fields[1].description);
Assert.assertEquals("", fields[0].description);
Assert.assertEquals("", fields[2].description);
// set some values
emp.setFirstName("John");
Expand All @@ -61,9 +61,9 @@ public void toMetaFieldInfoArray() throws Exception {
//for (MetaFieldInfo field : fields) {
// logger.debug("field name=" + field.name + ", value=" + field.value + ", description=" + field.description);
//}
Assert.assertEquals("John", fields[0].value);
Assert.assertEquals("Doe", fields[1].value);
Assert.assertEquals("EMPTY", fields[2].value);
Assert.assertEquals("EMPTY", fields[0].value);
Assert.assertEquals("John", fields[1].value);
Assert.assertEquals("Doe", fields[4].value);
}


Expand Down