package com.github.vbauer.herald.ext.guice;

import com.github.vbauer.herald.util.LoggerInjector;
import com.google.inject.AbstractModule;
import com.google.inject.TypeLiteral;
import com.google.inject.matcher.Matcher;
import com.google.inject.matcher.Matchers;
import com.google.inject.spi.InjectionListener;
import com.google.inject.spi.TypeEncounter;
import com.google.inject.spi.TypeListener;

/**
 * @author Vladislav Bauer
 */

public class LogModule extends AbstractModule {

    private final Matcher<Object> typeMatcher;


    public LogModule() {
        this(Matchers.any());
    }

    public LogModule(final Matcher<Object> typeMatcher) {
        this.typeMatcher = typeMatcher;
    }


    @Override
    protected void configure() {
        bindListener(typeMatcher, createTypeListener());
    }


    private TypeListener createTypeListener() {
        return new TypeListener() {
            @Override
            public <I> void hear(final TypeLiteral<I> typeLiteral, final TypeEncounter<I> typeEncounter) {
                typeEncounter.register(new InjectionListener<I>() {

                    @Override
                    public void afterInjection(final I injectee) {
                        LoggerInjector.inject(injectee);
                    }
                });
            }
        };
    }

}

@@ -472,24 +472,124 @@
 ctModule %7B%0A%0A
+    private static final InjectionListener%3CObject%3E INJECTION_LISTENER = createInjectionListener();%0A%0A
     private 
@@ -1161,46 +1161,142 @@
 ter(
-new InjectionListener%3CI%3E
+INJECTION_LISTENER);%0A            %7D%0A        %7D;%0A    %7D%0A%0A    private static InjectionListener%3CObject%3E createInjectionListener
 () %7B%0A
-%0A
         
     
@@ -1283,32 +1283,73 @@
 ner() %7B%0A        
+return new InjectionListener%3CObject%3E() %7B%0A
             @Ove
@@ -1350,32 +1350,24 @@
   @Override%0A
-        
             
@@ -1403,26 +1403,23 @@
 nal 
-I injectee) %7B%0A    
+Object bean) %7B%0A
     
@@ -1430,20 +1430,16 @@
         
-    
 LoggerIn
@@ -1456,58 +1456,12 @@
 ect(
-injectee);%0A                    %7D%0A                %7D
+bean
 );%0A 

Optimize Guice's LogModule class