我正在尝试使用反射获取类的方法,其中该方法的参数有时是原始类型或任何对象。
例子:
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句