001: // Copyright 2005 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.hivemind.service.impl;
016:
017: import java.lang.reflect.Method;
018: import java.lang.reflect.Modifier;
019: import java.util.ArrayList;
020: import java.util.HashSet;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Set;
024:
025: import org.apache.hivemind.service.ClassFabUtils;
026: import org.apache.hivemind.service.ClassFactory;
027: import org.apache.hivemind.service.InterfaceFab;
028: import org.apache.hivemind.service.InterfaceSynthesizer;
029: import org.apache.hivemind.service.MethodSignature;
030:
031: /**
032: * @author Howard M. Lewis Ship
033: */
034: public class InterfaceSynthesizerImpl implements InterfaceSynthesizer {
035: private ClassFactory _classFactory;
036:
037: private static class Operation {
038: private Set _interfaces = new HashSet();
039:
040: private Set _interfaceMethods = new HashSet();
041:
042: private Set _allMethods = new HashSet();
043:
044: private List _interfaceQueue = new ArrayList();
045:
046: public Set getInterfaces() {
047: return _interfaces;
048: }
049:
050: public Set getNonInterfaceMethodSignatures() {
051: Set result = new HashSet(_allMethods);
052:
053: result.removeAll(_interfaceMethods);
054:
055: return result;
056: }
057:
058: public void processInterfaceQueue() {
059: while (!_interfaceQueue.isEmpty()) {
060: Class interfaceClass = (Class) _interfaceQueue
061: .remove(0);
062:
063: processInterface(interfaceClass);
064: }
065: }
066:
067: private void processInterface(Class interfaceClass) {
068: Class[] interfaces = interfaceClass.getInterfaces();
069:
070: for (int i = 0; i < interfaces.length; i++)
071: addInterfaceToQueue(interfaces[i]);
072:
073: Method[] methods = interfaceClass.getDeclaredMethods();
074:
075: for (int i = 0; i < methods.length; i++) {
076: MethodSignature sig = new MethodSignature(methods[i]);
077:
078: _interfaceMethods.add(sig);
079: }
080: }
081:
082: private void addInterfaceToQueue(Class interfaceClass) {
083: if (_interfaces.contains(interfaceClass))
084: return;
085:
086: _interfaces.add(interfaceClass);
087: _interfaceQueue.add(interfaceClass);
088: }
089:
090: public void processClass(Class beanClass) {
091: Class[] interfaces = beanClass.getInterfaces();
092:
093: for (int i = 0; i < interfaces.length; i++)
094: addInterfaceToQueue(interfaces[i]);
095:
096: Method[] methods = beanClass.getDeclaredMethods();
097:
098: for (int i = 0; i < methods.length; i++) {
099: Method m = methods[i];
100: int modifiers = m.getModifiers();
101:
102: if (Modifier.isStatic(modifiers)
103: || !Modifier.isPublic(modifiers))
104: continue;
105:
106: MethodSignature sig = new MethodSignature(m);
107:
108: _allMethods.add(sig);
109: }
110: }
111:
112: }
113:
114: public Class synthesizeInterface(Class beanClass) {
115: Operation op = new Operation();
116:
117: explodeClass(beanClass, op);
118:
119: return createInterface(beanClass, op);
120: }
121:
122: void explodeClass(Class beanClass, Operation op) {
123: Class current = beanClass;
124:
125: while (current != Object.class) {
126: op.processClass(current);
127:
128: current = current.getSuperclass();
129: }
130:
131: op.processInterfaceQueue();
132: }
133:
134: Class createInterface(Class beanClass, Operation op) {
135: String name = ClassFabUtils.generateClassName(beanClass);
136:
137: return createInterface(name, op);
138: }
139:
140: private Class createInterface(String name, Operation op) {
141: InterfaceFab fab = _classFactory.newInterface(name);
142:
143: Iterator i = op.getInterfaces().iterator();
144: while (i.hasNext()) {
145: Class interfaceClass = (Class) i.next();
146:
147: fab.addInterface(interfaceClass);
148: }
149:
150: i = op.getNonInterfaceMethodSignatures().iterator();
151: while (i.hasNext()) {
152: MethodSignature sig = (MethodSignature) i.next();
153:
154: fab.addMethod(sig);
155: }
156:
157: return fab.createInterface();
158: }
159:
160: public void setClassFactory(ClassFactory classFactory) {
161: _classFactory = classFactory;
162: }
163: }
|