开发者

Representration of a Empty Class Array

开发者 https://www.devze.com 2023-01-22 11:37 出处:网络
I am actually looking at getting the method, whose name is stored in the string \"methodName\", from the Class \"CC1\" using Java Refl开发者_Go百科ection.

I am actually looking at getting the method, whose name is stored in the string "methodName", from the Class "CC1" using Java Refl开发者_Go百科ection.

Method actualMethod= CC1.getMethod(methodName, parameterTypes);

This is the syntax. The problem is the method takes no parameter. How do I represent that in the parameterTypes ?

where parameterTypes is the array of Class

Similarly, the below code will invoke that method.

Object retobj = actaulMethod.invoke(actualObject, arglist);

The arglist is array of Objects which again has to be nothing.

If anything is unclear , please ask. Thanks .


Don't pass the second argument:

CC1.getMethod(methodName);

(This makes use of varargs)

This is equivalent to passing an empty array:

CC1.getMethod(methodName, new Class[] {});


The signature is:

Method getMethod(String name, Class... parameterTypes) 

So just leave the second parameter out and it should work. i.e.

Method actualMethod= CC1.getMethod(methodName);
0

精彩评论

暂无评论...
验证码 换一张
取 消