Verwenden Sie Reflection, um eine Methode zu erhalten. Methodenparameter von Schnittstellentypen werden nicht gefunden

Möglicherweise fehlt hier etwas Einfaches, aber wie erhalte ich eine Methode, deren Parameter eine Schnittstelle mit Reflection ist.

Im folgenden FallnewValue wäre einList<String> namensfoo. Also würde ich @ anrufaddModelProperty("Bar", foo); Aber das funktioniert nur bei mir, wenn ich die Schnittstelle nicht benutze und nur @ benutLinkedList<String> foo. Wie benutze ich eine Schnittstelle fürnewValue und erhalte die Methode vonmodel das hat eine Schnittstelle als ParameteraddBar(List<String> a0)?

Hier ist ein ausführlicheres Beispiel. (beyogen auf:Dieses Beispiel)

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

Antworten auf die Frage(4)

Ihre Antwort auf die Frage