diff --git a/panda-beans/src/main/java/org/panda/beans/util/BeanUtil.java b/panda-beans/src/main/java/org/panda/beans/util/BeanUtil.java index 430ff72..07aeb21 100644 --- a/panda-beans/src/main/java/org/panda/beans/util/BeanUtil.java +++ b/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.code.uitl.StringUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -305,13 +306,48 @@ public class BeanUtil { try { 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) { LOGGER.error("无对应属性",e); + } catch (NoSuchMethodException e) { + e.printStackTrace(); } 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; + } + }