ConverterHelper { public ToConverter getToConverter(Class toClass, Class domainClass) throws JTransfoException { ToConverter converter = withPreConverter(toClass); List<SyntheticField> domainFields = reflectionHelper.getSyntheticFields(domainClass); for (Field field : reflectionHelper.getFields(toClass)) { boolean isTransient = Modifier.isTransient(field.getModifiers()); List<NotMapped> notMapped = reflectionHelper.getAnnotationWithMeta(field, NotMapped.class); if (!isTransient && (0 == notMapped.size())) { List<MappedBy> mappedBies = reflectionHelper.getAnnotationWithMeta(field, MappedBy.class); if (mappedBies.size() > 1) { throw new JTransfoException("Field " + field.getName() + " on type " + field.getDeclaringClass().getName() + " MappedBy is ambiguous, check your meta-annotations."); } MappedBy mappedBy = null; if (1 == mappedBies.size()) { mappedBy = mappedBies.get(0); } boolean isStatic = (0 != (field.getModifiers() & Modifier.STATIC)); if (0 != mappedBies.size() || !isStatic) { buildConverters(field, domainFields, domainClass, converter, mappedBy); } } } addPostConverter(converter, toClass); return converter; }  ToConverter getToConverter(Class toClass, Class domainClass);  }
@Test public void testGetToConverter() throws Exception { ReflectionTestUtils.setField(converterHelper, "reflectionHelper", new ReflectionHelper()); ToConverter res = converterHelper.getToConverter(SimpleExtendedTo.class, SimpleExtendedDomain.class); assertThat(res).isNotNull(); assertThat(res.getToTo()).hasSize(4); assertThat(res.getToTo().get(0)).isInstanceOf(ToToConverter.class); assertThat(res.getToDomain()).hasSize(3); assertThat(res.getToDomain().get(0)).isInstanceOf(ToDomainConverter.class); }
@Test public void testGetToConverter_mapOnly() throws Exception { ReflectionTestUtils.setField(converterHelper, "reflectionHelper", new ReflectionHelper()); ((Map<String, TypeConverter>) ReflectionTestUtils.getField(converterHelper, "typeConverterInstances")). put("always2", new DefaultTypeConverter()); ToConverter res = converterHelper.getToConverter(TaggedPersonTo.class, PersonDomain.class); assertThat(res).isNotNull(); assertThat(res.getToTo()).hasSize(4); assertThat(res.getToTo().get(0)).isInstanceOf(TaggedConverter.class); assertThat(res.getToDomain()).hasSize(3); assertThat(res.getToDomain().get(0)).isInstanceOf(TaggedConverter.class); }
@Test public void testGetToConverterTransitive() throws Exception { ReflectionTestUtils.setField(converterHelper, "reflectionHelper", new ReflectionHelper()); ToConverter res = converterHelper.getToConverter(PersonTransitiveTo.class, PersonDomain.class); assertThat(res).isNotNull(); assertThat(res.getToTo()).hasSize(3); assertThat(res.getToDomain()).hasSize(3); }
@Test public void testGetToConverterInvalidMapping() throws Exception { ReflectionTestUtils.setField(converterHelper, "reflectionHelper", new ReflectionHelper()); exception.expect(JTransfoException.class); exception.expectMessage("Cannot determine mapping for field string in class " + "org.jtransfo.object.FaultyExtendedTo. The field zzz in class " + "org.jtransfo.object.SimpleExtendedDomain cannot be found."); converterHelper.getToConverter(FaultyExtendedTo.class, SimpleExtendedDomain.class); }