mercredi 2 décembre 2020

Get final non null fields by reflection

class MyObject {
  private final String name;
  private final String uuid;
}

how to get all fields from given objects if they are not null?
This code fails on f.get("") with

cannot set final String field name...

when the field is null

Arrays.stream(object.getClass().getDeclaredFields())
                .forEach(f -> {
                    try {
                        f.setAccessible(true);
                        system.out.println(f.getName() + ":" + f.get(""));
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                });




Aucun commentaire:

Enregistrer un commentaire