新增反射生成实例方法

master
星期八 5 years ago
parent d7254d460c
commit 32b544672c
  1. 49
      panda-beans/src/main/java/org/panda/beans/util/BeanUtil.java
  2. 20
      panda-beans/src/test/java/org/panda/beans/entity/SysUser.java
  3. 11
      panda-beans/src/test/java/org/panda/beans/util/BeanUtilTest.java

@ -6,6 +6,7 @@ import org.slf4j.LoggerFactory;
import java.beans.IntrospectionException; import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor; import java.beans.PropertyDescriptor;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@ -208,4 +209,52 @@ public class BeanUtil {
return fieldObjectMap; return fieldObjectMap;
} }
/**
* 通过无参数构造函数返回实例
* @param clazz
* @param <T>
* @return
*/
public static <T> T instantiateClass(Class<T> clazz){
T t = null;
try {
t = clazz.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return t;
}
/**
* 通过有参数构造函数返回实例
* @param clazz
* @param params
* @param <T>
* @return
*/
public static <T> T instantiateClass(Class<T> clazz,Object... params){
T t = null;
Class[] classes = new Class[params.length];
for (int i=0;i<params.length;i++){
classes[i] = params[i].getClass();
}
try {
Constructor constructor = clazz.getConstructor(classes);
t =(T)constructor.newInstance(params);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return t;
}
} }

@ -17,6 +17,16 @@ public class SysUser {
private Date updateTime; private Date updateTime;
public SysUser() {
}
public SysUser(Long id, String name, Date createTime, Date updateTime) {
this.id = id;
this.name = name;
this.createTime = createTime;
this.updateTime = updateTime;
}
public Long getId() { public Long getId() {
return id; return id;
} }
@ -48,4 +58,14 @@ public class SysUser {
public void setUpdateTime(Date updateTime) { public void setUpdateTime(Date updateTime) {
this.updateTime = updateTime; this.updateTime = updateTime;
} }
@Override
public String toString() {
return "SysUser{" +
"id=" + id +
", name='" + name + '\'' +
", createTime=" + createTime +
", updateTime=" + updateTime +
'}';
}
} }

@ -3,6 +3,7 @@ package org.panda.beans.util;
import org.junit.Test; import org.junit.Test;
import org.panda.beans.entity.SysUser; import org.panda.beans.entity.SysUser;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.Date; import java.util.Date;
@ -30,4 +31,14 @@ public class BeanUtilTest {
} }
} }
@Test
public void test(){
// SysUser sysUser = BeanUtil.instantiateClass(SysUser.class);
SysUser sysUser = BeanUtil.instantiateClass(SysUser.class,1L,"qi",new Date(),new Date());
System.out.println(sysUser);
// System.out.println(sysUser);
}
} }
Loading…
Cancel
Save