PropertyDescriptor.java 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666
  1. /* java.beans.PropertyDescriptor
  2. Copyright (C) 1998, 2001, 2004, 2005 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 java.lang.reflect.Constructor;
  33. import java.lang.reflect.InvocationTargetException;
  34. import java.lang.reflect.Method;
  35. /**
  36. ** PropertyDescriptor describes information about a JavaBean property,
  37. ** by which we mean a property that has been exposed via a pair of
  38. ** get and set methods. (There may be no get method, which means
  39. ** the property is write-only, or no set method, which means the
  40. ** the property is read-only.)<P>
  41. **
  42. ** The constraints put on get and set methods are:<P>
  43. ** <OL>
  44. ** <LI>A get method must have signature
  45. ** <CODE>&lt;propertyType&gt; &lt;getMethodName&gt;()</CODE></LI>
  46. ** <LI>A set method must have signature
  47. ** <CODE>void &lt;setMethodName&gt;(&lt;propertyType&gt;)</CODE></LI>
  48. ** <LI>Either method type may throw any exception.</LI>
  49. ** <LI>Both methods must be public.</LI>
  50. ** </OL>
  51. **
  52. ** @author John Keiser
  53. ** @author Robert Schuster (thebohemian@gmx.net)
  54. ** @since 1.1
  55. ** @status updated to 1.4
  56. **/
  57. public class PropertyDescriptor extends FeatureDescriptor
  58. {
  59. Class<?> propertyType;
  60. Method getMethod;
  61. Method setMethod;
  62. Class<?> propertyEditorClass;
  63. boolean bound;
  64. boolean constrained;
  65. PropertyDescriptor(String name)
  66. {
  67. setName(name);
  68. }
  69. /** Create a new PropertyDescriptor by introspection.
  70. ** This form of constructor creates the PropertyDescriptor by
  71. ** looking for a getter method named <CODE>get&lt;name&gt;()</CODE>
  72. ** (or, optionally, if the property is boolean,
  73. ** <CODE>is&lt;name&gt;()</CODE>) and
  74. ** <CODE>set&lt;name&gt;()</CODE> in class
  75. ** <CODE>&lt;beanClass&gt;</CODE>, where &lt;name&gt; has its
  76. ** first letter capitalized by the constructor.<P>
  77. **
  78. ** Note that using this constructor the given property must be read- <strong>and</strong>
  79. ** writeable. If the implementation does not both, a read and a write method, an
  80. ** <code>IntrospectionException</code> is thrown.
  81. **
  82. ** <B>Implementation note:</B> If there is both are both isXXX and
  83. ** getXXX methods, the former is used in preference to the latter.
  84. ** We do not check that an isXXX method returns a boolean. In both
  85. ** cases, this matches the behaviour of JDK 1.4<P>
  86. **
  87. ** @param name the programmatic name of the property, usually
  88. ** starting with a lowercase letter (e.g. fooManChu
  89. ** instead of FooManChu).
  90. ** @param beanClass the class the get and set methods live in.
  91. ** @exception IntrospectionException if the methods are not found
  92. ** or invalid.
  93. **/
  94. public PropertyDescriptor(String name, Class<?> beanClass)
  95. throws IntrospectionException
  96. {
  97. setName(name);
  98. if (name.length() == 0)
  99. {
  100. throw new IntrospectionException("empty property name");
  101. }
  102. String caps = Character.toUpperCase(name.charAt(0)) + name.substring(1);
  103. findMethods(beanClass, "is" + caps, "get" + caps, "set" + caps);
  104. if (getMethod == null)
  105. {
  106. throw new IntrospectionException(
  107. "Cannot find a is" + caps + " or get" + caps + " method");
  108. }
  109. if (setMethod == null)
  110. {
  111. throw new IntrospectionException(
  112. "Cannot find a " + caps + " method");
  113. }
  114. // finally check the methods compatibility
  115. propertyType = checkMethods(getMethod, setMethod);
  116. }
  117. /** Create a new PropertyDescriptor by introspection.
  118. ** This form of constructor allows you to specify the
  119. ** names of the get and set methods to search for.<P>
  120. **
  121. ** <B>Implementation note:</B> If there is a get method (or
  122. ** boolean isXXX() method), then the return type of that method
  123. ** is used to find the set method. If there is no get method,
  124. ** then the set method is searched for exhaustively.<P>
  125. **
  126. ** <B>Spec note:</B>
  127. ** If there is no get method and multiple set methods with
  128. ** the same name and a single parameter (different type of course),
  129. ** then an IntrospectionException is thrown. While Sun's spec
  130. ** does not state this, it can make Bean behavior different on
  131. ** different systems (since method order is not guaranteed) and as
  132. ** such, can be treated as a bug in the spec. I am not aware of
  133. ** whether Sun's implementation catches this.
  134. **
  135. ** @param name the programmatic name of the property, usually
  136. ** starting with a lowercase letter (e.g. fooManChu
  137. ** instead of FooManChu).
  138. ** @param beanClass the class the get and set methods live in.
  139. ** @param getMethodName the name of the get method or <code>null</code> if the property is write-only.
  140. ** @param setMethodName the name of the set method or <code>null</code> if the property is read-only.
  141. ** @exception IntrospectionException if the methods are not found
  142. ** or invalid.
  143. **/
  144. public PropertyDescriptor(
  145. String name,
  146. Class<?> beanClass,
  147. String getMethodName,
  148. String setMethodName)
  149. throws IntrospectionException
  150. {
  151. setName(name);
  152. findMethods(beanClass, getMethodName, null, setMethodName);
  153. if (getMethod == null && getMethodName != null)
  154. {
  155. throw new IntrospectionException(
  156. "Cannot find a getter method called " + getMethodName);
  157. }
  158. if (setMethod == null && setMethodName != null)
  159. {
  160. throw new IntrospectionException(
  161. "Cannot find a setter method called " + setMethodName);
  162. }
  163. propertyType = checkMethods(getMethod, setMethod);
  164. }
  165. /** Create a new PropertyDescriptor using explicit Methods.
  166. ** Note that the methods will be checked for conformance to standard
  167. ** Property method rules, as described above at the top of this class.
  168. **<br>
  169. ** It is possible to call this method with both <code>Method</code> arguments
  170. ** being <code>null</code>. In such a case the property type is <code>null</code>.
  171. **
  172. ** @param name the programmatic name of the property, usually
  173. ** starting with a lowercase letter (e.g. fooManChu
  174. ** instead of FooManChu).
  175. ** @param readMethod the read method or <code>null</code> if the property is write-only.
  176. ** @param writeMethod the write method or <code>null</code> if the property is read-only.
  177. ** @exception IntrospectionException if the methods are not found
  178. ** or invalid.
  179. **/
  180. public PropertyDescriptor(
  181. String name,
  182. Method readMethod,
  183. Method writeMethod)
  184. throws IntrospectionException
  185. {
  186. setName(name);
  187. getMethod = readMethod;
  188. setMethod = writeMethod;
  189. propertyType = checkMethods(getMethod, setMethod);
  190. }
  191. /** Get the property type.
  192. ** This is the type the get method returns and the set method
  193. ** takes in.
  194. **/
  195. public Class<?> getPropertyType()
  196. {
  197. return propertyType;
  198. }
  199. /** Get the get method. Why they call it readMethod here and
  200. ** get everywhere else is beyond me.
  201. **/
  202. public Method getReadMethod()
  203. {
  204. return getMethod;
  205. }
  206. /** Sets the read method.<br/>
  207. * The read method is used to retrieve the value of a property. A legal
  208. * read method must have no arguments. Its return type must not be
  209. * <code>void</code>. If this methods succeeds the property type
  210. * is adjusted to the return type of the read method.<br/>
  211. * <br/>
  212. * It is legal to set the read and the write method to <code>null</code>
  213. * or provide method which have been declared in distinct classes.
  214. *
  215. * @param readMethod The new method to be used or <code>null</code>.
  216. * @throws IntrospectionException If the given method is invalid.
  217. * @since 1.2
  218. */
  219. public void setReadMethod(Method readMethod) throws IntrospectionException
  220. {
  221. propertyType = checkMethods(readMethod, setMethod);
  222. getMethod = readMethod;
  223. }
  224. /** Get the set method. Why they call it writeMethod here and
  225. ** set everywhere else is beyond me.
  226. **/
  227. public Method getWriteMethod()
  228. {
  229. return setMethod;
  230. }
  231. /** Sets the write method.<br/>
  232. * The write method is used to set the value of a property. A legal write method
  233. * must have a single argument which can be assigned to the property. If no
  234. * read method exists the property type changes to the argument type of the
  235. * write method.<br/>
  236. * <br/>
  237. * It is legal to set the read and the write method to <code>null</code>
  238. * or provide method which have been declared in distinct classes.
  239. *
  240. * @param writeMethod The new method to be used or <code>null</code>.
  241. * @throws IntrospectionException If the given method is invalid.
  242. * @since 1.2
  243. */
  244. public void setWriteMethod(Method writeMethod)
  245. throws IntrospectionException
  246. {
  247. propertyType = checkMethods(getMethod, writeMethod);
  248. setMethod = writeMethod;
  249. }
  250. /** Get whether the property is bound. Defaults to false. **/
  251. public boolean isBound()
  252. {
  253. return bound;
  254. }
  255. /** Set whether the property is bound.
  256. ** As long as the the bean implements addPropertyChangeListener() and
  257. ** removePropertyChangeListener(), setBound(true) may safely be called.<P>
  258. ** If these things are not true, then the behavior of the system
  259. ** will be undefined.<P>
  260. **
  261. ** When a property is bound, its set method is required to fire the
  262. ** <CODE>PropertyChangeListener.propertyChange())</CODE> event
  263. ** after the value has changed.
  264. ** @param bound whether the property is bound or not.
  265. **/
  266. public void setBound(boolean bound)
  267. {
  268. this.bound = bound;
  269. }
  270. /** Get whether the property is constrained. Defaults to false. **/
  271. public boolean isConstrained()
  272. {
  273. return constrained;
  274. }
  275. /** Set whether the property is constrained.
  276. ** If the set method throws <CODE>java.beans.PropertyVetoException</CODE>
  277. ** (or subclass thereof) and the bean implements addVetoableChangeListener()
  278. ** and removeVetoableChangeListener(), then setConstrained(true) may safely
  279. ** be called. Otherwise, the system behavior is undefined.
  280. ** <B>Spec note:</B> given those strict parameters, it would be nice if it
  281. ** got set automatically by detection, but oh well.<P>
  282. ** When a property is constrained, its set method is required to:<P>
  283. ** <OL>
  284. ** <LI>Fire the <CODE>VetoableChangeListener.vetoableChange()</CODE>
  285. ** event notifying others of the change and allowing them a chance to
  286. ** say it is a bad thing.</LI>
  287. ** <LI>If any of the listeners throws a PropertyVetoException, then
  288. ** it must fire another vetoableChange() event notifying the others
  289. ** of a reversion to the old value (though, of course, the change
  290. ** was never made). Then it rethrows the PropertyVetoException and
  291. ** exits.</LI>
  292. ** <LI>If all has gone well to this point, the value may be changed.</LI>
  293. ** </OL>
  294. ** @param constrained whether the property is constrained or not.
  295. **/
  296. public void setConstrained(boolean constrained)
  297. {
  298. this.constrained = constrained;
  299. }
  300. /** Get the PropertyEditor class. Defaults to null. **/
  301. public Class<?> getPropertyEditorClass()
  302. {
  303. return propertyEditorClass;
  304. }
  305. /** Set the PropertyEditor class. If the class does not implement
  306. ** the PropertyEditor interface, you will likely get an exception
  307. ** late in the game.
  308. ** @param propertyEditorClass the PropertyEditor class for this
  309. ** class to use.
  310. **/
  311. public void setPropertyEditorClass(Class<?> propertyEditorClass)
  312. {
  313. this.propertyEditorClass = propertyEditorClass;
  314. }
  315. /**
  316. * Instantiate a property editor using the property editor class.
  317. * If no property editor class has been set, this will return null.
  318. * If the editor class has a public constructor which takes a single
  319. * argument, that will be used and the bean parameter will be passed
  320. * to it. Otherwise, a public no-argument constructor will be used,
  321. * if available. This method will return null if no constructor is
  322. * found or if construction fails for any reason.
  323. * @param bean the argument to the constructor
  324. * @return a new PropertyEditor, or null on error
  325. * @since 1.5
  326. */
  327. public PropertyEditor createPropertyEditor(Object bean)
  328. {
  329. if (propertyEditorClass == null)
  330. return null;
  331. Constructor c = findConstructor(propertyEditorClass,
  332. new Class[] { Object.class });
  333. if (c != null)
  334. return instantiateClass(c, new Object[] { bean });
  335. c = findConstructor(propertyEditorClass, null);
  336. if (c != null)
  337. return instantiateClass(c, null);
  338. return null;
  339. }
  340. // Helper method to look up a constructor and return null if it is not
  341. // found.
  342. private Constructor findConstructor(Class k, Class[] argTypes)
  343. {
  344. try
  345. {
  346. return k.getConstructor(argTypes);
  347. }
  348. catch (NoSuchMethodException _)
  349. {
  350. return null;
  351. }
  352. }
  353. // Helper method to instantiate an object but return null on error.
  354. private PropertyEditor instantiateClass(Constructor c, Object[] args)
  355. {
  356. try
  357. {
  358. return (PropertyEditor) c.newInstance(args);
  359. }
  360. catch (InstantiationException _)
  361. {
  362. return null;
  363. }
  364. catch (InvocationTargetException _)
  365. {
  366. return null;
  367. }
  368. catch (IllegalAccessException _)
  369. {
  370. return null;
  371. }
  372. catch (ClassCastException _)
  373. {
  374. return null;
  375. }
  376. }
  377. private void findMethods(
  378. Class beanClass,
  379. String getMethodName1,
  380. String getMethodName2,
  381. String setMethodName)
  382. throws IntrospectionException
  383. {
  384. try
  385. {
  386. // Try the first get method name
  387. if (getMethodName1 != null)
  388. {
  389. try
  390. {
  391. getMethod =
  392. beanClass.getMethod(getMethodName1, new Class[0]);
  393. }
  394. catch (NoSuchMethodException e)
  395. {}
  396. }
  397. // Fall back to the second get method name
  398. if (getMethod == null && getMethodName2 != null)
  399. {
  400. try
  401. {
  402. getMethod =
  403. beanClass.getMethod(getMethodName2, new Class[0]);
  404. }
  405. catch (NoSuchMethodException e)
  406. {}
  407. }
  408. // Try the set method name
  409. if (setMethodName != null)
  410. {
  411. if (getMethod != null)
  412. {
  413. // If there is a get method, use its return type to help
  414. // select the corresponding set method.
  415. Class propertyType = getMethod.getReturnType();
  416. if (propertyType == Void.TYPE)
  417. {
  418. String msg =
  419. "The property's read method has return type 'void'";
  420. throw new IntrospectionException(msg);
  421. }
  422. Class[] setArgs = new Class[] { propertyType };
  423. try
  424. {
  425. setMethod = beanClass.getMethod(setMethodName, setArgs);
  426. }
  427. catch (NoSuchMethodException e)
  428. {}
  429. }
  430. else if (getMethodName1 == null && getMethodName2 == null)
  431. {
  432. // If this is a write-only property, choose the first set method
  433. // with the required name, one parameter and return type 'void'
  434. Method[] methods = beanClass.getMethods();
  435. for (int i = 0; i < methods.length; i++)
  436. {
  437. if (methods[i].getName().equals(setMethodName)
  438. && methods[i].getParameterTypes().length == 1
  439. && methods[i].getReturnType() == Void.TYPE)
  440. {
  441. setMethod = methods[i];
  442. break;
  443. }
  444. }
  445. }
  446. }
  447. }
  448. catch (SecurityException e)
  449. {
  450. // FIXME -- shouldn't we just allow SecurityException to propagate?
  451. String msg =
  452. "SecurityException thrown on attempt to access methods.";
  453. throw new IntrospectionException(msg);
  454. }
  455. }
  456. /** Checks whether the given <code>Method</code> instances are legal read and
  457. * write methods. The following requirements must be met:<br/>
  458. * <ul>
  459. * <li>the read method must not have an argument</li>
  460. * <li>the read method must have a non void return type</li>
  461. * <li>the read method may not exist</li>
  462. * <li>the write method must have a single argument</li>
  463. * <li>the property type and the read method's return type must be assignable from the
  464. * write method's argument type</li>
  465. * <li>the write method may not exist</li>
  466. * </ul>
  467. * While checking the methods a common new property type is calculated. If the method
  468. * succeeds this property type is returned.<br/>
  469. * <br/>
  470. * For compatibility this has to be noted:<br/>
  471. * The two methods are allowed to be defined in two distinct classes and may both be null.
  472. *
  473. * @param readMethod The new read method to check.
  474. * @param writeMethod The new write method to check.
  475. * @return The common property type of the two method.
  476. * @throws IntrospectionException If any of the above requirements are not met.
  477. */
  478. private Class<?> checkMethods(Method readMethod, Method writeMethod)
  479. throws IntrospectionException
  480. {
  481. Class<?> newPropertyType = propertyType;
  482. // a valid read method has zero arguments and a non-void return type.
  483. if (readMethod != null)
  484. {
  485. if (readMethod.getParameterTypes().length > 0)
  486. {
  487. throw new IntrospectionException("read method has unexpected parameters");
  488. }
  489. newPropertyType = readMethod.getReturnType();
  490. if (newPropertyType == Void.TYPE)
  491. {
  492. throw new IntrospectionException("read method return type is void");
  493. }
  494. }
  495. // a valid write method has one argument which can be assigned to the property
  496. if (writeMethod != null)
  497. {
  498. if (writeMethod.getParameterTypes().length != 1)
  499. {
  500. String msg = "write method does not have exactly one parameter";
  501. throw new IntrospectionException(msg);
  502. }
  503. if (readMethod == null)
  504. {
  505. // changes the property type if there is no read method
  506. newPropertyType = writeMethod.getParameterTypes()[0];
  507. }
  508. else
  509. {
  510. // checks whether the write method can be assigned to the return type of the read
  511. // method (if this is not the case, the methods are not compatible)
  512. // note: newPropertyType may be null if no methods or method names have been
  513. // delivered in the constructor.
  514. if (newPropertyType != null
  515. && !newPropertyType.isAssignableFrom(
  516. writeMethod.getParameterTypes()[0]))
  517. {
  518. // note: newPropertyType is the same as readMethod.getReturnType() at this point
  519. throw new IntrospectionException("read and write method are not compatible");
  520. }
  521. /* note: the check whether both method are defined in related classes makes sense but is not
  522. * done in the JDK.
  523. * I leave this code here in case someone at Sun decides to add that functionality in later versions (rschuster)
  524. if ((!readMethod
  525. .getDeclaringClass()
  526. .isAssignableFrom(writeMethod.getDeclaringClass()))
  527. && (!writeMethod
  528. .getDeclaringClass()
  529. .isAssignableFrom(readMethod.getDeclaringClass())))
  530. {
  531. String msg =
  532. "set and get methods are not in the same class.";
  533. throw new IntrospectionException(msg);
  534. }
  535. */
  536. }
  537. }
  538. return newPropertyType;
  539. }
  540. /**
  541. * Return a hash code for this object, conforming to the contract described
  542. * in {@link Object#hashCode()}.
  543. * @return the hash code
  544. * @since 1.5
  545. */
  546. public int hashCode()
  547. {
  548. return ((propertyType == null ? 0 : propertyType.hashCode())
  549. | (propertyEditorClass == null ? 0 : propertyEditorClass.hashCode())
  550. | (bound ? Boolean.TRUE : Boolean.FALSE).hashCode()
  551. | (constrained ? Boolean.TRUE : Boolean.FALSE).hashCode()
  552. | (getMethod == null ? 0 : getMethod.hashCode())
  553. | (setMethod == null ? 0 : setMethod.hashCode()));
  554. }
  555. /** Compares this <code>PropertyDescriptor</code> against the
  556. * given object.
  557. * Two PropertyDescriptors are equals if
  558. * <ul>
  559. * <li>the read methods are equal</li>
  560. * <li>the write methods are equal</li>
  561. * <li>the property types are equals</li>
  562. * <li>the property editor classes are equal</li>
  563. * <li>the flags (constrained and bound) are equal</li>
  564. * </ul>
  565. * @return Whether both objects are equal according to the rules given above.
  566. * @since 1.4
  567. */
  568. public boolean equals(Object o)
  569. {
  570. if (o instanceof PropertyDescriptor)
  571. {
  572. PropertyDescriptor that = (PropertyDescriptor) o;
  573. // compares the property types and checks the case where both are null
  574. boolean samePropertyType =
  575. (propertyType == null)
  576. ? that.propertyType == null
  577. : propertyType.equals(that.propertyType);
  578. // compares the property editor classes and checks the case where both are null
  579. boolean samePropertyEditorClass =
  580. (propertyEditorClass == null)
  581. ? that.propertyEditorClass == null
  582. : propertyEditorClass.equals(that.propertyEditorClass);
  583. // compares the flags for equality
  584. boolean sameFlags =
  585. bound == that.bound && constrained == that.constrained;
  586. // compares the read methods and checks the case where both are null
  587. boolean sameReadMethod =
  588. (getMethod == null)
  589. ? that.getMethod == null
  590. : getMethod.equals(that.getMethod);
  591. boolean sameWriteMethod =
  592. (setMethod == null)
  593. ? that.setMethod == null
  594. : setMethod.equals(that.setMethod);
  595. return samePropertyType
  596. && sameFlags
  597. && sameReadMethod
  598. && sameWriteMethod
  599. && samePropertyEditorClass;
  600. }
  601. else
  602. {
  603. return false;
  604. }
  605. }
  606. }