vendredi 29 juillet 2022

Create a hashmap with the names of the fields in the class as the key, and the values of those fields as the values of the map

Let's say you have a class: SomeClass with fields a=10, b=20, and c=30, where instanceOfSomeClass.getA()==10. I want a map like this

{
    a:10
    b:20
    c:30
}

I tried this, but got Class can not access a member of class with modifiers "private static final", but I also can't modify this class to not have private static final:

      Field[] fields = SomeClass.class.getDeclaredFields();
      for (Field f : fields) {
        map.put(f.toString(), f.get(instanceOfSomeClass).toString());
      }

Any ideas on how to make this hashmap?





Aucun commentaire:

Enregistrer un commentaire