EventSetDescriptor.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. /* java.beans.EventSetDescriptor
  2. Copyright (C) 1998, 2006 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.beans;
  32. import gnu.java.lang.ClassHelper;
  33. import java.lang.reflect.Method;
  34. import java.lang.reflect.Modifier;
  35. import java.util.Vector;
  36. /**
  37. * EventSetDescriptor describes the hookup between an event source class and
  38. * an event listener class.
  39. *
  40. * <p>EventSets have several attributes: the listener class,
  41. * the events that can be fired to the listener (methods in the listener
  42. * class), and an add and remove listener method from the event firer's
  43. * class.
  44. * </p>
  45. *
  46. * <p>
  47. * The methods have these constraints on them:
  48. * <ul>
  49. * <li>event firing methods: must have <code>void</code> return value. Any
  50. * parameters and exceptions are allowed. May be public, protected or
  51. * package-protected. (Don't ask me why that is, I'm just following the spec.
  52. * The only place it is even mentioned is in the Java Beans white paper, and
  53. * there it is only implied.)</li>
  54. *
  55. * <li>add listener method: must have <code>void</code> return value. Must
  56. * take exactly one argument, of the listener class's type. May fire either
  57. * zero exceptions, or one exception of type
  58. * <code>java.util.TooManyListenersException</code>.
  59. * Must be public.</li>
  60. *
  61. * <li>remove listener method: must have <code>void</code> return value. Must
  62. * take exactly one argument, of the listener class's type. May not fire any
  63. * exceptions. Must be public.</li>
  64. * </ul>
  65. *
  66. * <p>
  67. * A final constraint is that event listener classes must extend from
  68. * EventListener.
  69. * </p>
  70. *
  71. * <p>
  72. * There are also various design patterns associated with some of the methods
  73. * of construction. Those are explained in more detail in the appropriate
  74. * constructors.
  75. * </p>
  76. *
  77. * <p>
  78. * <strong>Documentation Convention:</strong> for proper Internalization of
  79. * Beans inside an RAD tool, sometimes there are two names for a property or
  80. * method: a programmatic, or locale-independent name, which can be used
  81. * anywhere, and a localized, display name, for ease of use. In the
  82. * documentation I will specify different String values as either
  83. * <em>programmatic</em> or <em>localized</em> to make this distinction clear.
  84. *
  85. * @author John Keiser
  86. * @author Robert Schuster (robertschuster@fsfe.org)
  87. * @since 1.1
  88. */
  89. public class EventSetDescriptor extends FeatureDescriptor
  90. {
  91. private Method addListenerMethod;
  92. private Method removeListenerMethod;
  93. private Class listenerType;
  94. private MethodDescriptor[] listenerMethodDescriptors;
  95. private Method[] listenerMethods;
  96. private Method getListenerMethod;
  97. private boolean unicast;
  98. private boolean inDefaultEventSet = true;
  99. /**
  100. * Creates a new <code>EventSetDescriptor</code<.
  101. *
  102. * <p>
  103. * This version of the constructor enforces the rules imposed on the methods
  104. * described at the top of this class, as well as searching for:
  105. * </p>
  106. *
  107. * <ol>
  108. * <li>
  109. * The event-firing method must be non-private with signature <code>void
  110. * &lt;listenerMethodName&gt;(&lt;eventSetName&gt;Event)</code> (where
  111. * <code>&lt;eventSetName&gt;</code> has its first character capitalized
  112. * by the constructor and the Event is a descendant of
  113. * {@link java.util.EventObject}) in class <code>listenerType</code>
  114. * (any exceptions may be thrown). <b>Implementation note:</b> Note that
  115. * there could conceivably be multiple methods with this type of signature
  116. * (example: <code>java.util.MouseEvent</code> vs.
  117. * <code>my.very.own.MouseEvent</code>). In this implementation, all
  118. * methods fitting the description will be put into the
  119. * <code>EventSetDescriptor</code>, even though the spec says only one
  120. * should be chosen (they probably weren't thinking as pathologically as I
  121. * was). I don't like arbitrarily choosing things. If your class has only one
  122. * such signature, as most do, you'll have no problems.</li>
  123. *
  124. * <li>The add and remove methods must be public and named <code>void
  125. * add&lt;eventSetName&gt;Listener(&lt;listenerType&gt;)</code> and
  126. * <code>void remove&lt;eventSetName&gt;Listener(&lt;listenerType&gt;)</code>
  127. * in in class <code>eventSourceClass</code>, where
  128. * <code>&lt;eventSetName&gt;</code> will have its first letter capitalized.
  129. * Standard exception rules (see class description) apply.</li>
  130. * </ol>
  131. *
  132. * @param eventSourceClass
  133. * the class containing the add/remove listener methods.
  134. * @param eventSetName
  135. * the programmatic name of the event set, generally starting with a
  136. * lowercase letter (i.e. fooManChu instead of FooManChu). This will
  137. * be used to generate the name of the event object as well as the
  138. * names of the add and remove methods.
  139. * @param listenerType
  140. * the class containing the event firing method.
  141. * @param listenerMethodName
  142. * the name of the event firing method.
  143. * @exception IntrospectionException
  144. * if listenerType is not an EventListener, or if methods are not
  145. * found or are invalid.
  146. */
  147. public EventSetDescriptor(Class<?> eventSourceClass, String eventSetName,
  148. Class<?> listenerType, String listenerMethodName)
  149. throws IntrospectionException
  150. {
  151. setName(eventSetName);
  152. if (!java.util.EventListener.class.isAssignableFrom(listenerType))
  153. {
  154. throw new IntrospectionException(
  155. "Listener type is not an EventListener.");
  156. }
  157. String[] names = new String[1];
  158. names[0] = listenerMethodName;
  159. try
  160. {
  161. eventSetName = Character.toUpperCase(eventSetName.charAt(0))
  162. + eventSetName.substring(1);
  163. }
  164. catch (StringIndexOutOfBoundsException e)
  165. {
  166. eventSetName = "";
  167. }
  168. findMethods(eventSourceClass, listenerType, names,
  169. "add" + eventSetName + "Listener",
  170. "remove" + eventSetName + "Listener", eventSetName + "Event");
  171. this.listenerType = listenerType;
  172. checkAddListenerUnicast();
  173. if (this.removeListenerMethod.getExceptionTypes().length > 0)
  174. {
  175. throw new IntrospectionException(
  176. "Listener remove method throws exceptions.");
  177. }
  178. }
  179. /**
  180. * Creates a new <code>EventSetDescriptor</code>.
  181. *
  182. * <p>This form of the constructor allows you to specify the names of the
  183. * methods and adds no new constraints on top of the rules already described
  184. * at the top of the class.
  185. * </p>
  186. *
  187. * @param eventSourceClass
  188. * the class containing the add and remove listener methods.
  189. * @param eventSetName
  190. * the programmatic name of the event set, generally starting with a
  191. * lowercase letter (i.e. fooManChu instead of FooManChu).
  192. * @param listenerType
  193. * the class containing the event firing methods.
  194. * @param listenerMethodNames
  195. * the names of the even firing methods.
  196. * @param addListenerMethodName
  197. * the name of the add listener method.
  198. * @param removeListenerMethodName
  199. * the name of the remove listener method.
  200. * @exception IntrospectionException
  201. * if listenerType is not an EventListener or if methods are not
  202. * found or are invalid.
  203. */
  204. public EventSetDescriptor(Class<?> eventSourceClass, String eventSetName,
  205. Class<?> listenerType, String[] listenerMethodNames,
  206. String addListenerMethodName,
  207. String removeListenerMethodName)
  208. throws IntrospectionException
  209. {
  210. setName(eventSetName);
  211. if (!java.util.EventListener.class.isAssignableFrom(listenerType))
  212. {
  213. throw new IntrospectionException(
  214. "Listener type is not an EventListener.");
  215. }
  216. findMethods(eventSourceClass, listenerType, listenerMethodNames,
  217. addListenerMethodName, removeListenerMethodName, null);
  218. this.listenerType = listenerType;
  219. checkAddListenerUnicast();
  220. if (this.removeListenerMethod.getExceptionTypes().length > 0)
  221. {
  222. throw new IntrospectionException(
  223. "Listener remove method throws exceptions.");
  224. }
  225. }
  226. /**
  227. * Creates a new <code>EventSetDescriptor</code>.
  228. *
  229. * <p>
  230. * This variant of the constructor allows you to specify the names of the
  231. * methods and adds no new constraints on top of the rules already described
  232. * at the top of the class.
  233. * </p>
  234. * <p>
  235. * A valid GetListener method is public, flags no exceptions and has one
  236. * argument which is of type <code>Class</code>
  237. * {@link java.awt.Component#getListeners(Class)} is such a method.
  238. * </p>
  239. * <p>
  240. * Note: The validity of the return value of the GetListener method is not
  241. * checked.
  242. * </p>
  243. *
  244. * @param eventSourceClass
  245. * the class containing the add and remove listener methods.
  246. * @param eventSetName
  247. * the programmatic name of the event set, generally starting with a
  248. * lowercase letter (i.e. fooManChu instead of FooManChu).
  249. * @param listenerType
  250. * the class containing the event firing methods.
  251. * @param listenerMethodNames
  252. * the names of the even firing methods.
  253. * @param addListenerMethodName
  254. * the name of the add listener method.
  255. * @param removeListenerMethodName
  256. * the name of the remove listener method.
  257. * @param getListenerMethodName
  258. * Name of a method which returns the array of listeners.
  259. * @exception IntrospectionException
  260. * if listenerType is not an EventListener or if methods are not
  261. * found or are invalid.
  262. * @since 1.4
  263. */
  264. public EventSetDescriptor(Class<?> eventSourceClass, String eventSetName,
  265. Class<?> listenerType, String[] listenerMethodNames,
  266. String addListenerMethodName,
  267. String removeListenerMethodName,
  268. String getListenerMethodName)
  269. throws IntrospectionException
  270. {
  271. this(eventSourceClass, eventSetName, listenerType, listenerMethodNames,
  272. addListenerMethodName, removeListenerMethodName);
  273. Method newGetListenerMethod = null;
  274. try
  275. {
  276. newGetListenerMethod
  277. = eventSourceClass.getMethod(getListenerMethodName,
  278. new Class[] { Class.class });
  279. }
  280. catch (NoSuchMethodException nsme)
  281. {
  282. throw (IntrospectionException)
  283. new IntrospectionException("No method named " + getListenerMethodName
  284. + " in class " + listenerType
  285. + " which can be used as"
  286. + " getListenerMethod.").initCause(nsme);
  287. }
  288. // Note: This does not check the return value (which
  289. // should be EventListener[]) but the JDK does not either.
  290. getListenerMethod = newGetListenerMethod;
  291. }
  292. /**
  293. * Creates a new <code>EventSetDescriptor.</code>
  294. *
  295. * <p>
  296. * This variant of the constructor allows you to specify the names of the
  297. * methods and adds no new constraints on top of the rules already described
  298. * at the top of the class.
  299. * </p>
  300. * <p>
  301. * A valid GetListener method is public, flags no exceptions and has one
  302. * argument which is of type <code>Class</code>
  303. * {@link java.awt.Component#getListeners(Class)} is such a method.
  304. * </p>
  305. * <p>
  306. * Note: The validity of the return value of the GetListener method is not
  307. * checked.
  308. * </p>
  309. *
  310. * @param eventSetName
  311. * the programmatic name of the event set, generally starting with a
  312. * lowercase letter (i.e. fooManChu instead of FooManChu).
  313. * @param listenerType
  314. * the class containing the listenerMethods.
  315. * @param listenerMethods
  316. * the event firing methods.
  317. * @param addListenerMethod
  318. * the add listener method.
  319. * @param removeListenerMethod
  320. * the remove listener method.
  321. * @param getListenerMethod
  322. * The method which returns an array of the listeners.
  323. * @exception IntrospectionException
  324. * if the listenerType is not an EventListener, or any of the
  325. * methods are invalid.
  326. * @since 1.4
  327. */
  328. public EventSetDescriptor(String eventSetName, Class<?> listenerType,
  329. Method[] listenerMethods, Method addListenerMethod,
  330. Method removeListenerMethod,
  331. Method getListenerMethod)
  332. throws IntrospectionException
  333. {
  334. this(eventSetName, listenerType, listenerMethods, addListenerMethod,
  335. removeListenerMethod);
  336. // Do no checks if the getListenerMethod is null.
  337. if (getListenerMethod.getParameterTypes().length != 1
  338. || getListenerMethod.getParameterTypes()[0] != Class.class
  339. || getListenerMethod.getExceptionTypes().length > 0
  340. || !Modifier.isPublic(getListenerMethod.getModifiers()))
  341. throw new IntrospectionException("GetListener method is invalid.");
  342. // Note: This does not check the return value (which
  343. // should be EventListener[]) but the JDK does not either.
  344. this.getListenerMethod = getListenerMethod;
  345. }
  346. /**
  347. * Creates a new <code>EventSetDescriptor</code>.
  348. *
  349. * <p>This form of constructor allows you to explicitly say which methods
  350. * do what, and no reflection is done by the <code>EventSetDescriptor</code>.
  351. * The methods are, however, checked to ensure that they follow the rules
  352. * set forth at the top of the class.
  353. *
  354. * @param eventSetName
  355. * the programmatic name of the event set, generally starting with a
  356. * lowercase letter (i.e. fooManChu instead of FooManChu).
  357. * @param listenerType
  358. * the class containing the listenerMethods.
  359. * @param listenerMethods
  360. * the event firing methods.
  361. * @param addListenerMethod
  362. * the add listener method.
  363. * @param removeListenerMethod
  364. * the remove listener method.
  365. * @exception IntrospectionException
  366. * if the listenerType is not an EventListener, or any of the
  367. * methods are invalid.
  368. */
  369. public EventSetDescriptor(String eventSetName, Class<?> listenerType,
  370. Method[] listenerMethods, Method addListenerMethod,
  371. Method removeListenerMethod)
  372. throws IntrospectionException
  373. {
  374. setName(eventSetName);
  375. if (!java.util.EventListener.class.isAssignableFrom(listenerType))
  376. {
  377. throw new IntrospectionException(
  378. "Listener type is not an EventListener.");
  379. }
  380. this.listenerMethods = listenerMethods;
  381. this.addListenerMethod = addListenerMethod;
  382. this.removeListenerMethod = removeListenerMethod;
  383. this.listenerType = listenerType;
  384. checkMethods();
  385. checkAddListenerUnicast();
  386. if (this.removeListenerMethod.getExceptionTypes().length > 0)
  387. {
  388. throw new IntrospectionException(
  389. "Listener remove method throws exceptions.");
  390. }
  391. }
  392. /** Creates a new <code>EventSetDescriptor</code>.
  393. *
  394. * <p>This form of constructor allows you to explicitly say which methods do
  395. * what, and no reflection is done by the <code>EventSetDescriptor</code>.
  396. * The methods are, however, checked to ensure that they follow the rules
  397. * set forth at the top of the class.
  398. *
  399. * @param eventSetName
  400. * the programmatic name of the event set, generally starting with a
  401. * lowercase letter (i.e. fooManChu instead of FooManChu).
  402. * @param listenerType
  403. * the class containing the listenerMethods.
  404. * @param listenerMethodDescriptors
  405. * the event firing methods.
  406. * @param addListenerMethod
  407. * the add listener method.
  408. * @param removeListenerMethod
  409. * the remove listener method.
  410. * @exception IntrospectionException
  411. * if the listenerType is not an EventListener, or any of the
  412. * methods are invalid.
  413. */
  414. public EventSetDescriptor(String eventSetName, Class<?> listenerType,
  415. MethodDescriptor[] listenerMethodDescriptors,
  416. Method addListenerMethod,
  417. Method removeListenerMethod)
  418. throws IntrospectionException
  419. {
  420. setName(eventSetName);
  421. if (!java.util.EventListener.class.isAssignableFrom(listenerType))
  422. {
  423. throw new IntrospectionException(
  424. "Listener type is not an EventListener.");
  425. }
  426. this.listenerMethodDescriptors = listenerMethodDescriptors;
  427. this.listenerMethods = new Method[listenerMethodDescriptors.length];
  428. for (int i = 0; i < this.listenerMethodDescriptors.length; i++)
  429. {
  430. this.listenerMethods[i]
  431. = this.listenerMethodDescriptors[i].getMethod();
  432. }
  433. this.addListenerMethod = addListenerMethod;
  434. this.removeListenerMethod = removeListenerMethod;
  435. this.listenerType = listenerType;
  436. checkMethods();
  437. checkAddListenerUnicast();
  438. if (this.removeListenerMethod.getExceptionTypes().length > 0)
  439. {
  440. throw new IntrospectionException(
  441. "Listener remove method throws exceptions.");
  442. }
  443. }
  444. /** Returns the class that contains the event firing methods.
  445. */
  446. public Class<?> getListenerType()
  447. {
  448. return listenerType;
  449. }
  450. /** Returns the event firing methods.
  451. */
  452. public Method[] getListenerMethods()
  453. {
  454. return listenerMethods;
  455. }
  456. /** Returns the event firing methods as {@link MethodDescriptor}.
  457. */
  458. public MethodDescriptor[] getListenerMethodDescriptors()
  459. {
  460. if (listenerMethodDescriptors == null)
  461. {
  462. listenerMethodDescriptors
  463. = new MethodDescriptor[listenerMethods.length];
  464. for (int i = 0; i < listenerMethods.length; i++)
  465. {
  466. listenerMethodDescriptors[i]
  467. = new MethodDescriptor(listenerMethods[i]);
  468. }
  469. }
  470. return listenerMethodDescriptors;
  471. }
  472. /** Returns the add listener method.
  473. */
  474. public Method getAddListenerMethod()
  475. {
  476. return addListenerMethod;
  477. }
  478. /* Returns the remove listener method.
  479. */
  480. public Method getRemoveListenerMethod()
  481. {
  482. return removeListenerMethod;
  483. }
  484. /**
  485. * Returns the method that retrieves the listeners or <code>null</code> if
  486. * it does not exist.
  487. */
  488. public Method getGetListenerMethod()
  489. {
  490. return getListenerMethod;
  491. }
  492. /** Sets whether or not multiple listeners may be added.
  493. *
  494. * @param unicast
  495. * whether or not multiple listeners may be added.
  496. */
  497. public void setUnicast(boolean unicast)
  498. {
  499. this.unicast = unicast;
  500. }
  501. /** Returns whether or not multiple listeners may be added.
  502. * (Defaults to false.)
  503. */
  504. public boolean isUnicast()
  505. {
  506. return unicast;
  507. }
  508. /** Sets whether or not this is in the default event set.
  509. *
  510. * @param inDefaultEventSet
  511. * whether this is in the default event set.
  512. */
  513. public void setInDefaultEventSet(boolean inDefaultEventSet)
  514. {
  515. this.inDefaultEventSet = inDefaultEventSet;
  516. }
  517. /** Returns whether or not this is in the default event set.
  518. * (Defaults to true.)
  519. */
  520. public boolean isInDefaultEventSet()
  521. {
  522. return inDefaultEventSet;
  523. }
  524. private void checkAddListenerUnicast() throws IntrospectionException
  525. {
  526. Class[] addListenerExceptions = this.addListenerMethod.getExceptionTypes();
  527. if (addListenerExceptions.length > 1)
  528. {
  529. throw new IntrospectionException(
  530. "Listener add method throws too many exceptions.");
  531. }
  532. else if (addListenerExceptions.length == 1
  533. && !java.util.TooManyListenersException.class
  534. .isAssignableFrom(addListenerExceptions[0]))
  535. {
  536. throw new IntrospectionException(
  537. "Listener add method throws too many exceptions.");
  538. }
  539. }
  540. private void checkMethods() throws IntrospectionException
  541. {
  542. if (!addListenerMethod.getDeclaringClass()
  543. .isAssignableFrom(removeListenerMethod.getDeclaringClass())
  544. && !removeListenerMethod.getDeclaringClass()
  545. .isAssignableFrom(addListenerMethod.getDeclaringClass()))
  546. {
  547. throw new IntrospectionException(
  548. "add and remove listener methods do not come from the"
  549. + " same class. This is bad.");
  550. }
  551. if (!addListenerMethod.getReturnType().equals(java.lang.Void.TYPE)
  552. || addListenerMethod.getParameterTypes().length != 1
  553. || !listenerType.equals(addListenerMethod.getParameterTypes()[0])
  554. || !Modifier.isPublic(addListenerMethod.getModifiers()))
  555. {
  556. throw new IntrospectionException("Add Listener Method invalid.");
  557. }
  558. if (!removeListenerMethod.getReturnType().equals(java.lang.Void.TYPE)
  559. || removeListenerMethod.getParameterTypes().length != 1
  560. || !listenerType.equals(removeListenerMethod.getParameterTypes()[0])
  561. || removeListenerMethod.getExceptionTypes().length > 0
  562. || !Modifier.isPublic(removeListenerMethod.getModifiers()))
  563. {
  564. throw new IntrospectionException("Remove Listener Method invalid.");
  565. }
  566. for (int i = 0; i < listenerMethods.length; i++)
  567. {
  568. if (!listenerMethods[i].getReturnType().equals(java.lang.Void.TYPE)
  569. || Modifier.isPrivate(listenerMethods[i].getModifiers()))
  570. {
  571. throw new IntrospectionException("Event Method "
  572. + listenerMethods[i].getName()
  573. + " non-void or private.");
  574. }
  575. if (!listenerMethods[i].getDeclaringClass()
  576. .isAssignableFrom(listenerType))
  577. {
  578. throw new IntrospectionException("Event Method "
  579. + listenerMethods[i].getName()
  580. + " not from class "
  581. + listenerType.getName());
  582. }
  583. }
  584. }
  585. private void findMethods(Class eventSourceClass, Class listenerType,
  586. String listenerMethodNames[],
  587. String addListenerMethodName,
  588. String removeListenerMethodName,
  589. String absurdEventClassCheckName)
  590. throws IntrospectionException
  591. {
  592. /* Find add listener method and remove listener method. */
  593. Class[] listenerArgList = new Class[1];
  594. listenerArgList[0] = listenerType;
  595. try
  596. {
  597. this.addListenerMethod
  598. = eventSourceClass.getMethod(addListenerMethodName,
  599. listenerArgList);
  600. }
  601. catch (SecurityException E)
  602. {
  603. throw new IntrospectionException(
  604. "SecurityException trying to access method "
  605. + addListenerMethodName + ".");
  606. }
  607. catch (NoSuchMethodException E)
  608. {
  609. throw new IntrospectionException("Could not find method "
  610. + addListenerMethodName + ".");
  611. }
  612. if (this.addListenerMethod == null
  613. || !this.addListenerMethod.getReturnType().equals(java.lang.Void.TYPE))
  614. {
  615. throw new IntrospectionException(
  616. "Add listener method does not exist, is not public,"
  617. + " or is not void.");
  618. }
  619. try
  620. {
  621. this.removeListenerMethod
  622. = eventSourceClass.getMethod(removeListenerMethodName,
  623. listenerArgList);
  624. }
  625. catch (SecurityException E)
  626. {
  627. throw new IntrospectionException(
  628. "SecurityException trying to access method "
  629. + removeListenerMethodName + ".");
  630. }
  631. catch (NoSuchMethodException E)
  632. {
  633. throw new IntrospectionException("Could not find method "
  634. + removeListenerMethodName + ".");
  635. }
  636. if (this.removeListenerMethod == null
  637. || !this.removeListenerMethod.getReturnType()
  638. .equals(java.lang.Void.TYPE))
  639. {
  640. throw new IntrospectionException(
  641. "Remove listener method does not exist, is not public,"
  642. + " or is not void.");
  643. }
  644. /* Find the listener methods. */
  645. Method[] methods;
  646. try
  647. {
  648. methods = ClassHelper.getAllMethods(listenerType);
  649. }
  650. catch (SecurityException E)
  651. {
  652. throw new IntrospectionException(
  653. "Security: You cannot access fields in this class.");
  654. }
  655. Vector chosenMethods = new Vector();
  656. boolean[] listenerMethodFound = new boolean[listenerMethodNames.length];
  657. for (int i = 0; i < methods.length; i++)
  658. {
  659. if (Modifier.isPrivate(methods[i].getModifiers()))
  660. {
  661. continue;
  662. }
  663. Method currentMethod = methods[i];
  664. Class retval = currentMethod.getReturnType();
  665. if (retval.equals(java.lang.Void.TYPE))
  666. {
  667. for (int j = 0; j < listenerMethodNames.length; j++)
  668. {
  669. if (currentMethod.getName().equals(listenerMethodNames[j])
  670. && (absurdEventClassCheckName == null
  671. || (currentMethod.getParameterTypes().length == 1
  672. && ((currentMethod.getParameterTypes()[0])
  673. .getName().equals(absurdEventClassCheckName)
  674. || (currentMethod.getParameterTypes()[0])
  675. .getName().endsWith("." + absurdEventClassCheckName)))))
  676. {
  677. chosenMethods.addElement(currentMethod);
  678. listenerMethodFound[j] = true;
  679. }
  680. }
  681. }
  682. }
  683. /* Make sure we found all the methods we were looking for. */
  684. for (int i = 0; i < listenerMethodFound.length; i++)
  685. {
  686. if (!listenerMethodFound[i])
  687. {
  688. throw new IntrospectionException("Could not find event method "
  689. + listenerMethodNames[i]);
  690. }
  691. }
  692. /* Now that we've chosen the listener methods we want, store them. */
  693. this.listenerMethods = new Method[chosenMethods.size()];
  694. for (int i = 0; i < chosenMethods.size(); i++)
  695. {
  696. this.listenerMethods[i] = (Method) chosenMethods.elementAt(i);
  697. }
  698. }
  699. }