1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| package org.picocontainer.alternatives; |
11 |
| |
12 |
| import org.picocontainer.ComponentAdapter; |
13 |
| import org.picocontainer.PicoContainer; |
14 |
| import org.picocontainer.PicoInitializationException; |
15 |
| import org.picocontainer.PicoIntrospectionException; |
16 |
| import org.picocontainer.defaults.AssignabilityRegistrationException; |
17 |
| import org.picocontainer.defaults.DecoratingComponentAdapter; |
18 |
| import org.picocontainer.defaults.NotConcreteRegistrationException; |
19 |
| |
20 |
| import java.lang.reflect.InvocationHandler; |
21 |
| import java.lang.reflect.InvocationTargetException; |
22 |
| import java.lang.reflect.Method; |
23 |
| import java.lang.reflect.Proxy; |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| |
39 |
| public class ImplementationHidingComponentAdapter extends DecoratingComponentAdapter { |
40 |
| private final boolean strict; |
41 |
| |
42 |
112
| public ImplementationHidingComponentAdapter(ComponentAdapter delegate, boolean strict) {
|
43 |
112
| super(delegate);
|
44 |
112
| this.strict = strict;
|
45 |
| } |
46 |
| |
47 |
98
| public Object getComponentInstance(final PicoContainer container)
|
48 |
| throws PicoInitializationException, PicoIntrospectionException, AssignabilityRegistrationException, NotConcreteRegistrationException { |
49 |
| |
50 |
98
| Object componentKey = getDelegate().getComponentKey();
|
51 |
98
| Class[] classes = null;
|
52 |
98
| if (componentKey instanceof Class && ((Class) getDelegate().getComponentKey()).isInterface()) {
|
53 |
54
| classes = new Class[]{(Class) getDelegate().getComponentKey()};
|
54 |
44
| } else if (componentKey instanceof Class[]) {
|
55 |
4
| classes = (Class[]) componentKey;
|
56 |
| } else { |
57 |
40
| if(strict) {
|
58 |
2
| throw new PicoIntrospectionException("In strict mode, " + getClass().getName() + " only allows components registered with interface keys (java.lang.Class or java.lang.Class[])");
|
59 |
| } |
60 |
38
| return getDelegate().getComponentInstance(container);
|
61 |
| } |
62 |
| |
63 |
58
| Class[] interfaces = verifyInterfacesOnly(classes);
|
64 |
56
| return createProxy(interfaces, container);
|
65 |
| } |
66 |
| |
67 |
56
| private Object createProxy(Class[] interfaces, final PicoContainer container) {
|
68 |
56
| return Proxy.newProxyInstance(getClass().getClassLoader(),
|
69 |
| interfaces, new InvocationHandler() { |
70 |
24
| public Object invoke(final Object proxy, final Method method,
|
71 |
| final Object[] args) |
72 |
| throws Throwable { |
73 |
24
| try {
|
74 |
24
| Object componentInstance = getDelegate().getComponentInstance(container);
|
75 |
24
| return method.invoke(componentInstance, args);
|
76 |
| } catch (final InvocationTargetException ite) { |
77 |
6
| throw ite.getTargetException();
|
78 |
| } |
79 |
| } |
80 |
| }); |
81 |
| } |
82 |
| |
83 |
58
| private Class[] verifyInterfacesOnly(Class[] classes) {
|
84 |
58
| for (int i = 0; i < classes.length; i++) {
|
85 |
60
| if(!classes[i].isInterface()) {
|
86 |
2
| throw new PicoIntrospectionException("Class keys must be interfaces. " + classes[i] + " is not an interface.");
|
87 |
| } |
88 |
| } |
89 |
56
| return classes;
|
90 |
| } |
91 |
| |
92 |
| |
93 |
| |
94 |
| |
95 |
| |
96 |
| |
97 |
| |
98 |
| |
99 |
| |
100 |
| |
101 |
| |
102 |
| |
103 |
| |
104 |
| |
105 |
| |
106 |
| |
107 |
| |
108 |
| |
109 |
| |
110 |
| |
111 |
| |
112 |
| |
113 |
| |
114 |
| |
115 |
| |
116 |
| |
117 |
| |
118 |
| |
119 |
| |
120 |
| |
121 |
| |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
| |
127 |
| |
128 |
| |
129 |
| } |