I have this setup
public class ClassA {
//I cannot modify or access this class
private List<Object> list = new ArrayList<>();
}
public class ClassB {
public void entry(String... args) {
ClassA instance = ClassRegistry.find(ClassA.class);
//add using reflection
//instance.list.add(new Object());
}
}
how do I add an object to that list using reflection?
I tried
Field list = instance.getClass().getDeclaredField("list");
list.setAccessible(true);
ArrayList<Object> actualList = (ArrayList<Object>) list.get(instance);
actualList.add(new Object());
list.set(instance, actualList);
but it didn't work. the list was still empty.
Aucun commentaire:
Enregistrer un commentaire