1 package org.apache.beehive.controls.api.events; 2 19 20 import java.lang.reflect.Method ; 21 import java.util.HashMap ; 22 import java.util.WeakHashMap ; 23 24 40 public class EventRef implements java.io.Serializable 41 { 42 static private HashMap <Class ,String > _primToType = new HashMap <Class ,String >(); 46 static 47 { 48 _primToType.put(Integer.TYPE, "I"); 49 _primToType.put(Boolean.TYPE, "Z"); 50 _primToType.put(Byte.TYPE, "B"); 51 _primToType.put(Character.TYPE, "C"); 52 _primToType.put(Short.TYPE, "S"); 53 _primToType.put(Long.TYPE, "J"); 54 _primToType.put(Float.TYPE, "F"); 55 _primToType.put(Double.TYPE, "D"); 56 _primToType.put(Void.TYPE, "V"); 57 } 58 59 64 public EventRef(Method eventMethod) 65 { 66 _method = eventMethod; 67 _descriptor = computeEventDescriptor(eventMethod); 68 } 69 70 99 public EventRef(String eventDescriptor) 100 { 101 _descriptor = eventDescriptor; 102 } 103 104 108 public String getEventDescriptor(Class controlInterface) 109 { 110 if (_descriptor == null) 118 _descriptor = computeEventDescriptor(_method); 119 120 return _descriptor; 121 } 122 123 126 private String computeEventDescriptor(Method method) 127 { 128 StringBuilder sb = new StringBuilder (); 129 130 sb.append(method.getDeclaringClass().getName()); 132 sb.append("."); 133 sb.append(method.getName()); 134 135 Class [] parms = method.getParameterTypes(); 137 sb.append("("); 138 for (int i = 0; i < parms.length; i++) 139 appendTypeDescriptor(sb, parms[i]); 140 sb.append(")"); 141 142 appendTypeDescriptor(sb, method.getReturnType()); 144 145 return sb.toString(); 146 } 147 148 152 private void appendTypeDescriptor(StringBuilder sb, Class clazz) 153 { 154 if (clazz.isPrimitive()) 155 sb.append(_primToType.get(clazz)); 156 else if (clazz.isArray()) 157 sb.append(clazz.getName().replace('.','/')); 158 else 159 { 160 sb.append("L"); 161 sb.append(clazz.getName().replace('.','/')); 162 sb.append(";"); 163 } 164 } 165 166 169 public Method getEventMethod(Class controlInterface) 170 { 171 if (_method != null && 176 _method.getDeclaringClass().getClassLoader().equals(controlInterface.getClassLoader())) 177 return _method; 178 179 String eventDescriptor = getEventDescriptor(controlInterface); 184 HashMap <String ,Method > descriptorMap = getDescriptorMap(controlInterface); 185 if (!descriptorMap.containsKey(eventDescriptor)) 186 { 187 throw new IllegalArgumentException ("Control interface " + controlInterface + 188 " does not contain an event method that " + 189 " corresponds to " + eventDescriptor); 190 } 191 return descriptorMap.get(eventDescriptor); 192 } 193 194 198 static private WeakHashMap <Class , HashMap <String ,Method >> _descriptorMaps = 199 new WeakHashMap <Class , HashMap <String ,Method >>(); 200 201 private HashMap <String ,Method > getDescriptorMap(Class controlInterface) 202 { 203 HashMap <String ,Method > descMap = _descriptorMaps.get(controlInterface); 207 if (descMap == null) 208 { 209 descMap = new HashMap <String , Method >(); 213 Class [] innerClasses = controlInterface.getClasses(); 214 for (int i = 0; i < innerClasses.length; i++) 215 { 216 if (!innerClasses[i].isInterface() || 217 !innerClasses[i].isAnnotationPresent(EventSet.class)) 218 continue; 219 220 Method [] eventMethods = innerClasses[i].getMethods(); 221 for (int j = 0; j < eventMethods.length; j++) 222 descMap.put(computeEventDescriptor(eventMethods[j]), eventMethods[j]); 223 } 224 _descriptorMaps.put(controlInterface, descMap); 225 } 226 return descMap; 227 } 228 229 230 233 public boolean equals(Object obj) 234 { 235 if (obj == null || !(obj instanceof EventRef)) 236 return false; 237 238 return _descriptor.equals(((EventRef)obj)._descriptor); 239 } 240 241 public String toString() 242 { 243 return "EventRef: " + _descriptor; 244 } 245 246 private String _descriptor; 253 transient private Method _method; 254 } 255 | Popular Tags |