AbstractListTypeConverter implements TypeConverter<List, List>, Named, NeedsJTransfo { @Override public List reverse(List domainObjects, SyntheticField toField, Object toObject, String... tags) throws JTransfoException { if (null == domainObjects) { return getNullList(toField, toObject); } List<Object> res = newList(toField, toObject); for (Object domain : domainObjects) { if (isPrimitiveOrString(domain.getClass())) { res.add(domain); } else { res.add(doReverseOne(jTransfo, domain, toField, toType, tags)); } } return sort(res); } AbstractListTypeConverter(String name, Class<?> toType); @Override String getName(); @Override void setJTransfo(JTransfo jTransfo); @Override boolean canConvert(Type realToType, Type realDomainType); abstract Object doConvertOne(JTransfo jTransfo, Object toObject, Class<?> domainObjectType, String... tags); @Override List convert(List toObjects, SyntheticField domainField, Object domainObject, String... tags); Object doReverseOne(
            JTransfo jTransfo, Object domainObject, SyntheticField toField, Class<?> toType, String... tags); @Override List reverse(List domainObjects, SyntheticField toField, Object toObject, String... tags); void setKeepNullList(boolean keepNullList); void setAlwaysNewList(boolean alwaysNewList); void setSortList(boolean sortList); void setEmptyListSupplier(Supplier<List> emptyListSupplier);  }
@Test public void testReverse() throws Exception { AddressDomain ad1 = new AddressDomain(); ad1.setId(1L); AddressDomain ad2 = new AddressDomain(); ad2.setId(2L); List<AddressDomain> addresses = new ArrayList<>(); addresses.add(ad1); addresses.add(ad2); Object toObject = new Object(); List resList = spy(new ArrayList()); when(field.get(toObject)).thenReturn(resList); List<AddressTo> res = listTypeConverter.reverse(addresses, field, toObject); assertThat(res).isNotNull(); assertThat(res).hasSize(2); verify(jTransfo).convertTo(ad1, AddressTo.class); verify(jTransfo).convertTo(ad2, AddressTo.class); verify(field).get(toObject); verify(resList).clear(); }
@Test public void testReverseGetFails() throws Exception { AddressDomain ad1 = new AddressDomain(); ad1.setId(1L); AddressDomain ad2 = new AddressDomain(); ad2.setId(2L); List<AddressDomain> addresses = new ArrayList<>(); addresses.add(ad1); addresses.add(ad2); Object toObject = new Object(); when(field.get(toObject)).thenThrow(new NullPointerException()); List<AddressTo> res = listTypeConverter.reverse(addresses, field, toObject); assertThat(res).isNotNull(); assertThat(res).hasSize(2); verify(jTransfo).convertTo(ad1, AddressTo.class); verify(jTransfo).convertTo(ad2, AddressTo.class); verify(field).get(toObject); }