import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class ReflectUtil {
public final static String CLASS_DATE = "class java.util.Date";
public static Object getObject(Object obj, Map<String, Object> addProperties) {
PropertyDescriptor[] descriptors = new PropertyDescriptor[0];
try {
descriptors = Introspector.getBeanInfo(obj.getClass()).getPropertyDescriptors();
} catch (IntrospectionException e) {
e.printStackTrace();
}
Map<String, Class> propertyMap = new HashMap<>();
Map<String, Object> valMap = new HashMap<>();
for (PropertyDescriptor d : descriptors) {
if (!"class".equalsIgnoreCase(d.getName())) {
propertyMap.put(d.getName(), d.getPropertyType());
try {
valMap.put(d.getName(),d.getReadMethod().invoke(obj));
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
addProperties.forEach((k, v) -> {
String classString = v.getClass().toString();
if (classString.equals(CLASS_DATE)) {
propertyMap.put(k, Long.class);
} else {
propertyMap.put(k, v.getClass());
}
});
DynamicBean dynamicBean = new DynamicBean(obj.getClass(), propertyMap);
propertyMap.forEach((k, v) -> {
try {
if (!addProperties.containsKey(k)) {
dynamicBean.setValue(k, valMap.get(k));
}
} catch (Exception e) {
e.printStackTrace();
}
});
addProperties.forEach((k, v) -> {
try {
String classString = v.getClass().toString();
if (classString.equals(CLASS_DATE)) {
Date date = (Date) v;
dynamicBean.setValue(k, date.getTime());
} else {
dynamicBean.setValue(k, v);
}
} catch (Exception e) {
e.printStackTrace();
}
});
return dynamicBean.getTarget();
}
}
import net.sf.cglib.beans.BeanGenerator;
import net.sf.cglib.beans.BeanMap;
import java.util.Map;
public class DynamicBean {
private Object target;
private BeanMap beanMap;
public DynamicBean(Class superclass, Map<String, Class> propertyMap) {
this.target = generateBean(superclass, propertyMap);
this.beanMap = BeanMap.create(this.target);
}
public void setValue(String property, Object value) {
beanMap.put(property, value);
}
public Object getValue(String property) {
return beanMap.get(property);
}
public Object getTarget() {
return this.target;
}
private Object generateBean(Class superclass, Map<String, Class> propertyMap) {
BeanGenerator generator = new BeanGenerator();
if (null != superclass) {
generator.setSuperclass(superclass);
}
BeanGenerator.addProperties(generator, propertyMap);
return generator.create();
}
}
import com.alibaba.fastjson.JSON;
import java.util.HashMap;
import java.util.Map;
public class ReflectTest {
public static void main(String[] args) {
BeanTest beanTest = new BeanTest();
beanTest.setT1("ttt1");
System.out.println("initial Object : " + JSON.toJSONString(beanTest));
Map<String, Object> addProperties = new HashMap<>();
addProperties.put("t2", "ttt2");
final Object object = ReflectUtil.getObject(beanTest, addProperties);
System.out.println("after Object : " + JSON.toJSONString(object));
}
}
class BeanTest {
private String t1;
public String getT1() {
return t1;
}
public void setT1(String t1) {
this.t1 = t1;
}
}