1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.component.guice

File Slf4jInjectionTypeListener.java

 

Coverage histogram

../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

2
10
2
1
70
37
4
0.4
5
2
2

Classes

Class Line # Actions
Slf4jInjectionTypeListener 43 10 0% 4 1
0.928571492.9%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.component.guice;
21   
22    import java.lang.reflect.Field;
23   
24    import org.slf4j.Logger;
25    import org.slf4j.LoggerFactory;
26   
27    import com.google.inject.ProvisionException;
28    import com.google.inject.TypeLiteral;
29    import com.google.inject.spi.InjectionListener;
30    import com.google.inject.spi.TypeEncounter;
31    import com.google.inject.spi.TypeListener;
32   
33    /**
34    * Add support for injecting SLF4J Loggers.
35    * <p>
36    * Note that the original source for this implementation (we've modified it) came from the <a
37    * href="http://code.google.com/p/google-sitebricks/">Sitebricks</a> project and was under an Apache License v2.0.
38    * </p>
39    *
40    * @version $Id: a93ada15953d99f5ac379818f00c8e03bcabd371 $
41    * @since 4.0RC1
42    */
 
43    public class Slf4jInjectionTypeListener implements TypeListener
44    {
 
45  6 toggle @Override
46    public <I> void hear(final TypeLiteral<I> type, TypeEncounter<I> encounter)
47    {
48  6 for (final Field field : type.getRawType().getDeclaredFields()) {
49  18 Class<?> typeOfField = field.getType();
50  18 if (Logger.class.isAssignableFrom(typeOfField)) {
51  2 encounter.register(new InjectionListener<I>()
52    {
 
53  2 toggle @Override
54    public void afterInjection(I injectee)
55    {
56  2 boolean isAccessible = field.isAccessible();
57  2 field.setAccessible(true);
58  2 try {
59  2 field.set(injectee, LoggerFactory.getLogger(type.getRawType()));
60    } catch (IllegalAccessException e) {
61  0 throw new ProvisionException("Unable to inject SLF4J logger", e);
62    } finally {
63  2 field.setAccessible(isAccessible);
64    }
65    }
66    });
67    }
68    }
69    }
70    }