1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.apache.commons.inject.impl; |
18 | |
|
19 | |
import org.apache.commons.inject.api.IBinding; |
20 | |
import org.apache.commons.inject.api.IInjector; |
21 | |
import org.apache.commons.inject.api.IKey; |
22 | |
import org.apache.commons.inject.api.Key; |
23 | |
import org.apache.commons.inject.api.NoSuchBindingException; |
24 | |
|
25 | |
|
26 | |
|
27 | |
|
28 | 9 | public abstract class AbstractInjector implements IInjector { |
29 | |
protected abstract <T> IBinding<T> getBinding(IKey<T> pKey); |
30 | |
protected abstract <T> IBinding<T> requireBinding(IKey<T> pKey); |
31 | |
|
32 | |
@Override |
33 | |
public <T> T getInstance(Class<T> pType) { |
34 | 1 | final IKey<T> key = new Key<T>(pType); |
35 | 1 | return getInstance(key); |
36 | |
} |
37 | |
|
38 | |
@Override |
39 | |
public <T> T getInstance(Class<T> pType, String pName) { |
40 | 4 | final IKey<T> key = new Key<T>(pType, pName); |
41 | 4 | return getInstance(key); |
42 | |
} |
43 | |
|
44 | |
@Override |
45 | |
public <T> T getInstance(IKey<T> pKey) { |
46 | 5 | final IBinding<T> binding = getBinding(pKey); |
47 | 5 | if (binding == null) { |
48 | 0 | return null; |
49 | |
} else { |
50 | 5 | return binding.getProvider().get(); |
51 | |
} |
52 | |
} |
53 | |
|
54 | |
@Override |
55 | |
public <T> T requireInstance(Class<T> pType) throws NoSuchBindingException { |
56 | 6 | final IKey<T> key = new Key<T>(pType); |
57 | 6 | return requireInstance(key); |
58 | |
} |
59 | |
|
60 | |
@Override |
61 | |
public <T> T requireInstance(Class<T> pType, String pName) |
62 | |
throws NoSuchBindingException { |
63 | 15 | final IKey<T> key = new Key<T>(pType, pName); |
64 | 15 | return requireInstance(key); |
65 | |
} |
66 | |
|
67 | |
@Override |
68 | |
public <T> T requireInstance(IKey<T> pKey) throws NoSuchBindingException { |
69 | 22 | final IBinding<T> binding = getBinding(pKey); |
70 | 22 | if (binding == null) { |
71 | 3 | throw new NoSuchBindingException("No binding registered for key: " + pKey); |
72 | |
} else { |
73 | 19 | return binding.getProvider().get(); |
74 | |
} |
75 | |
} |
76 | |
|
77 | |
@Override |
78 | |
public void injectMembers(Object pInstance) { |
79 | 0 | if (pInstance == null) { |
80 | 0 | throw new NullPointerException("The instance must not be null."); |
81 | |
} |
82 | |
@SuppressWarnings("unchecked") |
83 | 0 | final Class<Object> cl = (Class<Object>) pInstance.getClass(); |
84 | 0 | final IKey<Object> key = new Key<Object>(cl); |
85 | 0 | IBinding<Object> binding = requireBinding(key); |
86 | 0 | binding.getPoint().injectTo(pInstance, this); |
87 | 0 | } |
88 | |
} |