package org.rocket.guice;

import com.google.inject.Binder;
import com.google.inject.Module;
import com.typesafe.config.Config;
import org.rocket.InjectConfig;

import java.lang.reflect.Field;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.google.inject.matcher.Matchers.any;
import static org.rocket.guice.GuiceDSL.listener;
import static org.rocket.guice.GuiceDSL.members;

public final class ConfigModule implements Module {
	private final Config config;

	private ConfigModule(Config config) {
		this.config = Objects.requireNonNull(config, "config");
	}

	public static ConfigModule of(Config config) {
		return new ConfigModule(config);
	}

	@Override
	public void configure(Binder binder) {
		binder.bindListener(any(), listener((type, encounter) -> {
			List<Injectee> injectees = Stream.of(type.getRawType().getDeclaredFields())
				.filter(x -> x.isAnnotationPresent(InjectConfig.class))
				.peek(x -> x.setAccessible(true))
				.map(x -> new Injectee(x, x.getAnnotation(InjectConfig.class).value()))
				.collect(Collectors.toList());

			encounter.register(members(instance -> {
				for (Injectee injectee : injectees) {
					try {
						injectee.field.set(instance, config.getAnyRef(injectee.key));
					} catch (IllegalAccessException e) {
						binder.addError(e);
					}
				}
			}));
		}));
	}

	static class Injectee {
		final Field field;
		final String key;

		Injectee(Field field, String key) {
			this.field = Objects.requireNonNull(field, "field");
			this.key = Objects.requireNonNull(key, "key");
		}
	}
}

@@ -49,24 +49,66 @@
 ect.Binder;%0A
+import com.google.inject.MembersInjector;%0A
 import com.g
@@ -128,16 +128,16 @@
 Module;%0A
-
 import c
@@ -453,57 +453,8 @@
 ner;
-%0Aimport static org.rocket.guice.GuiceDSL.members;
 %0A%0Apu
@@ -1127,111 +1127,316 @@
 );%0A%0A
-%09%09%09encounter.register(members(instance -%3E %7B%0A%09%09%09%09for (Injectee injectee : injectees) %7B%0A%09%09%09%09%09try %7B%0A%09%09%09%09%09%09
+            //noinspection Convert2Lambda%0A            encounter.register(new MembersInjector%3CObject%3E() %7B%0A                @Override%0A                public void injectMembers(Object instance) %7B%0A                    for (Injectee injectee : injectees) %7B%0A                        try %7B%0A                            
 inje
@@ -1493,21 +1493,40 @@
 .key));%0A
-%09%09%09%09%09
+                        
 %7D catch 
@@ -1558,52 +1558,135 @@
 ) %7B%0A
-%09%09%09%09%09%09binder.addError(e);%0A%09%09%09%09%09%7D%0A%09%09%09%09%7D%0A%09%09%09%7D)
+                            binder.addError(e);%0A                        %7D%0A                    %7D%0A                %7D%0A            %7D
 );%0A%09

fix build