Collections.addAll(allFields, nextClass.getDeclaredFields());
nextClass = nextClass.getSuperclass();
}
ArrayList<CachedField> publicFields = new ArrayList();
PriorityQueue<CachedField> cachedFields = new PriorityQueue(Math.max(1, allFields.size()), new Comparator<CachedField>() {
public int compare (CachedField o1, CachedField o2) {
// Fields are sorted by alpha so the order of the data is known.
return o1.field.getName().compareTo(o2.field.getName());
}
});
for (int i = 0, n = allFields.size(); i < n; i++) {
Field field = allFields.get(i);
int modifiers = field.getModifiers();
if (Modifier.isTransient(modifiers)) continue;
if (Modifier.isStatic(modifiers)) continue;
if (field.isSynthetic() && ignoreSyntheticFields) continue;
if (!field.isAccessible()) {
if (!setFieldsAsAccessible) continue;
try {
field.setAccessible(true);
} catch (AccessControlException ex) {
continue;
}
}
CachedField cachedField = new CachedField();
cachedField.field = field;
if (fieldsCanBeNull)
cachedField.canBeNull = !field.isAnnotationPresent(NotNull.class);
else
cachedField.canBeNull = false;
// Always use the same serializer for this field if the field's class is final.
Class fieldClass = field.getType();
if (isFinal(fieldClass)) cachedField.fieldClass = fieldClass;
cachedFields.add(cachedField);
if (Modifier.isPublic(modifiers) && Modifier.isPublic(fieldClass.getModifiers())) publicFields.add(cachedField);
}
if (!Util.isAndroid && Modifier.isPublic(type.getModifiers()) && !publicFields.isEmpty()) {
// Use ReflectASM for any public fields.
try {
access = FieldAccess.get(type);
for (int i = 0, n = publicFields.size(); i < n; i++) {
CachedField cachedField = publicFields.get(i);
cachedField.accessIndex = ((FieldAccess)access).getIndex(cachedField.field.getName());
}
} catch (AccessControlException ignored) {
}
}
int fieldCount = cachedFields.size();
fields = new CachedField[fieldCount];
for (int i = 0; i < fieldCount; i++)
fields[i] = cachedFields.poll();
}