diff --git a/modules/i3plus-pojo-base/src/main/java/cn/estsh/i3plus/pojo/base/dynamic/DynamicEntity.java b/modules/i3plus-pojo-base/src/main/java/cn/estsh/i3plus/pojo/base/dynamic/DynamicEntity.java index 2fcee00..c8006b8 100644 --- a/modules/i3plus-pojo-base/src/main/java/cn/estsh/i3plus/pojo/base/dynamic/DynamicEntity.java +++ b/modules/i3plus-pojo-base/src/main/java/cn/estsh/i3plus/pojo/base/dynamic/DynamicEntity.java @@ -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 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 getPropertyList() { + return propertyList; + } + + public void setPropertyList(List propertyList) { + this.propertyList = propertyList; } }