1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
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 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
|
39 |
|
|
40 |
|
@version |
41 |
|
@since |
42 |
|
|
|
|
| 92.9% |
Uncovered Elements: 1 (14) |
Complexity: 4 |
Complexity Density: 0.4 |
|
43 |
|
public class Slf4jInjectionTypeListener implements TypeListener |
44 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
45 |
6 |
@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 |
|
{ |
|
|
| 83.3% |
Uncovered Elements: 1 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
53 |
2 |
@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 |
|
} |