1 7 8 package java.beans; 9 10 import java.lang.ref.Reference ; 11 12 import java.lang.reflect.Method ; 13 14 22 public class EventSetDescriptor extends FeatureDescriptor { 23 24 private MethodDescriptor [] listenerMethodDescriptors; 25 private MethodDescriptor addMethodDescriptor; 26 private MethodDescriptor removeMethodDescriptor; 27 private MethodDescriptor getMethodDescriptor; 28 29 private Reference listenerMethodsRef; 30 private Reference listenerTypeRef; 31 32 private boolean unicast; 33 private boolean inDefaultEventSet = true; 34 35 54 public EventSetDescriptor(Class <?> sourceClass, String eventSetName, 55 Class <?> listenerType, String listenerMethodName) 56 throws IntrospectionException { 57 this(sourceClass, eventSetName, listenerType, 58 new String [] { listenerMethodName }, 59 "add" + getListenerClassName(listenerType), 60 "remove" + getListenerClassName(listenerType), 61 "get" + getListenerClassName(listenerType) + "s"); 62 63 String eventName = capitalize(eventSetName) + "Event"; 64 Method [] listenerMethods = getListenerMethods(); 65 if (listenerMethods.length > 0) { 66 Class [] args = listenerMethods[0].getParameterTypes(); 67 if (!"vetoableChange".equals(eventSetName) && !args[0].getName().endsWith(eventName)) { 69 throw new IntrospectionException ("Method \"" + listenerMethodName + 70 "\" should have argument \"" + 71 eventName + "\""); 72 } 73 } 74 } 75 76 private static String getListenerClassName(Class cls) { 77 String className = cls.getName(); 78 return className.substring(className.lastIndexOf('.') + 1); 79 } 80 81 99 public EventSetDescriptor(Class <?> sourceClass, 100 String eventSetName, 101 Class <?> listenerType, 102 String listenerMethodNames[], 103 String addListenerMethodName, 104 String removeListenerMethodName) 105 throws IntrospectionException { 106 this(sourceClass, eventSetName, listenerType, 107 listenerMethodNames, addListenerMethodName, 108 removeListenerMethodName, null); 109 } 110 111 132 public EventSetDescriptor(Class <?> sourceClass, 133 String eventSetName, 134 Class <?> listenerType, 135 String listenerMethodNames[], 136 String addListenerMethodName, 137 String removeListenerMethodName, 138 String getListenerMethodName) 139 throws IntrospectionException { 140 if (sourceClass == null || eventSetName == null || listenerType == null) { 141 throw new NullPointerException (); 142 } 143 setName(eventSetName); 144 setClass0(sourceClass); 145 setListenerType(listenerType); 146 147 Method [] listenerMethods = new Method [listenerMethodNames.length]; 148 for (int i = 0; i < listenerMethodNames.length; i++) { 149 if (listenerMethodNames[i] == null) { 151 throw new NullPointerException (); 152 } 153 listenerMethods[i] = getMethod(listenerType, listenerMethodNames[i], 1); 154 } 155 setListenerMethods(listenerMethods); 156 157 setAddListenerMethod(getMethod(sourceClass, addListenerMethodName, 1)); 158 setRemoveListenerMethod(getMethod(sourceClass, removeListenerMethodName, 1)); 159 160 Method method = Introspector.findMethod(sourceClass, 162 getListenerMethodName, 0); 163 if (method != null) { 164 setGetListenerMethod(method); 165 } 166 } 167 168 private static Method getMethod(Class cls, String name, int args) 169 throws IntrospectionException { 170 if (name == null) { 171 return null; 172 } 173 Method method = Introspector.findMethod(cls, name, args); 174 if (method == null) { 175 throw new IntrospectionException ("Method not found: " + name + 176 " on class " + cls.getName()); 177 } 178 return method; 179 } 180 181 196 public EventSetDescriptor(String eventSetName, 197 Class <?> listenerType, 198 Method listenerMethods[], 199 Method addListenerMethod, 200 Method removeListenerMethod) 201 throws IntrospectionException { 202 this(eventSetName, listenerType, listenerMethods, 203 addListenerMethod, removeListenerMethod, null); 204 } 205 206 224 public EventSetDescriptor(String eventSetName, 225 Class <?> listenerType, 226 Method listenerMethods[], 227 Method addListenerMethod, 228 Method removeListenerMethod, 229 Method getListenerMethod) 230 throws IntrospectionException { 231 setName(eventSetName); 232 setListenerMethods(listenerMethods); 233 setAddListenerMethod(addListenerMethod); 234 setRemoveListenerMethod( removeListenerMethod); 235 setGetListenerMethod(getListenerMethod); 236 setListenerType(listenerType); 237 } 238 239 256 public EventSetDescriptor(String eventSetName, 257 Class <?> listenerType, 258 MethodDescriptor listenerMethodDescriptors[], 259 Method addListenerMethod, 260 Method removeListenerMethod) 261 throws IntrospectionException { 262 setName(eventSetName); 263 this.listenerMethodDescriptors = listenerMethodDescriptors; 264 setAddListenerMethod(addListenerMethod); 265 setRemoveListenerMethod(removeListenerMethod); 266 setListenerType(listenerType); 267 } 268 269 275 public Class <?> getListenerType() { 276 return (Class )getObject(listenerTypeRef); 277 } 278 279 private void setListenerType(Class cls) { 280 listenerTypeRef = createReference(cls); 281 } 282 283 290 public synchronized Method [] getListenerMethods() { 291 Method [] methods = getListenerMethods0(); 292 if (methods == null) { 293 if (listenerMethodDescriptors != null) { 294 methods = new Method [listenerMethodDescriptors.length]; 295 for (int i = 0; i < methods.length; i++) { 296 methods[i] = listenerMethodDescriptors[i].getMethod(); 297 } 298 } 299 setListenerMethods(methods); 300 } 301 return methods; 302 } 303 304 private void setListenerMethods(Method [] methods) { 305 if (methods == null) { 306 return; 307 } 308 if (listenerMethodDescriptors == null) { 309 listenerMethodDescriptors = new MethodDescriptor [methods.length]; 310 for (int i = 0; i < methods.length; i++) { 311 listenerMethodDescriptors[i] = new MethodDescriptor (methods[i]); 312 } 313 } 314 listenerMethodsRef = createReference(methods, true); 315 } 316 317 private Method [] getListenerMethods0() { 318 return (Method [])getObject(listenerMethodsRef); 319 } 320 321 328 public synchronized MethodDescriptor [] getListenerMethodDescriptors() { 329 return listenerMethodDescriptors; 330 } 331 332 337 public synchronized Method getAddListenerMethod() { 338 return (addMethodDescriptor != null ? 339 addMethodDescriptor.getMethod() : null); 340 } 341 342 private synchronized void setAddListenerMethod(Method method) { 343 if (method == null) { 344 return; 345 } 346 if (getClass0() == null) { 347 setClass0(method.getDeclaringClass()); 348 } 349 addMethodDescriptor = new MethodDescriptor (method); 350 } 351 352 357 public synchronized Method getRemoveListenerMethod() { 358 return (removeMethodDescriptor != null ? 359 removeMethodDescriptor.getMethod() : null); 360 } 361 362 private synchronized void setRemoveListenerMethod(Method method) { 363 if (method == null) { 364 return; 365 } 366 if (getClass0() == null) { 367 setClass0(method.getDeclaringClass()); 368 } 369 removeMethodDescriptor = new MethodDescriptor (method); 370 } 371 372 379 public synchronized Method getGetListenerMethod() { 380 return (getMethodDescriptor != null ? 381 getMethodDescriptor.getMethod() : null); 382 } 383 384 private synchronized void setGetListenerMethod(Method method) { 385 if (method == null) { 386 return; 387 } 388 if (getClass0() == null) { 389 setClass0(method.getDeclaringClass()); 390 } 391 getMethodDescriptor = new MethodDescriptor (method); 392 } 393 394 399 public void setUnicast(boolean unicast) { 400 this.unicast = unicast; 401 } 402 403 410 public boolean isUnicast() { 411 return unicast; 412 } 413 414 422 public void setInDefaultEventSet(boolean inDefaultEventSet) { 423 this.inDefaultEventSet = inDefaultEventSet; 424 } 425 426 432 public boolean isInDefaultEventSet() { 433 return inDefaultEventSet; 434 } 435 436 444 EventSetDescriptor(EventSetDescriptor x, EventSetDescriptor y) { 445 super(x,y); 446 listenerMethodDescriptors = x.listenerMethodDescriptors; 447 if (y.listenerMethodDescriptors != null) { 448 listenerMethodDescriptors = y.listenerMethodDescriptors; 449 } 450 451 listenerTypeRef = x.listenerTypeRef; 452 if (y.listenerTypeRef != null) { 453 listenerTypeRef = y.listenerTypeRef; 454 } 455 456 addMethodDescriptor = x.addMethodDescriptor; 457 if (y.addMethodDescriptor != null) { 458 addMethodDescriptor = y.addMethodDescriptor; 459 } 460 461 removeMethodDescriptor = x.removeMethodDescriptor; 462 if (y.removeMethodDescriptor != null) { 463 removeMethodDescriptor = y.removeMethodDescriptor; 464 } 465 466 getMethodDescriptor = x.getMethodDescriptor; 467 if (y.getMethodDescriptor != null) { 468 getMethodDescriptor = y.getMethodDescriptor; 469 } 470 471 unicast = y.unicast; 472 if (!x.inDefaultEventSet || !y.inDefaultEventSet) { 473 inDefaultEventSet = false; 474 } 475 } 476 477 481 EventSetDescriptor(EventSetDescriptor old) { 482 super(old); 483 if (old.listenerMethodDescriptors != null) { 484 int len = old.listenerMethodDescriptors.length; 485 listenerMethodDescriptors = new MethodDescriptor [len]; 486 for (int i = 0; i < len; i++) { 487 listenerMethodDescriptors[i] = new MethodDescriptor ( 488 old.listenerMethodDescriptors[i]); 489 } 490 } 491 listenerTypeRef = old.listenerTypeRef; 492 493 addMethodDescriptor = old.addMethodDescriptor; 494 removeMethodDescriptor = old.removeMethodDescriptor; 495 getMethodDescriptor = old.getMethodDescriptor; 496 497 unicast = old.unicast; 498 inDefaultEventSet = old.inDefaultEventSet; 499 } 500 } 501 | Popular Tags |