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