// Sets fields to be a sorted array of the serializable fields of
// clazz.
private void setFields(Class cl)
{
SetAccessibleAction setAccessible = new SetAccessibleAction();
if (!isSerializable() || isExternalizable() || isEnum())
{
fields = NO_FIELDS;
return;
}
try
{
final Field f =
cl.getDeclaredField("serialPersistentFields");
setAccessible.setMember(f);
AccessController.doPrivileged(setAccessible);
int modifiers = f.getModifiers();
if (Modifier.isStatic(modifiers)
&& Modifier.isFinal(modifiers)
&& Modifier.isPrivate(modifiers))
{
fields = getSerialPersistentFields(cl);
if (fields != null)
{
ObjectStreamField[] fieldsName = new ObjectStreamField[fields.length];
System.arraycopy(fields, 0, fieldsName, 0, fields.length);
Arrays.sort (fieldsName, new Comparator() {
public int compare(Object o1, Object o2)
{
ObjectStreamField f1 = (ObjectStreamField)o1;
ObjectStreamField f2 = (ObjectStreamField)o2;
return f1.getName().compareTo(f2.getName());
}
});
for (int i=1; i < fields.length; i++)
{
if (fieldsName[i-1].getName().equals(fieldsName[i].getName()))
{
fields = INVALID_FIELDS;
return;
}
}
Arrays.sort (fields);
// Retrieve field reference.
for (int i=0; i < fields.length; i++)
{
try
{
fields[i].lookupField(cl);
}
catch (NoSuchFieldException _)
{
fields[i].setToSet(false);
}
}
calculateOffsets();
return;
}
}
}
catch (NoSuchFieldException ignore)
{
}
catch (IllegalAccessException ignore)
{
}
int num_good_fields = 0;
Field[] all_fields = cl.getDeclaredFields();
int modifiers;
// set non-serializable fields to null in all_fields
for (int i = 0; i < all_fields.length; i++)
{
modifiers = all_fields[i].getModifiers();
if (Modifier.isTransient(modifiers)
|| Modifier.isStatic(modifiers))
all_fields[i] = null;
else
num_good_fields++;
}
// make a copy of serializable (non-null) fields
fields = new ObjectStreamField[ num_good_fields ];
for (int from = 0, to = 0; from < all_fields.length; from++)
if (all_fields[from] != null)
{
final Field f = all_fields[from];
setAccessible.setMember(f);
AccessController.doPrivileged(setAccessible);
fields[to] = new ObjectStreamField(all_fields[from]);
to++;
}