简体   繁体   中英

Reflection: How to invoke method without parameter properly?

I am trying to invoke a method by Java reflection, which don't have any parameter as:

Method getSelectedCriteriaMethod = multipleSorting.getClass().getDeclaredMethod("getSelectedCriteria", null);
EstimateSortField selectedCriteria = (EstimateSortField)getSelectedCriteriaMethod.invoke(multipleSorting, null);

This is working, but I am getting compiler warning:

warning: non-varargs call of varargs method with inexact argument type for last parameter;
cast to java.lang.Class<?> for a varargs call
non-varargs call of varargs method with inexact argument type for last parameter;
cast to java.lang.Object for a varargs call

What is the right way to invoke a method which don't have any parameter?

你可以简单地说

selectedCriteria.invoke(multipleSorting);
getDeclaredMethod("getSelectedCriteria");
invoke(multipleSorting);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM