JavaCompilerFieldReaderFactory implements FieldReaderFactory { @Override public FieldReader apply(Field field) { Class<?> declaringClass = field.getDeclaringClass(); Optional<String> methodName = getMethodName(declaringClass, field); FieldReader fieldReader = methodName.map(compile(declaringClass)) .orElseGet(() -> fallback.apply(field)); return fieldReader; } JavaCompilerFieldReaderFactory(JavaCompilerFacade compilerFacade, Reflections reflections, FieldReaderFactory fallback); @Override FieldReader apply(Field field);  }
@Test public void shouldCreateFieldReader() { Foo foo = new Foo(); foo.setBar("bar"); FieldReader fieldReader = factory.apply(Foo.class.getDeclaredFields()[0]); Assertions.assertNotNull(fieldReader); Object value = fieldReader.read(foo); Assertions.assertEquals("bar", value); }
@Test public void shouldUseFallBackWhenThereIsNotGetter() { Foo foo = new Foo(); Field field = Foo.class.getDeclaredFields()[1]; field.setAccessible(true); FieldReader fieldReader = factory.apply(field); Assertions.assertNotNull(fieldReader); Object value = fieldReader.read(foo); Assertions.assertEquals("bar2", value); }
@Test public void shouldUseFallBackWhenGetterIsNotPublic() { Foo foo = new Foo(); Field field = Foo.class.getDeclaredFields()[2]; field.setAccessible(true); FieldReader fieldReader = factory.apply(field); Assertions.assertNotNull(fieldReader); Object value = fieldReader.read(foo); Assertions.assertEquals("bar3", value); }