1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| package org.picocontainer.defaults; |
11 |
| |
12 |
| import org.picocontainer.Parameter; |
13 |
| import org.picocontainer.PicoContainer; |
14 |
| import org.picocontainer.PicoIntrospectionException; |
15 |
| import org.picocontainer.PicoVisitor; |
16 |
| |
17 |
| import java.lang.reflect.Constructor; |
18 |
| import java.lang.reflect.InvocationTargetException; |
19 |
| import java.lang.reflect.Modifier; |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| public abstract class InstantiatingComponentAdapter extends AbstractComponentAdapter { |
34 |
| |
35 |
| protected transient Guard verifyingGuard; |
36 |
| |
37 |
| protected transient Parameter[] parameters; |
38 |
| |
39 |
| protected boolean allowNonPublicClasses; |
40 |
| |
41 |
| protected static abstract class Guard extends ThreadLocalCyclicDependencyGuard { |
42 |
| protected PicoContainer guardedContainer; |
43 |
124
| protected void setArguments(PicoContainer container) {
|
44 |
124
| this.guardedContainer = container;
|
45 |
| } |
46 |
| } |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
| |
53 |
| |
54 |
| |
55 |
| |
56 |
| |
57 |
1208
| protected InstantiatingComponentAdapter(Object componentKey, Class componentImplementation, Parameter[] parameters, boolean allowNonPublicClasses) {
|
58 |
1208
| super(componentKey, componentImplementation);
|
59 |
1208
| checkConcrete();
|
60 |
| |
61 |
1204
| this.parameters = parameters;
|
62 |
1204
| this.allowNonPublicClasses = allowNonPublicClasses;
|
63 |
| } |
64 |
| |
65 |
1208
| private void checkConcrete() throws NotConcreteRegistrationException {
|
66 |
| |
67 |
1208
| boolean isAbstract = (getComponentImplementation().getModifiers() & Modifier.ABSTRACT) == Modifier.ABSTRACT;
|
68 |
1208
| if (getComponentImplementation().isInterface() || isAbstract) {
|
69 |
4
| throw new NotConcreteRegistrationException(getComponentImplementation());
|
70 |
| } |
71 |
| } |
72 |
| |
73 |
| |
74 |
| |
75 |
| |
76 |
| |
77 |
| |
78 |
| |
79 |
2688
| protected Parameter[] createDefaultParameters(Class[] parameters) {
|
80 |
2688
| Parameter[] componentParameters = new Parameter[parameters.length];
|
81 |
2688
| for (int i = 0; i < parameters.length; i++) {
|
82 |
2866
| componentParameters[i] = ComponentParameter.DEFAULT;
|
83 |
| } |
84 |
2688
| return componentParameters;
|
85 |
| } |
86 |
| |
87 |
64
| public void verify(final PicoContainer container) throws PicoIntrospectionException {
|
88 |
64
| if (verifyingGuard == null) {
|
89 |
54
| verifyingGuard = new Guard() {
|
90 |
62
| public Object run() {
|
91 |
62
| final Constructor constructor = getGreediestSatisfiableConstructor(guardedContainer);
|
92 |
34
| final Class[] parameterTypes = constructor.getParameterTypes();
|
93 |
34
| final Parameter[] currentParameters = parameters != null ? parameters : createDefaultParameters(parameterTypes);
|
94 |
34
| for (int i = 0; i < currentParameters.length; i++) {
|
95 |
14
| currentParameters[i].verify(container, InstantiatingComponentAdapter.this, parameterTypes[i]);
|
96 |
| } |
97 |
30
| return null;
|
98 |
| } |
99 |
| }; |
100 |
| } |
101 |
64
| verifyingGuard.setArguments(container);
|
102 |
64
| verifyingGuard.observe(getComponentImplementation());
|
103 |
| } |
104 |
| |
105 |
178
| public void accept(PicoVisitor visitor) {
|
106 |
178
| super.accept(visitor);
|
107 |
178
| if (parameters != null) {
|
108 |
34
| for (int i = 0; i < parameters.length; i++) {
|
109 |
50
| parameters[i].accept(visitor);
|
110 |
| } |
111 |
| } |
112 |
| } |
113 |
| |
114 |
| |
115 |
| |
116 |
| |
117 |
| |
118 |
| |
119 |
| |
120 |
| |
121 |
| |
122 |
| |
123 |
| |
124 |
906
| protected Object newInstance(Constructor constructor, Object[] parameters) throws InstantiationException, IllegalAccessException, InvocationTargetException {
|
125 |
906
| if (allowNonPublicClasses) {
|
126 |
24
| constructor.setAccessible(true);
|
127 |
| } |
128 |
906
| return constructor.newInstance(parameters);
|
129 |
| } |
130 |
| |
131 |
| |
132 |
| |
133 |
| |
134 |
| |
135 |
| |
136 |
| |
137 |
| |
138 |
| |
139 |
| |
140 |
| |
141 |
| |
142 |
| protected abstract Constructor getGreediestSatisfiableConstructor(PicoContainer container) throws PicoIntrospectionException, UnsatisfiableDependenciesException, AmbiguousComponentResolutionException, AssignabilityRegistrationException, NotConcreteRegistrationException; |
143 |
| } |