Clover icon

sunshower-core

  1. Project Clover database Fri Apr 6 2018 03:27:42 UTC
  2. Package io.sunshower.inject

File Injector.java

 

Coverage histogram

../../../img/srcFileCovDistChart6.png
31% of files have more coverage

Code metrics

22
38
9
2
94
78
22
0.58
4.22
4.5
2.44

Classes

Class Line # Actions
Injector 10 25 11
0.8461538684.6%
Injector.InjectionSite 67 13 11
0.330%
 

Contributing tests

This file is covered by 1 test. .

Source view

1    package io.sunshower.inject;
2   
3    import java.lang.annotation.Annotation;
4    import java.lang.reflect.Field;
5    import java.util.HashMap;
6    import java.util.Map;
7    import java.util.Objects;
8   
9    /** Created by haswell on 3/5/17. */
 
10    public class Injector<T extends Annotation> {
11   
12    private final Class<T> scanType;
13    protected final NameResolver resolver;
14    private final Map<Class<?>, Object> bindings;
15    private final Map<InjectionSite, Object> sites;
16   
 
17  1 toggle public Injector(Class<T> scanType, NameResolver resolver) {
18  1 this.resolver = resolver;
19  1 this.scanType = scanType;
20  1 this.sites = new HashMap<>();
21  1 this.bindings = new HashMap<>();
22    }
23   
 
24  0 toggle public void register(Class<?> boundType, String name, Object instance) {
25  0 this.sites.put(new InjectionSite(boundType, name), instance);
26    }
27   
 
28  1 toggle public void register(Class<?> boundType, Object instance) {
29  1 this.bindings.put(boundType, instance);
30    }
31   
 
32  1 toggle public <U> U inject(Class<U> target, U instance) {
33  1 Objects.requireNonNull(instance);
34  1 Objects.requireNonNull(target);
35  2 for (Class<?> current = target; current != Object.class; current = current.getSuperclass()) {
36  1 doInject(current, target, instance);
37    }
38  1 return instance;
39    }
40   
 
41  1 toggle private <U> void doInject(Class<?> current, Class<U> target, U instance) {
42  1 for (Field field : target.getDeclaredFields()) {
43  2 if (field.isAnnotationPresent(scanType)) {
44  1 Object binding = resolveBinding(field);
45  1 if (binding != null) {
46  1 field.setAccessible(true);
47  1 try {
48  1 field.set(instance, binding);
49    } catch (IllegalAccessException e) {
50  0 throw new InjectionException(current, target, instance, field, binding, e);
51    }
52    }
53    }
54    }
55    }
56   
 
57  1 toggle protected Object resolveBinding(Field field) {
58  1 final String name = resolver.resolve(field);
59  1 final Class<?> ftype = field.getType();
60  1 Object o = sites.get(new InjectionSite(ftype, name));
61  1 if (o != null) {
62  0 return o;
63    }
64  1 return bindings.get(ftype);
65    }
66   
 
67    static final class InjectionSite {
68    final Class<?> type;
69    final String name;
70   
 
71  1 toggle InjectionSite(Class<?> type, String name) {
72  1 this.type = type;
73  1 this.name = name;
74    }
75   
 
76  0 toggle @Override
77    public boolean equals(Object o) {
78  0 if (this == o) return true;
79  0 if (o == null || getClass() != o.getClass()) return false;
80   
81  0 InjectionSite that = (InjectionSite) o;
82   
83  0 if (type != null ? !type.equals(that.type) : that.type != null) return false;
84  0 return name != null ? name.equals(that.name) : that.name == null;
85    }
86   
 
87  1 toggle @Override
88    public int hashCode() {
89  1 int result = type != null ? type.hashCode() : 0;
90  1 result = 31 * result + (name != null ? name.hashCode() : 0);
91  1 return result;
92    }
93    }
94    }