0
0

More than 3 years have passed since last update.

[Java] BeanClassに全ての項目に値がリフラッシュ

Last updated at Posted at 2019-10-30

以前仕事中によく使われていた共通メソッド

呼出元.java {
    return (GetChangeAddressDialogInfoOutDto) CommonUtil.refreshServOutDto(serviceOutDto);
}

CommonUtil.java
/**
     * ServiceOutDtoをRereshする <br>
     * (String Type : Null -> Blank)
     * 
     * @param bean
     * @return Object
     */
    public static Object refreshServOutDto(Object bean) {
        if (bean != null) {
            return refreshServOutDto(bean, StringUtils.EMPTY);
        }
        return bean;
    }

    /**
     * ServiceOutDtoをRereshする
     * 
     * @param bean
     * @param defaultStr
     * @return Object
     */
    @SuppressWarnings({"rawtypes", "unchecked"})
    public static Object refreshServOutDto(Object bean, String defaultStr) {
        if (bean != null) {
            Map bMap = new BeanMap(bean);
            HashMap hMap = new HashMap(bMap);
            hMap.remove("class");
            Iterator itr = hMap.keySet().iterator();
            while (itr.hasNext()) {
                String propNm = (String) itr.next();
                reSetter(bean, propNm, defaultStr);
            }
        }
        return bean;
    }

    /**
     * Nullをブランクに変換
     * 
     * @param bean
     * @param propNm
     * @param defaultStr
     */
    @SuppressWarnings("rawtypes")
    public static <T> void reSetter(Object bean, String propNm, String defaultStr) {
        // デスクリプタを用意
        PropertyDescriptor nameProp;
        try {
            nameProp = new PropertyDescriptor(propNm, bean.getClass());
            // ゲッターメソッド取得
            Method nameGetter = nameProp.getReadMethod();
            Object nowVal = nameGetter.invoke(bean, (Object[]) null);

            // セッターメソッド取得
            Method nameSetter = nameProp.getWriteMethod();
            String PropType = nameProp.getPropertyType().toString();
            if (PropType.endsWith("java.util.List")) {
                // 配列
                if (nowVal == null) {
                    nameSetter.invoke(bean, (List) new ArrayList());
                }
            } else if (PropType.endsWith("java.lang.String")) {
                // 文字列
                String tmpStr = (String) nowVal;
                tmpStr = StringUtils.defaultIfEmpty(((String) nowVal), defaultStr);
                // "null"/"NULL"/"Null"の場合、変換
                if (Objects.equal(tmpStr.toLowerCase(), "null")) {
                    tmpStr = defaultStr;
                }
                nameSetter.invoke(bean, tmpStr);
            } else if (PropType.endsWith("int")) {
                // int

            } else if (PropType.endsWith("java.lang.Number")) {
                // Number

            } else if (PropType.endsWith("boolean")) {
                // boolean

            } else if (PropType.endsWith("java.util.Date")) {
                // Date

            } else if (PropType.matches(".*com\\.smbc_card\\.credit\\.compass\\.service\\.dto\\..+")) {
                // Java Bean Class (ex. Addressなど)
                if (nowVal == null) {
                    nameSetter.invoke(    bean,
                                        refreshServOutDto(getClassForName(
                                                                            PropType.replaceFirst(    "class ",
                                                                                                    StringUtils.EMPTY))
                                            .newInstance()));
                }
            }
        } catch (IntrospectionException | IllegalAccessException | IllegalArgumentException | InvocationTargetException
            | InstantiationException | ClassNotFoundException e) {
            System.out.println("異常情報 :" + e);
        }
    }

    /**
     * getClassForName
     * 
     * @param className
     * @return Class<T>
     * @throws ClassNotFoundException
     */
    @SuppressWarnings("unchecked")
    public static <T> Class<T> getClassForName(String className) throws ClassNotFoundException {
        return (Class<T>) Class.forName(className);
    }
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0