JavaCompilerFieldWriterFactory implements FieldWriterFactory { @Override public FieldWriter apply(Field field) { Class<?> declaringClass = field.getDeclaringClass(); Optional<String> methodName = getMethodName(declaringClass, field); FieldWriter fieldWriter = methodName.map(compile(declaringClass, field.getType())) .orElseGet(() -> fallback.apply(field)); return fieldWriter; } JavaCompilerFieldWriterFactory(JavaCompilerFacade compilerFacade, Reflections reflections, FieldWriterFactory fallback); @Override FieldWriter apply(Field field);  }
@Test public void shouldCreateFieldWriter() { Foo foo = new Foo(); foo.setBar("bar"); FieldWriter writer = factory.apply(Foo.class.getDeclaredFields()[0]); Assertions.assertNotNull(writer); writer.write(foo, "bar"); Assertions.assertEquals("bar", foo.getBar()); }
@Test public void shouldUseFallBackWhenThereIsNotSetter() throws IllegalAccessException { Foo foo = new Foo(); Field field = Foo.class.getDeclaredFields()[1]; field.setAccessible(true); FieldWriter writer = factory.apply(field); Assertions.assertNotNull(writer); writer.write(foo, "update"); Assertions.assertEquals("update", field.get(foo)); }
@Test public void shouldUseFallBackWhenGetterIsNotPublic() throws IllegalAccessException { Foo foo = new Foo(); Field field = Foo.class.getDeclaredFields()[2]; field.setAccessible(true); FieldWriter writer = factory.apply(field); Assertions.assertNotNull(writer); writer.write(foo, "update"); Assertions.assertEquals("update", field.get(foo)); }