KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I need to invoke the setter methods of a class using reflection, and the code is as below: try { Method method = myObj.getClass().getMethod("set" + fieldName, new Class[] { value.getClass() }); method.invoke(myObj, value); } catch (Exception ex) { ex.printStackTrace(); } The value is an ArrayList and the setter method is as below: public void setNames(List<String> names){ this.names = names; } A java.lang.NoSuchMethodException is thrown when running this code, but when the setter method parameter type is changed to ArrayList from List it executes fine. Is there a way to keep the setter method parameter in super type and still use reflection without manually giving the type of the parameter when getting the method from the class?
Tags (comma-separated)
Save Edits
Cancel