java 反射构造函数,传参时int被自动装箱成Integer,那么如何得到int.class 20
目的:自定义一个函数,传入类.class与可变参数,返回通过反射调用对应构造函数构造出的类注意:int.class不等于Integer.class问题:因此此处调用Per...
目的:自定义一个函数,传入类.class与可变参数,返回通过反射调用对应构造函数构造出的类
注意:int.class不等于Integer.class
问题:因此此处调用Person p = createPerson(Person.class,3,"nilu");会找不到对应构造函数,因为它找的是传入Integer与String的构造函数,如何解决?
//junit测试函数
@Test
public void test() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
Person p = createPerson(Person.class,3,"nilu");
p.eat();
}
public <T> T createPerson(Class<T> clazz, Object ...args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
final List<Class> list = new ArrayList<Class>();
for(Object obj : args) {
list.add(obj.getClass());
}
Class []_args = new Class[args.length];
list.toArray(_args);
list.clear();
Constructor<T> constructor = clazz.getConstructor(_args);
T t = constructor.newInstance(args);
return t;
}
类:
public class Person {
private String name;
private int age;
public Person() {
this.age = 18;
this.name = "default name";
System.out.println("Person constructor");
}
public Person(String name) {
this.name = name;
System.out.println("Person name : " + name);
}
public Person(int age, String name) {
this.age = age;
this.name = name;
System.out.println("Person name : " + name);
System.out.println("Person age : " + age);
}
}
=======================================================================
问题描述补充:
Constructor<T> constructor = clazz.getConstructor(_args);
这一行 根据_args是哪些.class 会决定得到哪个构造函数,这里实际得到的是Integer.class与String.class;
然而Person(int age, String name)实际要的是int.class与String.class;
这是由于调用createPerson时候,3这个int被自动装箱了,变成了Integer,因此程序会报错提示味找到构造函数createPerson(Integer,String) 展开
注意:int.class不等于Integer.class
问题:因此此处调用Person p = createPerson(Person.class,3,"nilu");会找不到对应构造函数,因为它找的是传入Integer与String的构造函数,如何解决?
//junit测试函数
@Test
public void test() throws ClassNotFoundException, SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
Person p = createPerson(Person.class,3,"nilu");
p.eat();
}
public <T> T createPerson(Class<T> clazz, Object ...args) throws SecurityException, NoSuchMethodException, IllegalArgumentException, InstantiationException, IllegalAccessException, InvocationTargetException {
final List<Class> list = new ArrayList<Class>();
for(Object obj : args) {
list.add(obj.getClass());
}
Class []_args = new Class[args.length];
list.toArray(_args);
list.clear();
Constructor<T> constructor = clazz.getConstructor(_args);
T t = constructor.newInstance(args);
return t;
}
类:
public class Person {
private String name;
private int age;
public Person() {
this.age = 18;
this.name = "default name";
System.out.println("Person constructor");
}
public Person(String name) {
this.name = name;
System.out.println("Person name : " + name);
}
public Person(int age, String name) {
this.age = age;
this.name = name;
System.out.println("Person name : " + name);
System.out.println("Person age : " + age);
}
}
=======================================================================
问题描述补充:
Constructor<T> constructor = clazz.getConstructor(_args);
这一行 根据_args是哪些.class 会决定得到哪个构造函数,这里实际得到的是Integer.class与String.class;
然而Person(int age, String name)实际要的是int.class与String.class;
这是由于调用createPerson时候,3这个int被自动装箱了,变成了Integer,因此程序会报错提示味找到构造函数createPerson(Integer,String) 展开
2个回答
展开全部
循环取参数class的时候判断一下是否基本类型,如果是就先转成基本类型的class。或者判断下这个类是否有个TYPE常量:
Class cls = obj.getClass();
Field f = cls.getDeclaredField("TYPE");
if (f != null) {
list.add((Class) f.get(null));
} else {
list.add(cls);
}
追问
1、已经装箱了如何判断它是否是基本数据类型?
2、Field f = cls.getDeclaredField("TYPE"); 什么意思? cls是String.class或者Integer.class 你这样得到什么字段?
追答
1、判断它是否是基本类型对应的包装类(Byte/Short/Integer/Long/...)
2、每个基本类型对应的包装类都有一个TYPE常量,这个TYPE就是基本类型的class,你看看它们的源码就明白了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |