我正在尝试使用反射获取类的方法,其中该方法的参数有时是原始类型或任何对象。
示例:
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
这是一个通过反射进行两次调用的示例:一个使用原始参数,另一个使用对象参数。
A类
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();
}
}
输出:
primitive 10
Wrapper 10
关于java - 使用反射获取具有原始类型参数的类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49084164/