How to use getMethod() with primitive types?

前端 未结 2 1917
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-24 04:31

This is the class:

class Foo {
  public void bar(int a, Object b) {
  }
}

Now I\'m trying to get \"reflect\" this method from the class:

相关标签:
2条回答
  • 2020-12-24 05:11

    There's just an int.class.

    Class[] types = { int.class, Object.class };
    

    An alternative is Integer.TYPE.

    Class[] types = { Integer.TYPE, Object.class };
    

    The same applies on other primitives.

    0 讨论(0)
  • 2020-12-24 05:13

    The parameter of the method is a primitive short not an Object Short.

    Reflection will not find the method because you specified an object short. The parameters in getMethod() have to match exactly.

    EDIT: The question was changed. Initially, the question was to find a method that takes a single primitive short.

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