ConverterHelper { SyntheticField[] findField(List<SyntheticField> domainFields, String fieldName, String[] path, Class<?> type, boolean readOnlyField) throws JTransfoException { List<SyntheticField> fields = domainFields; SyntheticField[] result = new SyntheticField[path.length + 1]; int index = 0; Class<?> currentType = type; for (; index < path.length; index++) { boolean found = false; for (SyntheticField field : fields) { if (field.getName().equals(path[index])) { found = true; result[index] = field; break; } } if (!found) { result[index] = new AccessorSyntheticField(reflectionHelper, currentType, path[index], readOnlyField); } currentType = result[index].getType(); fields = reflectionHelper.getSyntheticFields(currentType); } for (SyntheticField field : fields) { if (field.getName().equals(fieldName)) { result[index] = field; return result; } } result[index] = new AccessorSyntheticField(reflectionHelper, currentType, fieldName, readOnlyField); return result; }  ToConverter getToConverter(Class toClass, Class domainClass);  }
@Test public void testFindField() throws Exception { List<SyntheticField> fields = new ArrayList<>(); SyntheticField f1 = new SimpleSyntheticField(SimpleExtendedDomain.class.getDeclaredField("b")); SyntheticField f2 = new SimpleSyntheticField(SimpleExtendedDomain.class.getDeclaredField("c")); fields.add(f1); fields.add(f2); SyntheticField[] res; res = converterHelper.findField(fields, "c", new String[0], SimpleExtendedDomain.class, false); assertThat(res[0]).isEqualTo(f2); exception.expect(JTransfoException.class); exception.expectMessage("Cannot find getter from [getBla, bla, isBla, hasBla] on class " + "org.jtransfo.object.SimpleExtendedDomain."); converterHelper.findField(fields, "bla", new String[0], SimpleExtendedDomain.class, false); }
@Test public void testFindFieldTransitive() throws Exception { ReflectionHelper reflectionHelper = mock(ReflectionHelper.class); ReflectionTestUtils.setField(converterHelper, "reflectionHelper", reflectionHelper); List<SyntheticField> fields = new ArrayList<>(); SyntheticField f1 = new SimpleSyntheticField(PersonDomain.class.getDeclaredField("name")); SyntheticField f2 = new SimpleSyntheticField(PersonDomain.class.getDeclaredField("address")); fields.add(f1); fields.add(f2); SyntheticField f3 = new SimpleSyntheticField(AddressDomain.class.getDeclaredField("id")); when(reflectionHelper.getSyntheticFields(any(Class.class))).thenReturn(Collections.singletonList(f3)); SyntheticField[] res; res = converterHelper.findField(fields, "id", new String[] { "address" }, PersonDomain.class, false); assertThat(res).hasSize(2); assertThat(res[0]).isEqualTo(f2); assertThat(res[1]).isEqualTo(f3); exception.expect(JTransfoException.class); exception.expectMessage("Cannot find getter from [getBla, bla, isBla, hasBla] on " + "class org.jtransfo.object.PersonDomain."); converterHelper.findField(fields, "id", new String[]{"bla"}, PersonDomain.class, false); }