KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > hivemind > service > impl > InterfaceSynthesizerImpl


1 // Copyright 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.hivemind.service.impl;
16
17 import java.lang.reflect.Method JavaDoc;
18 import java.lang.reflect.Modifier JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.HashSet JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Set JavaDoc;
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  * @author Howard M. Lewis Ship
33  */

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