javabean을 통해 대상을 호출하는 set 방법

1270 단어
 /**
     *  javabean set 
     *
     * @param object
     * @param propertyName
     * @param propertyValue
     * @Author zy
     */
    public static void setPropertyValue(Object object, String propertyName, Object propertyValue) throws Exception {
        if (object == null)
            throw new Exception("object  !");
        if (propertyName == null)
            throw new Exception("propertyName  !");
        try {
            BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass());
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
                if (propertyName.equals(propertyDescriptor.getName())) {
                    if (propertyDescriptor.getPropertyType() == null ||
                            !propertyDescriptor.getPropertyType().equals(propertyValue.getClass())) {
                        throw new Exception("propertyValue  ");
                    }
                    propertyDescriptor.getWriteMethod().invoke(object, propertyValue);
                    break;
                }
            }
        } catch (Exception e) {
            throw e;
        }
    }

좋은 웹페이지 즐겨찾기