I need to implement getPublicFields method to list names of public fields declared in class the object belongs to. Fields inherited from parent classes should be omitted. Field names should be sorted in lexical order.
What I am doing wrong?
/** Get sorted list of public fields the object declares (inherited fields should be skipped). */ class FieldGetter {
public String[] getPublicFields(Object object) {
Field[] fieldsObject = object.getClass().getDeclaredFields();
ArrayList<String> fieldsString = new ArrayList<>();
for (Field fields : fieldsObject) {
if (Modifier.isPublic(fields.getModifiers())){
fieldsString.add(fields.toString());
}
}
String[]result = fieldsString.toArray(new String[fieldsString.size()]);
Arrays.sort(result);
return result;
}
}
Aucun commentaire:
Enregistrer un commentaire