显示方法param类型的所有数组元素 [英] display all array elements of a method param types

查看:133
本文介绍了显示方法param类型的所有数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取包含方法的Param类型的数组的所有元素(使用 Java.reflect 动态获取),



这里的代码如果我有两个参数的方法:

 方法testMethod = c.getMethod(method.getName() ,新的Class [] {int.class,String.class}); 

但它应该是动态的,所以我使用: method.getTypeParameters() ,它返回所有参数类型的数组。



然后当我写:

 方法testMethod = c.getMethod(method.getName(),new Class [] {m​​ethod.getParameterTypes}); 

但它给了我: 类型不匹配:无法从Class []到



我知道我必须循环参数,因为我有:

  method.getParameterTypes [0] //给出int

  method.getParameterTypes [1] //给出String

那么我该如何实现呢?一个循环呢?你有什么想法吗?谢谢。

解决方案


 方法testMethod = c.getMethod (
method.getName(),new Class [] {m​​ethod.getParameterTypes});


应该是

 方法testMethod = c.getMethod(
method.getName(),method.getParameterTypes());

第一个问题是你缺少()在$ code> getParameterTypes 的末尾。调用方法时,Java需要明确的括号。



第二个问题是您尝试将 getParameterTypes Class [] 中的类<?> [] 。唯一的一个 Class [] 可以包含的是单独的类,而不是数组,所以只需使用 getParameterTypes()






我不清楚为什么你这样做。您可以从接口获取方法,并将其应用到该接口的实例,而不必查看实例的具体类上的方法。类似于抽象类。除非您的继承图中有一个孔或某种奇怪的相应的静态方法约定,否则不需要使用一种方法签名来获取另一种方法。任何一个都可能是重构的机会。



代码库中反映较少,人们学习越容易,并且您将获得更多的里程找出错误查找器和其他分析工具,以及您将拥有的不完整的类路径问题。


I want to get all the element of the array which contains the Param types of a method (getted dynamically with Java.reflect),

here's the code if I have 2 params in the method :

Method testMethod = c.getMethod(method.getName(), new Class[] {int.class, String.class});

But it should be dynamic, so I use : method.getTypeParameters() which returns an array on all the param types.

Then when I write :

Method testMethod = c.getMethod(method.getName(), new Class[] {method.getParameterTypes});

but it gives me that : Type mismatch: cannot convert from Class[] to Class

the I understood that I have to loop on parameters because I have :

method.getParameterTypes[0] // gives  "int"

and

method.getParameterTypes[1] // gives  "String"

So how can I achieve this ? a loop maybe ? do you have any idea about this ? Thank you.

解决方案

Method testMethod = c.getMethod(
    method.getName(), new Class[] {method.getParameterTypes});

should be

Method testMethod = c.getMethod(
    method.getName(), method.getParameterTypes());

The first problem is that you're missing the () at the end of getParameterTypes. Java requires explicit parentheses when calling a method.

The second problem is that you're trying to put the result of getParameterTypes, a Class<?>[] inside a Class[]. The only thing a Class[] can contain is individual classes, not arrays, so just use the array returned by getParameterTypes().


I'm unclear why you're doing this at all though. You can take a Method from an interface and apply it to an instance of that interface just fine without looking up the method on the instance's concrete class. Similarly for abstract classes. You shouldn't need to use one methods signature to get another method unless there's a hole in your inheritance graph or some kind of strange corresponding static method convention. Either of those is probably an opportunity for refactoring.

The less reflection in your code-base, the easier it will be for people to learn, and the more mileage you will get out of bug finders and other analysis tools, and the fewer incomplete class-path problems you will have.

这篇关于显示方法param类型的所有数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆