简体   繁体   English

如何检查方法是否使用反射声明?

[英]How to check if method is declared using reflection?

I'm thinking about possibility of checking if methods was declared? 我正在考虑检查方法是否已声明的可能性? For example in code 例如在代码中

class foo
{
 variable.getSomething(true);
}

Can I check if method getSomething(true) was declared and what is value of boolean arrgument using reflection? 我可以检查方法getSomething(true)是否已声明,以及使用反射的布尔参数的值是什么?

You use the Class#getMethod() call for reflection. 您使用Class#getMethod()调用进行反射。 method_name is the method you are looking for. method_name是您要寻找的方法。 For the primitive you want to just use the lower case boolean.class . 对于原语,您只需要使用小写的boolean.class Boolean.TYPE will work as well for the primitive value. Boolean.TYPE也适用于原始值。 Boolean.class will not work at all because it is the object type. Boolean.class根本不起作用,因为它是对象类型。

Class<variable type> clazz = variable.getClass();
try {
    //Boolean.TYPE can be used instead of boolean.class
    Method m = clazz.getMethod("method_name", boolean.class);
    //method exists
 }
 catch(NoSuchMethodException e) {
      //method does not exist
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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