元素虚拟属性封装

yun-zuoyi
wei.peng 6 years ago
parent f810e35873
commit cc3b1a741c

@ -1,12 +1,6 @@
package cn.estsh.i3plus.pojo.base.dynamic;
import cn.estsh.i3plus.pojo.base.bean.BaseBean;
import com.google.common.base.CaseFormat;
import lombok.Getter;
import lombok.Setter;
import lombok.val;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -14,8 +8,6 @@ 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.Date;
import java.util.List;
@ -32,14 +24,9 @@ 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 Object[] args;
private Object result;
private String operator;
private String appName;*/
private static final String ATTR_PREFIX = "$cglib_prop_";
public String tableName;
public List<String> propertyList;
public DynamicEntity(){
@ -71,67 +58,14 @@ public class DynamicEntity extends BaseBean implements Serializable {
*/
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) && !"dynProperty".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 if (f.getType() == String.class) {
fieldVal = "";
}else if (f.getType() == Character.class) {
fieldVal = "";
}else if (f.getType() == Boolean.class) {
fieldVal = true;
}else if (f.getType() == Byte.class) {
fieldVal = 0;
}else if (f.getType() == Date.class) {
fieldVal = new Date();
}else {
fieldVal = "";
}
}
try {
setMethod = this.getClass().getDeclaredMethod(setMethodName, new Class[]{f.getType()});
// System.out.println("Method Name:" + setMethod.getName() + "\t\t Value : " + fieldVal);
setMethod.invoke(this, fieldVal);
} catch (ClassCastException e) {
e.printStackTrace();
LOGGER.error("ClassCastException {}", setMethodName, e);
} catch (IllegalArgumentException e) {
e.printStackTrace();
LOGGER.error("IllegalArgumentException {}", setMethodName, e);
} catch (NoSuchMethodException e) {
LOGGER.error("没有方法:{}", setMethodName, e);
} catch (IllegalAccessException e) {
LOGGER.error("入参出错:{}:{}:{}", f, f.getType(), fieldVal, e);
} catch (InvocationTargetException e) {
LOGGER.error("方法返回出错:{}", setMethodName, e);
}catch (RuntimeException e) {
LOGGER.error("RuntimeException {}", setMethodName, e);
}catch (Exception e) {
LOGGER.error("Exception {}", setMethodName, e);
}
String propName = f.getName().replace(ATTR_PREFIX, "");
if(!"LOGGER".equals(propName) && !"ATTR_PREFIX".equals(propName)
&& !"propertyList".equals(propName) && !"dynProperty".equals(propName) ) {
// 添加到属性list中
this.getPropertyList().add(propName);
// 属性初始化
setDynProperty(propName,getDynProperty(propName));
}
}
}
@ -140,31 +74,13 @@ public class DynamicEntity extends BaseBean implements Serializable {
public String toString() {
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 = "";
}
}
String fieldName = f.getName().replace(ATTR_PREFIX, "");
if (!"LOGGER".equals(fieldName) && !"ATTR_PREFIX".equals(fieldName)
&& !"propertyList".equals(fieldName) && !"dynProperty".equals(fieldName)) {
result += "\"" + fieldName + "\":\"" + fieldVal + "\",";
result += "\"" + fieldName + "\":\"" + getDynProperty(fieldName) + "\",";
}
}
if(fields.length > 0) {
@ -187,7 +103,6 @@ public class DynamicEntity extends BaseBean implements Serializable {
/**
*
* //TODO wei.peng 设置为空时会无效
* @param propName
* @param val
* @throws InvocationTargetException
@ -195,12 +110,10 @@ public class DynamicEntity extends BaseBean implements Serializable {
* @throws NoSuchMethodException
*/
public void setDynProperty(String propName,Object val){
//初始化set方法
String setMethodName = "set" + CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, propName);
//获取方法
Method setMethod = null;
String setMethodName = "set" + propName.substring(0,1).toUpperCase() + propName.substring(1);
try {
setMethod = this.getClass().getDeclaredMethod(setMethodName, new Class[]{val.getClass()});
val = getValue(propName,val);
Method setMethod = this.getClass().getDeclaredMethod(setMethodName, new Class[]{val.getClass()});
setMethod.invoke(this, val);
} catch (NoSuchMethodException e) {
LOGGER.error("没有方法:{}",setMethodName,e);
@ -211,23 +124,12 @@ public class DynamicEntity extends BaseBean implements Serializable {
}
}
/**
*
* @param propName
* @return
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalAccessException
*/
public Object getDynProperty(String propName){
//初始化set方法
String setMethodName = "get" + CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, propName);
String setMethodName = "get" + propName.substring(0,1).toUpperCase() + propName.substring(1);
Object result = null;
try {
//获取方法
Method getMethod = this.getClass().getDeclaredMethod(setMethodName);
//实现方法
return getMethod.invoke(this);
result = getMethod.invoke(this);
} catch (NoSuchMethodException e) {
LOGGER.error("没有方法:{}:{}",setMethodName,propName,e);
} catch (IllegalAccessException e) {
@ -235,7 +137,7 @@ public class DynamicEntity extends BaseBean implements Serializable {
} catch (InvocationTargetException e) {
LOGGER.error("方法返回出错:{}",setMethodName,e);
}
return null;
return getValue(propName,result);
}
public List<String> getPropertyList() {
@ -245,4 +147,45 @@ public class DynamicEntity extends BaseBean implements Serializable {
public void setPropertyList(List<String> propertyList) {
this.propertyList = propertyList;
}
/**
*
* @param propName
* @param val
* @return
*/
public Object getValue(String propName,Object val){
if(val == null){
try {
Field field = this.getClass().getDeclaredField(ATTR_PREFIX + propName);
if (field.getType() == Integer.class) {
val = 0;
} else if (field.getType() == Long.class) {
val = 0L;
} else if (field.getType() == Float.class) {
val = 0.0f;
} else if (field.getType() == Double.class) {
val = 0.0d;
} else if (field.getType() == String.class) {
val = "";
}else if (field.getType() == Character.class) {
val = "";
}else if (field.getType() == Boolean.class) {
val = true;
}else if (field.getType() == Byte.class) {
val = 0;
}else if (field.getType() == Date.class) {
val = new Date();
}else {
val = "";
}
}catch (NoSuchFieldException e){
LOGGER.error("没有指定属性:{}:{}",propName,e);
}catch (SecurityException e){
LOGGER.error("获取属性安全错误:{}",propName,e);
}
}
return val;
}
}

@ -1055,7 +1055,99 @@ public class BlockFormEnumUtil {
return tmp;
}
public Object getPropertyVirtual(Object ... objs){
if(this.getValue() == STRING_SPLICE.getValue()){
return getPropertyVirtualString(objs);
}else if(this.getValue() == NUM_ADD.getValue()){
return getPropertyVirtualDoubleAdd(objs);
}else if(this.getValue() == NUM_LESS.getValue()){
return getPropertyVirtualDoubleLess(objs);
}else if(this.getValue() == NUM_MAKE.getValue()){
return getPropertyVirtualDoubleMake(objs);
}else if(this.getValue() == NUM_DIVISION.getValue()){
return getPropertyVirtualDoubleDivision(objs);
}
return objs;
}
private String getPropertyVirtualString(Object ... objs){
if(objs != null && objs.length > 0){
StringBuffer result = new StringBuffer();
for (Object o : objs) {
result.append(o == null ? "" : o.toString());
}
return result.toString();
}
return null;
}
private Double getPropertyVirtualDoubleAdd(Object ... objs){
if(objs != null && objs.length > 0){
Double result = new Double(0);
for (Object o : objs) {
try {
if(o != null){
result += Double.parseDouble(o.toString());
}
}catch (Exception e){
e.printStackTrace();
}
}
return result;
}
return null;
}
private Double getPropertyVirtualDoubleLess(Object ... objs){
if(objs != null && objs.length > 0){
Double result = new Double(0);
for (Object o : objs) {
try {
if(o != null){
result -= Double.parseDouble(o.toString());
}
}catch (Exception e){
e.printStackTrace();
}
}
return result;
}
return null;
}
private Double getPropertyVirtualDoubleMake(Object ... objs){
if(objs != null && objs.length > 0){
Double result = new Double(0);
for (Object o : objs) {
try {
if(o != null){
result *= Double.parseDouble(o.toString());
}
}catch (Exception e){
e.printStackTrace();
}
}
return result;
}
return null;
}
private Double getPropertyVirtualDoubleDivision(Object ... objs){
if(objs != null && objs.length > 0){
Double result = new Double(0);
for (Object o : objs) {
try {
if(o != null){
result /= Double.parseDouble(o.toString());
}
}catch (Exception e){
e.printStackTrace();
}
}
return result;
}
return null;
}
}
/**

Loading…
Cancel
Save