1

// 我使用这个简单的程序: public static Object convertToBean(Class type, Map map) { BeanInfo beanInfo; 对象 obj = null; 尝试 { beanInfo = Introspector.getBeanInfo(type); obj = type.newInstance();

            // When I debugging to here, I found that some properties is different from the variable the Object own. PropertyDescriptor changes charactor case when the variable is not in "String" type.
            PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
            for (PropertyDescriptor descriptor : propertyDescriptors) {
                String propertyName = descriptor.getName();

                if (map.containsKey(propertyName)) {
                    Object value = map.get(propertyName);
                    Object[] args = new Object[1];
                    args[0] = value;
                    descriptor.getWriteMethod().invoke(obj, args);
                }
            }
        } catch (Exception ignored) {
        }
        return obj;
    }

//Using BeanMap is the same question.
4

1 回答 1

1

最后我找到了根本原因。通过将“A01”更改为“a01”解决了问题。变量名必须是严格的骆驼规则。第一个字符必须小写,除了前两个字符都是大写,如“AD”。因为 setter 和 getter 方法将以相同的模式生成。所以很难识别一个变量的真实名称。

于 2016-12-09T03:35:32.153 回答