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」のように、最初の 2 文字がすべて大文字であることを除いて、最初の文字は小文字でなければなりません。setter メソッドと getter メソッドは同じパターンで生成されるためです。そのため、1 つの変数の実際の名前を認識するのは困難です。

于 2016-12-09T03:35:32.153 に答える