The idea is not to be specific, but to be general. This is an example. In 
practice, I don't know what "o" is and so there has to be a discovery phase. 
I'm not sure how to make it more general than discovering the class via 
o.getClass(). As in this example, yes the class object returned is an 
ObservableList but I'm unable to add an object to this list via reflection. 
Why? And what can be done to change that? Why should it be changed? Because an 
object can be added to the list via vbox.getChildren().add(). Therefore an 
object should be permitted to be added via introspection. The avenue for the 
addition is different, the end result should be the same.
There are lots of other classes in the Java libraries that have this same 
issue. 
There's also the issue of what gets returned can be many different types. How 
do you figure out which type should be returned? Not a trivial question; not so 
sure there's an answer. In this case, all I need is a List. Not an ArrayList, 
or an ObservableList, LinkedList, SortedList .... Please don't focus on this 
... yet. Original issue first.
    On Friday, January 12, 2018, 1:25:13 PM CST, mandy chung 
<mandy.ch...@oracle.com> wrote:  
 
  
 
 On 1/12/18 10:26 AM, jeffrey kutcher wrote:
  
             m = o.getClass().getMethod("add", new Class[] { Object.class, });
            o = m.invoke(o, new Object[] { button1, }); 
 
 o.getClass().getMethod(...) is an anti-pattern for finding a public method.   
Object.getClass() returns the implementation class while you want to invoke a 
public method `javafx.collections.ObservableList::add` in this case.  In this 
case, the declaring class of the method is known and so one way to fix it is to 
use the specific Class:
 Class<?> observableListClass = javafx.collections.ObservableList.class;
m = observableListClass.getMethod("add", new Class[] { Object.class, });
o = m.invoke(o, new Object[] { button1, });

Mandy
 
 
 
   

Reply via email to