Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing reflection to get a method; method parameters of interface types aren't found
    primarykey
    data
    text
    <p>Maybe I'm missing something simple here but how do I get a method whose parameter is an interface using reflection.</p> <p>In the following case <code>newValue</code> would be a <code>List&lt;String&gt;</code> called <code>foo</code>. So I would call <code>addModelProperty("Bar", foo);</code> But this only works for me if I don't use the interface and only use <code>LinkedList&lt;String&gt; foo</code>. How do I use an interface for <code>newValue</code> and get the method from <code>model</code> that has an interface as the parameter <code>addBar(List&lt;String&gt; a0)</code>?</p> <p>Here is a more detailed example. (based on: <a href="http://www.oracle.com/technetwork/articles/javase/index-142890.html" rel="nofollow noreferrer">This example</a>)</p> <pre><code>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&lt;String&gt; list) { this.list.addAll(list); } } public class Controller extends AbstractController { public void addList(List&lt;String&gt; list) { addModelProperty(list); } } public void example() { Model model = new Model(); Controller controller = new Controller(); List&lt;String&gt; list = new LinkedList&lt;String&gt;(); list.add("example"); // addList in the model is only found if LinkedList is used everywhere instead of List controller.addList(list); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload