「Java」ConvertUtilsBean.registerメソッドで独自コンバーターを実装してコピー先の型に変換する方法

説明
コピー元とコピー先でプロパティーの型が異なる場合、独自コンバーターを実装してコピー先の型に変換します。
1.コピー元クラスの定義

package com.arkgame.study;

import java.util.Date;

public class SrcBean {

      private String username;
      //Date型createDate
      private Date createDate;

      public SrcBean(String username, Date createDate) {
            this.username = username;
            this.createDate = createDate;
      }

      public String getUsername() {
            return username;
      }

      public void setUsername(String username) {
            this.username = username;
      }

      public Date getCreateDate() {
            return createDate;
      }

      public void setCreateDate(Date createDate) {
            this.createDate = createDate;
      }

}

2.コピー先クラスの定義

package com.arkgame.study;

public class DestBean {
      private Integer age;
      // String型createDate
      private String createDate;

      public DestBean(Integer age, String createDate) {
            this.age = age;
            this.createDate = createDate;
      }

      public Integer getAge() {
            return age;
      }

      public void setAge(Integer age) {
            this.age = age;
      }

      public String getCreateDate() {
            return createDate;
      }

      public void setCreateDate(String createDate) {
            this.createDate = createDate;
      }

}

■ 3.独自コンバーターでConverterを実装

package com.arkgame.study;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.beanutils.Converter;

public class ArkStringConverter implements Converter {

      @SuppressWarnings({ "unchecked", "rawtypes" })
      public Object convert(Class type, Object value) {
            if (value == null) {
                  return (String) null;
            } else if (value instanceof Date) {
                  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                  return sdf.format(value);
            } else {
                  return value.toString();
            }
      }
}

■ 4.getConvertUtils().registerで独自コンバーターを呼び出す

package com.arkgame.study;

import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.apache.commons.beanutils.ConvertUtilsBean;

public class BeanCopyPropDemo {

      public static void main(String[] args) throws IllegalAccessException, InvocationTargetException {

            // コピー元
            SrcBean srcBean = new SrcBean("user001", new Date());
            // コピー先
            DestBean dstBean = new DestBean(34, "2020/10/26");

            BeanUtilsBean bub = new BeanUtilsBean(
                        new ConvertUtilsBean(), BeanUtilsBean.getInstance().getPropertyUtils());
            //独自ArkStringConverter String型への変換
            bub.getConvertUtils().register(
                        new ArkStringConverter(), String.class);
            //srcをdstへコピー
            bub.copyProperties(dstBean, srcBean);
            System.out.println("日付:"+dstBean.getCreateDate());
      }

}

■実行結果
日付:2020-10-23 11:18:07

Java

Posted by arkgame