PropertyDescriptor.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /* java.beans.PropertyDescriptor
  2. Copyright (C) 1998, 2001 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., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307 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 java.util.*;
  33. import java.lang.reflect.*;
  34. /**
  35. ** PropertyDescriptor describes information about a JavaBean property,
  36. ** by which we mean a property that has been exposed via a pair of
  37. ** get and set methods. (There may be no get method, which means
  38. ** the property is write-only, or no set method, which means the
  39. ** the property is read-only.)<P>
  40. **
  41. ** The constraints put on get and set methods are:<P>
  42. ** <OL>
  43. ** <LI>A get method must have signature
  44. ** <CODE>&lt;propertyType&gt; &lt;getMethodName&gt;()</CODE></LI>
  45. ** <LI>A set method must have signature
  46. ** <CODE>void &lt;setMethodName&gt;(&lt;propertyType&gt;)</CODE></LI>
  47. ** <LI>Either method type may throw any exception.</LI>
  48. ** <LI>Both methods must be public.</LI>
  49. ** </OL>
  50. **
  51. ** @author John Keiser
  52. ** @since JDK1.1
  53. ** @version 1.1.0, 26 Jul 1998
  54. **/
  55. public class PropertyDescriptor extends FeatureDescriptor {
  56. Class propertyType;
  57. Method getMethod;
  58. Method setMethod;
  59. Class propertyEditorClass;
  60. boolean bound;
  61. boolean constrained;
  62. PropertyDescriptor(String name) {
  63. setName(name);
  64. }
  65. /** Create a new PropertyDescriptor by introspection.
  66. ** This form of constructor creates the PropertyDescriptor by
  67. ** looking for a getter method named <CODE>get&lt;name&gt;()</CODE>
  68. ** (or, optionally, if the property is boolean,
  69. ** <CODE>is&lt;name&gt;()</CODE>) and
  70. ** <CODE>set&lt;name&gt;()</CODE> in class
  71. ** <CODE>&lt;beanClass&gt;</CODE>, where &lt;name&gt; has its
  72. ** first letter capitalized by the constructor.<P>
  73. **
  74. ** <B>Implementation note:</B> If there is both are both isXXX and
  75. ** getXXX methods, the former is used in preference to the latter.
  76. ** We do not check that an isXXX method returns a boolean. In both
  77. ** cases, this matches the behaviour of JDK 1.4<P>
  78. **
  79. ** @param name the programmatic name of the property, usually
  80. ** starting with a lowercase letter (e.g. fooManChu
  81. ** instead of FooManChu).
  82. ** @param beanClass the class the get and set methods live in.
  83. ** @exception IntrospectionException if the methods are not found
  84. ** or invalid.
  85. **/
  86. public PropertyDescriptor(String name, Class beanClass)
  87. throws IntrospectionException
  88. {
  89. setName(name);
  90. if (name.length() == 0) {
  91. throw new IntrospectionException("empty property name");
  92. }
  93. String caps = Character.toUpperCase(name.charAt(0)) + name.substring(1);
  94. findMethods(beanClass, "is" + caps, "get" + caps, "set" + caps);
  95. if (getMethod == null) {
  96. throw new IntrospectionException("Cannot find an is" + caps +
  97. " or get" + caps + " method");
  98. }
  99. if (setMethod == null) {
  100. throw new IntrospectionException("Cannot find a " + caps + " method");
  101. }
  102. checkMethods();
  103. }
  104. /** Create a new PropertyDescriptor by introspection.
  105. ** This form of constructor allows you to specify the
  106. ** names of the get and set methods to search for.<P>
  107. **
  108. ** <B>Implementation note:</B> If there is a get method (or
  109. ** boolean isXXX() method), then the return type of that method
  110. ** is used to find the set method. If there is no get method,
  111. ** then the set method is searched for exhaustively.<P>
  112. **
  113. ** <B>Spec note:</B>
  114. ** If there is no get method and multiple set methods with
  115. ** the same name and a single parameter (different type of course),
  116. ** then an IntrospectionException is thrown. While Sun's spec
  117. ** does not state this, it can make Bean behavior different on
  118. ** different systems (since method order is not guaranteed) and as
  119. ** such, can be treated as a bug in the spec. I am not aware of
  120. ** whether Sun's implementation catches this.
  121. **
  122. ** @param name the programmatic name of the property, usually
  123. ** starting with a lowercase letter (e.g. fooManChu
  124. ** instead of FooManChu).
  125. ** @param beanClass the class the get and set methods live in.
  126. ** @param getMethodName the name of the get method.
  127. ** @param setMethodName the name of the set method.
  128. ** @exception IntrospectionException if the methods are not found
  129. ** or invalid.
  130. **/
  131. public PropertyDescriptor(String name, Class beanClass,
  132. String getMethodName, String setMethodName)
  133. throws IntrospectionException
  134. {
  135. setName(name);
  136. findMethods(beanClass, getMethodName, null, setMethodName);
  137. if (getMethod == null && getMethodName != null) {
  138. throw new IntrospectionException("Cannot find a getter method called " +
  139. getMethodName);
  140. }
  141. if (setMethod == null && setMethodName != null) {
  142. throw new IntrospectionException("Cannot find a setter method called " +
  143. setMethodName);
  144. }
  145. checkMethods();
  146. }
  147. /** Create a new PropertyDescriptor using explicit Methods.
  148. ** Note that the methods will be checked for conformance to standard
  149. ** Property method rules, as described above at the top of this class.
  150. **
  151. ** @param name the programmatic name of the property, usually
  152. ** starting with a lowercase letter (e.g. fooManChu
  153. ** instead of FooManChu).
  154. ** @param getMethod the get method.
  155. ** @param setMethod the set method.
  156. ** @exception IntrospectionException if the methods are not found
  157. ** or invalid.
  158. **/
  159. public PropertyDescriptor(String name, Method getMethod, Method setMethod)
  160. throws IntrospectionException
  161. {
  162. setName(name);
  163. this.getMethod = getMethod;
  164. this.setMethod = setMethod;
  165. if (getMethod != null) {
  166. this.propertyType = getMethod.getReturnType();
  167. }
  168. else if (setMethod != null) {
  169. this.propertyType = setMethod.getParameterTypes()[0];
  170. }
  171. checkMethods();
  172. }
  173. /** Get the property type.
  174. ** This is the type the get method returns and the set method
  175. ** takes in.
  176. **/
  177. public Class getPropertyType() {
  178. return propertyType;
  179. }
  180. /** Get the get method. Why they call it readMethod here and
  181. ** get everywhere else is beyond me.
  182. **/
  183. public Method getReadMethod() {
  184. return getMethod;
  185. }
  186. /** Get the set method. Why they call it writeMethod here and
  187. ** set everywhere else is beyond me.
  188. **/
  189. public Method getWriteMethod() {
  190. return setMethod;
  191. }
  192. /** Get whether the property is bound. Defaults to false. **/
  193. public boolean isBound() {
  194. return bound;
  195. }
  196. /** Set whether the property is bound.
  197. ** As long as the the bean implements addPropertyChangeListener() and
  198. ** removePropertyChangeListener(), setBound(true) may safely be called.<P>
  199. ** If these things are not true, then the behavior of the system
  200. ** will be undefined.<P>
  201. **
  202. ** When a property is bound, its set method is required to fire the
  203. ** <CODE>PropertyChangeListener.propertyChange())</CODE> event
  204. ** after the value has changed.
  205. ** @param bound whether the property is bound or not.
  206. **/
  207. public void setBound(boolean bound) {
  208. this.bound = bound;
  209. }
  210. /** Get whether the property is constrained. Defaults to false. **/
  211. public boolean isConstrained() {
  212. return constrained;
  213. }
  214. /** Set whether the property is constrained.
  215. ** If the set method throws <CODE>java.beans.PropertyVetoException</CODE>
  216. ** (or subclass thereof) and the bean implements addVetoableChangeListener()
  217. ** and removeVetoableChangeListener(), then setConstrained(true) may safely
  218. ** be called. Otherwise, the system behavior is undefined.
  219. ** <B>Spec note:</B> given those strict parameters, it would be nice if it
  220. ** got set automatically by detection, but oh well.<P>
  221. ** When a property is constrained, its set method is required to:<P>
  222. ** <OL>
  223. ** <LI>Fire the <CODE>VetoableChangeListener.vetoableChange()</CODE>
  224. ** event notifying others of the change and allowing them a chance to
  225. ** say it is a bad thing.</LI>
  226. ** <LI>If any of the listeners throws a PropertyVetoException, then
  227. ** it must fire another vetoableChange() event notifying the others
  228. ** of a reversion to the old value (though, of course, the change
  229. ** was never made). Then it rethrows the PropertyVetoException and
  230. ** exits.</LI>
  231. ** <LI>If all has gone well to this point, the value may be changed.</LI>
  232. ** </OL>
  233. ** @param constrained whether the property is constrained or not.
  234. **/
  235. public void setConstrained(boolean constrained) {
  236. this.constrained = constrained;
  237. }
  238. /** Get the PropertyEditor class. Defaults to null. **/
  239. public Class getPropertyEditorClass() {
  240. return propertyEditorClass;
  241. }
  242. /** Set the PropertyEditor class. If the class does not implement
  243. ** the PropertyEditor interface, you will likely get an exception
  244. ** late in the game.
  245. ** @param propertyEditorClass the PropertyEditor class for this
  246. ** class to use.
  247. **/
  248. public void setPropertyEditorClass(Class propertyEditorClass) {
  249. this.propertyEditorClass = propertyEditorClass;
  250. }
  251. private void findMethods(Class beanClass, String getMethodName1,
  252. String getMethodName2, String setMethodName)
  253. throws IntrospectionException
  254. {
  255. try {
  256. // Try the first get method name
  257. if (getMethodName1 != null) {
  258. try {
  259. getMethod = beanClass.getMethod(getMethodName1, new Class[0]);
  260. }
  261. catch (NoSuchMethodException e) {
  262. }
  263. }
  264. // Fall back to the second get method name
  265. if (getMethod == null && getMethodName2 != null) {
  266. try {
  267. getMethod = beanClass.getMethod(getMethodName2, new Class[0]);
  268. }
  269. catch (NoSuchMethodException e) {
  270. }
  271. }
  272. // Try the set method name
  273. if (setMethodName != null) {
  274. if (getMethod != null) {
  275. // If there is a get method, use its return type to help
  276. // select the corresponding set method.
  277. Class propertyType = getMethod.getReturnType();
  278. if (propertyType == Void.TYPE) {
  279. String msg = "The property's read method has return type 'void'";
  280. throw new IntrospectionException(msg);
  281. }
  282. Class[] setArgs = new Class[]{propertyType};
  283. try {
  284. setMethod = beanClass.getMethod(setMethodName, setArgs);
  285. }
  286. catch (NoSuchMethodException e) {
  287. }
  288. }
  289. else if (getMethodName1 == null && getMethodName2 == null) {
  290. // If this is a write-only property, choose the first set method
  291. // with the required name, one parameter and return type 'void'
  292. Method[] methods = beanClass.getMethods();
  293. for (int i = 0; i < methods.length; i++) {
  294. if (methods[i].getName().equals(setMethodName) &&
  295. methods[i].getParameterTypes().length == 1 &&
  296. methods[i].getReturnType() == Void.TYPE) {
  297. setMethod = methods[i];
  298. break;
  299. }
  300. }
  301. }
  302. }
  303. }
  304. catch (SecurityException e) {
  305. // FIXME -- shouldn't we just allow SecurityException to propagate?
  306. String msg = "SecurityException thrown on attempt to access methods.";
  307. throw new IntrospectionException(msg);
  308. }
  309. }
  310. private void checkMethods()
  311. throws IntrospectionException
  312. {
  313. if (getMethod != null) {
  314. if (getMethod.getParameterTypes().length > 0) {
  315. throw new IntrospectionException("get method has parameters");
  316. }
  317. this.propertyType = getMethod.getReturnType();
  318. if (propertyType == Void.TYPE) {
  319. throw new IntrospectionException("get method has void return type");
  320. }
  321. }
  322. if (setMethod != null) {
  323. if (setMethod.getParameterTypes().length != 1) {
  324. String msg = "set method does not have exactly one parameter";
  325. throw new IntrospectionException(msg);
  326. }
  327. if (getMethod == null) {
  328. propertyType = setMethod.getParameterTypes()[0];
  329. }
  330. else {
  331. if (!propertyType.equals(setMethod.getParameterTypes()[0])) {
  332. String msg = "set and get methods do not share the same type";
  333. throw new IntrospectionException(msg);
  334. }
  335. if ((!getMethod.getDeclaringClass().
  336. isAssignableFrom(setMethod.getDeclaringClass())) &&
  337. (!setMethod.getDeclaringClass().
  338. isAssignableFrom(getMethod.getDeclaringClass()))) {
  339. String msg = "set and get methods are not in the same class.";
  340. throw new IntrospectionException(msg);
  341. }
  342. }
  343. }
  344. }
  345. }