简体   繁体   中英

Can Class.getmethod find the method include type of Class< ? > paramter?

My English is not good,so I just can give you the code and the error message Codes:

  public class ReflectDemo {
  private   void t00(Class<?> unknown,String str)
   {
       System.out.println("t0");
   }
}
   @Test
    public void test01()
    {
        Class<?> unknown= null;
        try {
            Method method= ReflectDemo.class.getDeclaredMethod("t00",unknown,String.class);
            method.setAccessible(true);
            method.invoke(new ReflectDemo(),"a","b");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Error message: java.lang.NoSuchMethodException: Demo.ReflectDemo.t00(null, java.lang.String) How can I get the method of t00?

You can't pass null as the value of a parameter to getDeclaredMethod .

Pass the class literal instead:

ReflectDemo.class.getDeclaredMethod("t00", Class.class,String.class)

Note that your invoke will then fail, because "a" is not an instance of Class<?> .

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