1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| package org.picocontainer.defaults; |
12 |
| |
13 |
| import org.picocontainer.ComponentAdapter; |
14 |
| import org.picocontainer.Parameter; |
15 |
| import org.picocontainer.PicoContainer; |
16 |
| import org.picocontainer.PicoException; |
17 |
| import org.picocontainer.PicoIntrospectionException; |
18 |
| import org.picocontainer.PicoVisitor; |
19 |
| |
20 |
| import java.io.Serializable; |
21 |
| import java.lang.reflect.Field; |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| public class ConstantParameter |
36 |
| implements Parameter, Serializable { |
37 |
| |
38 |
| private final Object value; |
39 |
| |
40 |
208
| public ConstantParameter(Object value) {
|
41 |
208
| this.value = value;
|
42 |
| } |
43 |
| |
44 |
158
| public Object resolveInstance(PicoContainer container, ComponentAdapter adapter, Class expectedType) {
|
45 |
158
| return value;
|
46 |
| } |
47 |
| |
48 |
178
| public boolean isResolvable(PicoContainer container, ComponentAdapter adapter, Class expectedType) {
|
49 |
178
| try {
|
50 |
178
| verify(container, adapter, expectedType);
|
51 |
130
| return true;
|
52 |
| } catch(final PicoIntrospectionException e) { |
53 |
48
| return false;
|
54 |
| } |
55 |
| } |
56 |
| |
57 |
| |
58 |
| |
59 |
| |
60 |
| |
61 |
| |
62 |
| |
63 |
186
| public void verify(PicoContainer container, ComponentAdapter adapter, Class expectedType) throws PicoException {
|
64 |
186
| if (!checkPrimitive(expectedType) && !expectedType.isInstance(value)) {
|
65 |
48
| throw new PicoIntrospectionException(expectedType.getClass().getName()
|
66 |
| + " is not assignable from " |
67 |
| + value.getClass().getName()); |
68 |
| } |
69 |
| } |
70 |
| |
71 |
| |
72 |
| |
73 |
| |
74 |
| |
75 |
| |
76 |
26
| public void accept(final PicoVisitor visitor) {
|
77 |
26
| visitor.visitParameter(this);
|
78 |
| } |
79 |
| |
80 |
186
| private boolean checkPrimitive(Class expectedType) {
|
81 |
186
| try {
|
82 |
186
| if (expectedType.isPrimitive()) {
|
83 |
28
| final Field field = value.getClass().getField("TYPE");
|
84 |
26
| final Class type = (Class) field.get(value);
|
85 |
26
| return expectedType.isAssignableFrom(type);
|
86 |
| } |
87 |
| } catch (NoSuchFieldException e) { |
88 |
| } catch (IllegalAccessException e) { |
89 |
| } |
90 |
160
| return false;
|
91 |
| } |
92 |
| |
93 |
| } |