Answer the question
In order to leave comments, you need to log in
Reflection in Java. How to get a method from a parameter (@Autowired) or how to get the value of a parameter from Field?
There is a dynamic Field
Field field = this.getClass().getDeclaredField("dict" + name + "Repository");
Method saveMethod = field.getClass().getMethod("save", dict.getClass());
Answer the question
In order to leave comments, you need to log in
field.getClass()
will return a Field class that does not have a save
. The method must be looked for in the class whose type has a field, and this method must be called on the value of the field.
Example obj = new Example();
Field field = obj.getClass().getDeclaredField("dict" + name + "Repository");
SomeClass fieldValue = (SomeClass) field.get(obj);
Method method = fieldValue.getClass().getDeclaredMethod("save", dict.getClass());
Object returnValue = method.invoke(fieldValue, someArgOfTypeDict);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question