L
L
LS Timer2019-08-23 09:08:16
Java
LS Timer, 2019-08-23 09:08:16

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");

How to get the save method from it?
Does not work

Method saveMethod = field.getClass().getMethod("save", dict.getClass());

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2019-08-23
@light___soul

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 question

Ask a Question

731 491 924 answers to any question