How does one handle a java method which returns a non-public sub-type via reflection?

mandy chung mandy.chung at oracle.com
Fri Jan 12 19:25:20 UTC 2018



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






More information about the jigsaw-dev mailing list