Usando reflexão para obter um método; parâmetros de método de tipos de interface não encontrados
Talvez esteja faltando algo simples aqui, mas como obtenho um método cujo parâmetro é uma interface usando reflexão.
No seguinte casonewValue
seria umList<String>
chamadofoo
. Então eu ligariaaddModelProperty("Bar", foo);
Mas isso só funciona para mim se eu não usar a interface e usar apenasLinkedList<String> foo
. Como uso uma interface paranewValue
e obtenha o método emmodel
que tem uma interface como parâmetroaddBar(List<String> a0)
?
Aqui está um exemplo mais detalhado. (baseado em:Este exemplo)
public class AbstractController {
public setModel(AbstractModel model) {
this.model = model;
}
protected void addModelProperty(String propertyName, Object newValue) {
try {
Method method = getMethod(model.getClass(), "add" + propertyName, newValue);
method.invoke(model, newValue);
} catch (NoSuchMethodException e) {
} catch (InvocationTargetException e) {
} catch (Exception e) {}
}
}
public class AbstractModel {
protected PropertyChangeSupport propertyChangeSupport;
protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
propertyChangeSupport.firePropertyChange(propertyName, oldValue, newValue);
}
}
public class Model extends AbstractModel {
public void addList(List<String> list) {
this.list.addAll(list);
}
}
public class Controller extends AbstractController {
public void addList(List<String> list) {
addModelProperty(list);
}
}
public void example() {
Model model = new Model();
Controller controller = new Controller();
List<String> list = new LinkedList<String>();
list.add("example");
// addList in the model is only found if LinkedList is used everywhere instead of List
controller.addList(list);
}