Passing a class (“Country.class”) as an argument in Java

后端 未结 7 1069
感情败类
感情败类 2020-12-12 06:29

I\'m trying to make a method that takes an argument of Country.class, User.class etc, and returns argument.count().

All the po

相关标签:
7条回答
  • 2020-12-12 07:35

    I think you want to do

    private long countModel(Class<? extends Model> clazz) throws SecurityException, NoSuchMethodException, IllegalArgumentException, IllegalAccessException, InvocationTargetException
    {
      Method countMethod =  clazz.getDeclaredMethod("count", null);
      return (Long) countMethod.invoke(null, null);
    }
    

    Hopefully something like this would work (my reflection skills are not really that good).

    0 讨论(0)
提交回复
热议问题