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

Michał Zegan webczat_200 at poczta.onet.pl
Fri Jan 12 19:37:20 UTC 2018


Actually, shouldn't reflection be fixed so that you can invoke any
method of a non exported class, if the method overrides one that is
accessible to the class calling invoke... Not sure it makes sense...

W dniu 12.01.2018 o 20:25, mandy chung pisze:
> 
> 
> 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