Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package com.google.inject.slf4j;
 
 import  com.google.inject.ProvisionException;
 import  com.google.inject.TypeLiteral;
 import  com.google.inject.spi.InjectionListener;
 import  com.google.inject.spi.TypeEncounter;
 import  com.google.inject.spi.TypeListener;

Author(s):
dhanji@gmail.com (Dhanji R. Prasanna)
class Slf4jInjectionTypeListener implements TypeListener {
  public <I> void hear(final TypeLiteral<I> type, TypeEncounter<I> encounter) {
    for (final Field field : type.getRawType().getDeclaredFields()) {
      Class<?> typeOfField = field.getType();
      if (Logger.class.isAssignableFrom(typeOfField)) {
        encounter.register(new InjectionListener<I>() {
          @Override
          public void afterInjection(I injectee) {
            try {
              field.set(injectee, LoggerFactory.getLogger(type.getRawType()));
            } catch (IllegalAccessException e) {
              throw new ProvisionException("Unable to inject SLF4J logger"e);
            }
          }
        });
      }
    }
  }
New to GrepCode? Check out our FAQ X