Bean新增调用set get方法

master
星期八 3 years ago
parent e37753bab5
commit b36d466837
  1. 38
      panda-beans/src/main/java/org/panda/beans/util/BeanUtil.java

@ -2,6 +2,7 @@ package org.panda.beans.util;
import org.panda.beans.model.BeanAttributeMethod; import org.panda.beans.model.BeanAttributeMethod;
import org.panda.code.uitl.StringUtil;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -305,13 +306,48 @@ public class BeanUtil {
try { try {
Field field = clazz.getDeclaredField(fieldName); Field field = clazz.getDeclaredField(fieldName);
String methodEnd = StringUtil.captureName(fieldName);
setMethod = clazz.getDeclaredMethod(SET_PREFIX+methodEnd,new Class[]{field.getType()});
getMethod = clazz.getDeclaredMethod(GET_PREFIX+methodEnd,new Class[]{});
beanAttributeMethod = new BeanAttributeMethod(setMethod,getMethod,fieldName);
} catch (NoSuchFieldException e) { } catch (NoSuchFieldException e) {
LOGGER.error("无对应属性",e); LOGGER.error("无对应属性",e);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} }
return beanAttributeMethod; return beanAttributeMethod;
} }
public static void setMethod(Object obj,String fieldName,Object value){
Class clazz = obj.getClass();
BeanAttributeMethod beanAttributeMethod = getAttributeMethod(clazz,fieldName);
Method setMethod = beanAttributeMethod.getSetMethod();
try {
setMethod.invoke(obj,new Object[]{value});
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
public static Object getMethod(Object obj,String fieldName){
Class clazz = obj.getClass();
BeanAttributeMethod beanAttributeMethod = getAttributeMethod(clazz,fieldName);
Method getMethod = beanAttributeMethod.getGetMethod();
Object[] args;
Object value = null;
try {
value = getMethod.invoke(obj, new Object(){});
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return value;
}
} }

Loading…
Cancel
Save