1、引入Jar
2、代码
package cn.szsc.com.common.util;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.apache.log4j.Logger;
import org.springframework.cglib.beans.BeanGenerator;
import org.springframework.cglib.beans.BeanMap;
import com.alibaba.fastjson.JSONObject;
import cn.szsc.com.module.company.entity.ListedInfo;
public class DynamicObject {
private Object object; //对象
private BeanMap beanMap; //对象的属性
private BeanGenerator beanGenerator; //对象生成器
private Map
private static final Logger logger = Logger.getLogger(DynamicObject.class);
public DynamicObject() {
}
/**给对象属性赋值
*
* @param property
* @param value
*/
public void setValue(String property, Object value){
beanMap.put(property, value);
}
private void setValue(Object object, Map
for(String propertyName : property.keySet()){
if(allProperty.containsKey(propertyName)){
Object propertyValue = getPropertyValueByName(object, propertyName);
this.setValue(propertyName, propertyValue);
}
}
}
private void setValue(Map
for(Entry
this.setValue(entry.getKey(), entry.getValue());
}
}
/**通过属性名获取属性值
*
* @param property
* @return
*/
public Object getValue(String property){
return beanMap.get(property);
}
/**获取该bean的实体
*
* @return
*/
public Object getObject(){
return this.object;
}
public Map
return allProperty;
}
private Object generateObject(Map propertyMap){
if(null == beanGenerator){
beanGenerator = new BeanGenerator();
}
Set keySet = propertyMap.keySet();
for(Iterator i = keySet.iterator(); i.hasNext();){
String key = (String) i.next();
beanGenerator.addProperty(key, (Class)propertyMap.get(key));
}
return beanGenerator.create();
}
/**添加属性名与属性值
*
* @param propertyType
* @param propertyValue
*/
private void addProperty(Map propertyType, Map propertyValue){
if(null == propertyType){
throw new RuntimeException("动态添加属性失败!");
}
Object oldObject = object;
object = generateObject(propertyType);
beanMap = BeanMap.create(object);
if(null != oldObject){
setValue(oldObject, allProperty);
}
setValue(propertyValue);
if(null == allProperty){
allProperty = propertyType;
}else{
allProperty.putAll(propertyType);
}
}
/**获取对象中的所有属性名与属性值
*
* @param object
* @return
* @throws ClassNotFoundException
*/
public Map
Map
Field[] fields = object.getClass().getDeclaredFields();
for(int index = 0; index < fields.length; index ++){
Field field = fields[index];
String propertyName = field.getName();
Class> propertyType = Class.forName(field.getGenericType().getTypeName());
map.put(propertyName, propertyType);
}
return map;
}
/**获取对象中的所有属性名与属性值
*
* @param object
* @return
* @throws ClassNotFoundException
*/
public Map
Map
Field[] fields = object.getClass().getDeclaredFields();
for(int index = 0; index < fields.length; index ++){
Field field = fields[index];
String propertyName = field.getName();
Object propertyValue = getPropertyValueByName(object, propertyName);
map.put(propertyName, propertyValue);
}
return map;
}
/**根据属性名获取对象中的属性值
*
* @param propertyName
* @param object
* @return
*/
private Object getPropertyValueByName(Object object, String propertyName){
String methodName = "get" + propertyName.substring(0, 1).toUpperCase() + propertyName.substring(1);
Object value = null;
try {
Method method = object.getClass().getMethod(methodName, new Class[]{});
value = method.invoke(object, new Object[]{});
} catch (Exception e) {
logger.error(String.format("从对象%s获取%s的=属性值失败", object, propertyName));
}
return value;
}
public static void main(String[] args) throws ClassNotFoundException {
ListedInfo info = new ListedInfo();
info.setId("id");
info.setCompanyId("companyId");
info.setListedCity("sz");
info.setListedCode("111111");
info.setListedCountry("China");
info.setCreateUser("cd");
DynamicObject dynamicBean = new DynamicObject();
//1、在原来的对象新增属性
Map
Map
allPropertyType.put("houseName", Class.forName("java.lang.String"));
allPropertyValue.put("houseName", "my hotel");
dynamicBean.addProperty(allPropertyType, allPropertyValue);
//2、动态创建一个新对象
Map
Map
newPropertyType.put("houseId", Class.forName("java.lang.String"));
newPropertyValue.put("houseId", "这是什么东西");
newPropertyType.put("listedInfo", Class.forName("cn.szsc.com.module.company.entity.ListedInfo"));
newPropertyValue.put("listedInfo", info);
dynamicBean.addProperty(newPropertyType, newPropertyValue);
Object entity = dynamicBean.getObject();
System.out.println(JSONObject.toJSONString(entity));
}
}