Supposed I have a User list on Post entity
private List<User> users = new ArrayList<>();
and I can clear it using
post.getUsers().clear();
and can add to it with
post.getUsers().addAll(Something);
how can I do the same if use to call the function getUsers
dynamically? I tried
post.getClass().getMethod("getUsers").invoke(post).getClass().getMethod("clear").invoke(new ArrayList<>());
and also I tried
ArrayList.class.getMethod("clear").invoke(post);
but im getting a
WARN o.s.w.s.m.m.a.ExceptionHandlerExceptionResolver - Resolved [java.lang.IllegalArgumentException: object is not an instance of declaring class]
Any idea on how can I do it?
take note that this is working
post.getClass().getMethod("getUsers").invoke(post); //get the users
I just have no idea how can I chained the clear method or the addAll on it.