自定义表单,动态对象工具

yun-zuoyi
alwaysfrin 6 years ago
parent 5f5db23021
commit 576743ed18

@ -1,7 +1,21 @@
package cn.estsh.i3plus.pojo.base.dynamic;
import cn.estsh.i3plus.pojo.base.bean.BaseBean;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @Description :
@ -10,24 +24,46 @@ import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
* @CreateDate : 2019-01-24 15:56
* @Modify:
**/
public class DynamicEntity extends BaseBean {
public class DynamicEntity extends BaseBean implements Serializable {
public static final Logger LOGGER = LoggerFactory.getLogger(DynamicEntity.class);
public String tableName;
/*private String uri;
private String method;
private String uri;
private Object[] args;
private Object result;
private String operator;
private String appName;
private String appName;*/
public List<String> propertyList;
public DynamicEntity(){
try {
this.setPropertyList(new ArrayList<>());
initDynamic();
} catch (Exception e) {
e.printStackTrace();
}
}
public DynamicEntity(String tableName){
this.tableName = tableName;
}
public String getTableName() {
return tableName;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
/**
*
*
* @param method
* @param uri
* @param args
* @param result
* @return
*/
*//*
public DynamicEntity get(String method, String uri, Object[] args, Object result, String operator, String appName) {
setMethod(method);
setUri(uri);
@ -84,10 +120,160 @@ public class DynamicEntity extends BaseBean {
public void setAppName(String appName) {
this.appName = appName;
}*/
/**
* 便
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws NoSuchMethodException
*/
public void initDynamic() {
Field[] fields = this.getClass().getDeclaredFields();
Method setMethod = null;
String setMethodName,propName;
Object fieldVal = null;
for(Field f : fields) {
propName = f.getName().replace("$cglib_prop_", "");
if(!"LOGGER".equals(propName) && !"propertyList".equals(propName)) {
this.getPropertyList().add(propName); //添加到属性list中
setMethodName = "set" + propName.substring(0,1).toUpperCase() + propName.substring(1);
f.setAccessible(true);
try {
fieldVal = f.get(this);
} catch (IllegalAccessException e) {
fieldVal = null;
}
if(fieldVal == null) {
if (f.getType() == Integer.class) {
fieldVal = 0;
} else if (f.getType() == Long.class) {
fieldVal = 0L;
} else if (f.getType() == Float.class) {
fieldVal = 0.0f;
} else if (f.getType() == Double.class) {
fieldVal = 0.0d;
} else {
fieldVal = "";
}
}
try {
setMethod = this.getClass().getDeclaredMethod(setMethodName, new Class[]{f.getType()});
setMethod.invoke(this, fieldVal);
} catch (NoSuchMethodException e) {
LOGGER.error("没有方法:{}", setMethodName, e);
} catch (IllegalAccessException e) {
LOGGER.error("入参出错:{}:{}:{}", f, f.getType(), fieldVal, e);
} catch (InvocationTargetException e) {
LOGGER.error("方法返回出错:{}", setMethodName, e);
}
}
}
}
@Override
public String toString() {
return ReflectionToStringBuilder.toString(this);
String result = "{";
Object fieldVal = null;
Field[] fields = this.getClass().getDeclaredFields();
String fieldName;
for(Field f : fields) {
fieldName = f.getName().replace("$cglib_prop_", "");
if(!"LOGGER".equals(fieldName) && !"propertyList".equals(fieldName)) {
f.setAccessible(true);
fieldVal = new Object();
try {
fieldVal = f.get(this);
} catch (IllegalAccessException e) {
fieldVal = null;
}
if (fieldVal == null) {
if (f.getType() == Integer.class || f.getType() == Long.class) {
fieldVal = 0;
} else if (f.getType() == Float.class || f.getType() == Double.class) {
fieldVal = 0.0;
} else {
fieldVal = "";
}
}
result += "\"" + fieldName + "\":\"" + fieldVal + "\",";
}
}
if(fields.length > 0) {
result = result.substring(0,result.length()-1);
}
result += "}";
return result;
}
/**
*
* @param propName
* @param val
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws NoSuchMethodException
*/
public void setDynProperty(String propName,Object val){
//初始化set方法
String setMethodName = "set" + propName.substring(0, 1).toUpperCase() + propName.substring(1);
//获取方法
Method setMethod = null;
try {
setMethod = this.getClass().getDeclaredMethod(setMethodName, new Class[]{val.getClass()});
setMethod.invoke(this, val);
} catch (NoSuchMethodException e) {
LOGGER.error("没有方法:{}",setMethodName,e);
} catch (IllegalAccessException e) {
LOGGER.error("入参出错:{}:{}",val,val.getClass(),e);
} catch (InvocationTargetException e) {
LOGGER.error("方法返回出错:{}",setMethodName,e);
}
}
/**
*
* @param propName
* @return
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
public Object getDynProperty(String propName){
//初始化set方法
String setMethodName = "get" + propName.substring(0,1).toUpperCase() + propName.substring(1);
try {
//获取方法
Method getMethod = this.getClass().getDeclaredMethod(setMethodName);
//实现方法
return getMethod.invoke(this);
} catch (NoSuchMethodException e) {
LOGGER.error("没有方法:{}:{}",setMethodName,propName,e);
} catch (IllegalAccessException e) {
LOGGER.error("没有方法:{}:{}",setMethodName,propName,e);
} catch (InvocationTargetException e) {
LOGGER.error("方法返回出错:{}",setMethodName,e);
}
return null;
}
public List<String> getPropertyList() {
return propertyList;
}
public void setPropertyList(List<String> propertyList) {
this.propertyList = propertyList;
}
}

Loading…
Cancel
Save