Is there a way to map a set to a list with BeanUtils.copyProperties?

I am using BeanUtils.copyProperties

two beans to transform.

BeanUtils.copyProperties(organization, dtoOrganization);

      

I would like to have List

in one bean and a Set

in the other.

First bean:

public class Form {

  private Set<Organization> organization;

}

      

Second bean:

public final class DTOForm {

  private List<DTOOrganization> organization;

}

      

The result is an exception as stated here: Arguments type mismatch with BeanUtils.copyProperties

Can you customize BeanUtils.copyProperties

to achieve it?

+3


source to share


2 answers


You can fix this problem with custom converters. The main idea is to register a new converter for Set

using ConvertUtils.register(Converter converter, Class<?> clazz)

. This is not a problem to implement your custom converter convert(Class<T> type, Object value)

.

Here's a simple example for your problem:

ListEntity that has a property List

(don't ignore setters and getters, as far as I know, their existence is required):

public class ListEntity {
    private List<Integer> col = new ArrayList<>();

    public List<Integer> getCol() {
        return col;
    }

    public void setCol(List<Integer> col) {
        this.col = col;
    }
}

      

SetEntity which has a property Set

:



public class SetEntity {
    private Set<Integer> col = new HashSet<>();

    public Set<Integer> getCol() {
        return col;
    }

    public void setCol(Set<Integer> col) {
        this.col = col;
    }
}

      

A simple test class to work with:

public class Test {
    public static void main(String... args) throws InvocationTargetException, IllegalAccessException {
        SetEntity se = new SetEntity();
        se.getCol().add(1);
        se.getCol().add(2);
        ListEntity le = new ListEntity();
        ConvertUtils.register(new Converter() {
            @Override
            public <T> T convert(Class<T> tClass, Object o) {
                List list = new ArrayList<>();
                Iterator it = ((Set)o).iterator();
                while (it.hasNext()) {
                    list.add(it.next());
                }
                return (T)list;
            }
        }, List.class);
        BeanUtils.copyProperties(le, se);
        System.out.println(se.getCol().toString());
        System.out.println(le.getCol().toString());
    }
}

      

The main idea behind this snipper code is that we register a converter for all properties of the destination class List

, it will try to convert some object o

to List

. Assuming what o

is a set, we iterate over it and then return the newly created list.

The result le

will contain the values 1

and 2

. If you no longer need this converter, you can cancel it using ConvertUtils.deregister()

.

+1


source


Converting from a set of one type to a list of another type is rather lengthy.



While you can achieve this by creating custom PropertyEditors JavaBeans, I would rather use a Mapper Framework like Dozer .

+1


source







All Articles