Dealing with modifiers List public fields
Implement the getPublicFields method that returns String array with names of public fields declared in the class object belongs to. Fields inherited from parent classes should be omitted.
/**
* Get sorted list of public fields the object declares (inherited fields
* should be skipped).
*/
class FieldGetter {
public String[] getPublicFields(Object object) {
java.util.List<String> rs = new java.util.ArrayList<>();
for (Field field : object.getClass().getFields()) {
if (field.getDeclaringClass().equals(object.getClass())) {
rs.add(field.getName());
}
}
java.util.Collections.sort(rs);
return rs.toArray(new String[0]);
}
}

浙公网安备 33010602011771号