我试图使用反射获取类的方法,其中该方法的参数有时是基本类型或任何对象。
例:
public class A {
public void display(short a){
System.out.println(a);
}
}
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import com.rexample.model.A;
public class ReflectionExample {
public static void main(String[] args) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
ReflectionExample example=new ReflectionExample();
example.print();
}
public void print() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
String nameOfTheMethod="display";//Assume name of the method is display
short v=10;//Assume primitive type is short
Object value=v;
Method method=A.class.getDeclaredMethod(nameOfTheMethod,value.getClass());
method.invoke(new A(),value);
}
}
我收到错误:
Exception in thread "main" java.lang.NoSuchMethodException: com.rexample.model.A.display(java.lang.Short)
at java.lang.Class.getDeclaredMethod(Class.java:2130)
at com.rexample.test.ReflectionExample.print(ReflectionExample.java:34)
at com.rexample.test.ReflectionExample.main(ReflectionExample.java:27)
上面的代码只是我正在构建的一个较大程序的一个小例子,我无法获得参数类型short
或任何其他原始类型的方法。
我无法在我的代码中直接使用short.class或Short.TYPE,因为我试图以更通用的方式进行。
有没有办法解决我的基本类型参数和任何对象的问题?
实际上,您将值(对象或基元)分配给声明的类型Object
:
Object value=v;
由于v
是一个Object
,它是可以的,因为它是一个原始的问题,因为在运行时JVM将它装入相应的包装类。
所以在这里你失去了v
原始的原始信息:
method=A.class.getDeclaredMethod(nameOfTheMethod,value.getClass());
正确处理它的唯一方法是区分基元和对象。
你可以重载getType()
来处理两个:
public Class<?> getType(Object o) {
return o.getClass();
}
public Class<?> getType(short s) {
return short.class;
}
public Class<?> getType(int i) {
return int.class;
}
// add overloads with other primitive types if required
这是一个通过反射进行两次调用的示例:一个带有原始参数,另一个带有对象参数。
一类
public class A {
public void display(short a) {
System.out.println("primitive " + a);
}
public void display(Short a) {
System.out.println("Wrapper " + a);
}
}
ReflectionExample类
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class ReflectionExample {
public static void main(String[] args)
throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
ReflectionExample example = new ReflectionExample();
example.print();
}
public void print() throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
String nameOfTheMethod = "display";
// primitive param call
short s = 10;
Method method = A.class.getDeclaredMethod(nameOfTheMethod, getType(s)); // invoke getType(short s)
method.invoke(new A(), s);
// object param call
Short sWrapper = 10;// Assume primitive type is short
method = A.class.getDeclaredMethod(nameOfTheMethod, getType(sWrapper)); // invoke getType(Object o)
method.invoke(new A(), sWrapper);
}
public Class<?> getType(short s) {
return short.class;
}
public Class<?> getType(int i) {
return int.class;
}
... //
public Class<?> getType(Object o) {
return o.getClass();
}
}
输出:
原始10
包装10