1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.hivemind.service.impl; |
16 |
| |
17 |
| import java.lang.reflect.Method; |
18 |
| import java.lang.reflect.Modifier; |
19 |
| import java.util.ArrayList; |
20 |
| import java.util.HashSet; |
21 |
| import java.util.Iterator; |
22 |
| import java.util.List; |
23 |
| import java.util.Set; |
24 |
| |
25 |
| import org.apache.hivemind.service.ClassFabUtils; |
26 |
| import org.apache.hivemind.service.ClassFactory; |
27 |
| import org.apache.hivemind.service.InterfaceFab; |
28 |
| import org.apache.hivemind.service.InterfaceSynthesizer; |
29 |
| import org.apache.hivemind.service.MethodSignature; |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| public class InterfaceSynthesizerImpl implements InterfaceSynthesizer |
35 |
| { |
36 |
| private ClassFactory _classFactory; |
37 |
| |
38 |
| private static class Operation |
39 |
| { |
40 |
| private Set _interfaces = new HashSet(); |
41 |
| |
42 |
| private Set _interfaceMethods = new HashSet(); |
43 |
| |
44 |
| private Set _allMethods = new HashSet(); |
45 |
| |
46 |
| private List _interfaceQueue = new ArrayList(); |
47 |
| |
48 |
15
| public Set getInterfaces()
|
49 |
| { |
50 |
15
| return _interfaces;
|
51 |
| } |
52 |
| |
53 |
15
| public Set getNonInterfaceMethodSignatures()
|
54 |
| { |
55 |
15
| Set result = new HashSet(_allMethods);
|
56 |
| |
57 |
15
| result.removeAll(_interfaceMethods);
|
58 |
| |
59 |
15
| return result;
|
60 |
| } |
61 |
| |
62 |
15
| public void processInterfaceQueue()
|
63 |
| { |
64 |
15
| while (!_interfaceQueue.isEmpty())
|
65 |
| { |
66 |
12
| Class interfaceClass = (Class) _interfaceQueue.remove(0);
|
67 |
| |
68 |
12
| processInterface(interfaceClass);
|
69 |
| } |
70 |
| } |
71 |
| |
72 |
12
| private void processInterface(Class interfaceClass)
|
73 |
| { |
74 |
12
| Class[] interfaces = interfaceClass.getInterfaces();
|
75 |
| |
76 |
12
| for (int i = 0; i < interfaces.length; i++)
|
77 |
5
| addInterfaceToQueue(interfaces[i]);
|
78 |
| |
79 |
12
| Method[] methods = interfaceClass.getDeclaredMethods();
|
80 |
| |
81 |
12
| for (int i = 0; i < methods.length; i++)
|
82 |
| { |
83 |
8
| MethodSignature sig = new MethodSignature(methods[i]);
|
84 |
| |
85 |
8
| _interfaceMethods.add(sig);
|
86 |
| } |
87 |
| } |
88 |
| |
89 |
13
| private void addInterfaceToQueue(Class interfaceClass)
|
90 |
| { |
91 |
13
| if (_interfaces.contains(interfaceClass))
|
92 |
1
| return;
|
93 |
| |
94 |
12
| _interfaces.add(interfaceClass);
|
95 |
12
| _interfaceQueue.add(interfaceClass);
|
96 |
| } |
97 |
| |
98 |
16
| public void processClass(Class beanClass)
|
99 |
| { |
100 |
16
| Class[] interfaces = beanClass.getInterfaces();
|
101 |
| |
102 |
16
| for (int i = 0; i < interfaces.length; i++)
|
103 |
8
| addInterfaceToQueue(interfaces[i]);
|
104 |
| |
105 |
16
| Method[] methods = beanClass.getDeclaredMethods();
|
106 |
| |
107 |
16
| for (int i = 0; i < methods.length; i++)
|
108 |
| { |
109 |
28
| Method m = methods[i];
|
110 |
28
| int modifiers = m.getModifiers();
|
111 |
| |
112 |
28
| if (Modifier.isStatic(modifiers) || !Modifier.isPublic(modifiers))
|
113 |
3
| continue;
|
114 |
| |
115 |
25
| MethodSignature sig = new MethodSignature(m);
|
116 |
| |
117 |
25
| _allMethods.add(sig);
|
118 |
| } |
119 |
| } |
120 |
| |
121 |
| } |
122 |
| |
123 |
15
| public Class synthesizeInterface(Class beanClass)
|
124 |
| { |
125 |
15
| Operation op = new Operation();
|
126 |
| |
127 |
15
| explodeClass(beanClass, op);
|
128 |
| |
129 |
15
| return createInterface(beanClass, op);
|
130 |
| } |
131 |
| |
132 |
15
| void explodeClass(Class beanClass, Operation op)
|
133 |
| { |
134 |
15
| Class current = beanClass;
|
135 |
| |
136 |
15
| while (current != Object.class)
|
137 |
| { |
138 |
16
| op.processClass(current);
|
139 |
| |
140 |
16
| current = current.getSuperclass();
|
141 |
| } |
142 |
| |
143 |
15
| op.processInterfaceQueue();
|
144 |
| } |
145 |
| |
146 |
15
| Class createInterface(Class beanClass, Operation op)
|
147 |
| { |
148 |
15
| String name = ClassFabUtils.generateClassName(beanClass);
|
149 |
| |
150 |
15
| return createInterface(name, op);
|
151 |
| } |
152 |
| |
153 |
15
| private Class createInterface(String name, Operation op)
|
154 |
| { |
155 |
15
| InterfaceFab fab = _classFactory.newInterface(name);
|
156 |
| |
157 |
15
| Iterator i = op.getInterfaces().iterator();
|
158 |
15
| while (i.hasNext())
|
159 |
| { |
160 |
12
| Class interfaceClass = (Class) i.next();
|
161 |
| |
162 |
12
| fab.addInterface(interfaceClass);
|
163 |
| } |
164 |
| |
165 |
15
| i = op.getNonInterfaceMethodSignatures().iterator();
|
166 |
15
| while (i.hasNext())
|
167 |
| { |
168 |
17
| MethodSignature sig = (MethodSignature) i.next();
|
169 |
| |
170 |
17
| fab.addMethod(sig);
|
171 |
| } |
172 |
| |
173 |
15
| return fab.createInterface();
|
174 |
| } |
175 |
| |
176 |
14
| public void setClassFactory(ClassFactory classFactory)
|
177 |
| { |
178 |
14
| _classFactory = classFactory;
|
179 |
| } |
180 |
| } |