vendredi 26 décembre 2014

Cannot access field to change its modifier using java reflection

What im trying to do is:



Field f = /*some field from some class. the field modifier is "protected"*/
f.setAccessible(true);


But it says I cant access a field with "protected" modifier... So if I cant access it, how will I change the modifier?


Thanks!


EDIT:



((Field) getAllFields(nestedClass).get(1).get(nestedObj)).setAccessible(true); /*Exception is thrown because of this line*/
private ArrayList<Field> getAllFields(Class<?> c) {
ArrayList<Field> fields = new ArrayList<>();
do {
fields.addAll(Arrays.asList(c.getDeclaredFields()));
c = c.getSuperclass();
} while (c != null);
return fields;
}





Aucun commentaire:

Enregistrer un commentaire