1 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 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 public Set getInterfaces() 49 { 50 return _interfaces; 51 } 52 53 public Set getNonInterfaceMethodSignatures() 54 { 55 Set result = new HashSet (_allMethods); 56 57 result.removeAll(_interfaceMethods); 58 59 return result; 60 } 61 62 public void processInterfaceQueue() 63 { 64 while (!_interfaceQueue.isEmpty()) 65 { 66 Class interfaceClass = (Class ) _interfaceQueue.remove(0); 67 68 processInterface(interfaceClass); 69 } 70 } 71 72 private void processInterface(Class interfaceClass) 73 { 74 Class [] interfaces = interfaceClass.getInterfaces(); 75 76 for (int i = 0; i < interfaces.length; i++) 77 addInterfaceToQueue(interfaces[i]); 78 79 Method [] 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 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 beanClass) 99 { 100 Class [] interfaces = beanClass.getInterfaces(); 101 102 for (int i = 0; i < interfaces.length; i++) 103 addInterfaceToQueue(interfaces[i]); 104 105 Method [] methods = beanClass.getDeclaredMethods(); 106 107 for (int i = 0; i < methods.length; i++) 108 { 109 Method 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 synthesizeInterface(Class beanClass) 124 { 125 Operation op = new Operation(); 126 127 explodeClass(beanClass, op); 128 129 return createInterface(beanClass, op); 130 } 131 132 void explodeClass(Class beanClass, Operation op) 133 { 134 Class current = beanClass; 135 136 while (current != Object .class) 137 { 138 op.processClass(current); 139 140 current = current.getSuperclass(); 141 } 142 143 op.processInterfaceQueue(); 144 } 145 146 Class createInterface(Class beanClass, Operation op) 147 { 148 String name = ClassFabUtils.generateClassName(beanClass); 149 150 return createInterface(name, op); 151 } 152 153 private Class createInterface(String name, Operation op) 154 { 155 InterfaceFab fab = _classFactory.newInterface(name); 156 157 Iterator i = op.getInterfaces().iterator(); 158 while (i.hasNext()) 159 { 160 Class interfaceClass = (Class ) 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 |