IndexedPropertyDescriptor.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. /* IndexedPropertyDescriptor.java --
  2. Copyright (C) 1998, 2003 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.Array;
  33. import java.lang.reflect.Method;
  34. /**
  35. * IndexedPropertyDescriptor describes information about a JavaBean
  36. * indexed property, by which we mean an array-like property that
  37. * has been exposed via a pair of get and set methods and another
  38. * pair that allows you to get to the property by an index.<P>
  39. *
  40. * An example property would have four methods like this:<P>
  41. * <CODE>FooBar[] getFoo()</CODE><BR>
  42. * <CODE>void setFoo(FooBar[])</CODE><BR>
  43. * <CODE>FooBar getFoo(int)</CODE><BR>
  44. * <CODE>void setFoo(int,FooBar)</CODE><P>
  45. *
  46. * The constraints put on get and set methods are:<P>
  47. * <OL>
  48. * <LI>There must be at least a get(int) or a set(int,...) method.
  49. * Nothing else is required. <B>Spec note:</B>One nice restriction
  50. * would be that if there is a get() there must be a get(int), same
  51. * with set, but that is not in the spec and is fairly harmless.)</LI>
  52. * <LI>A get array method must have signature
  53. * <CODE>&lt;propertyType&gt;[] &lt;getMethodName&gt;()</CODE></LI>
  54. * <LI>A set array method must have signature
  55. * <CODE>void &lt;setMethodName&gt;(&lt;propertyType&gt;[])</CODE></LI>
  56. * <LI>A get index method must have signature
  57. * <CODE>&lt;propertyType&gt; &lt;getMethodName&gt;(int)</CODE></LI>
  58. * <LI>A set index method must have signature
  59. * <CODE>void &lt;setMethodName&gt;(int,&lt;propertyType&gt;)</CODE></LI>
  60. * <LI>All these methods may throw any exception.</LI>
  61. * <LI>All these methods must be public.</LI>
  62. * </OL>
  63. *
  64. * @author John Keiser
  65. * @since JDK1.1
  66. */
  67. public class IndexedPropertyDescriptor extends PropertyDescriptor
  68. {
  69. private Class<?> indexedPropertyType;
  70. private Method setIndex;
  71. private Method getIndex;
  72. /**
  73. * Create a new IndexedPropertyDescriptor by introspection.
  74. * This form of constructor creates the PropertyDescriptor by
  75. * looking for getter methods named <CODE>get&lt;name&gt;()</CODE>
  76. * and setter methods named
  77. * <CODE>set&lt;name&gt;()</CODE> in class
  78. * <CODE>&lt;beanClass&gt;</CODE>, where &lt;name&gt; has its
  79. * first letter capitalized by the constructor.<P>
  80. *
  81. * <B>Implementation note:</B> If there is a get(int) method,
  82. * then the return type of that method is used to find the
  83. * remaining methods. If there is no get method, then the
  84. * set(int) method is searched for exhaustively and that type
  85. * is used to find the others.<P>
  86. *
  87. * <B>Spec note:</B>
  88. * If there is no get(int) method and multiple set(int) methods with
  89. * the same name and the correct parameters (different type of course),
  90. * then an IntrospectionException is thrown. While Sun's spec
  91. * does not state this, it can make Bean behavior different on
  92. * different systems (since method order is not guaranteed) and as
  93. * such, can be treated as a bug in the spec. I am not aware of
  94. * whether Sun's implementation catches this.
  95. *
  96. * @param name the programmatic name of the property, usually
  97. * starting with a lowercase letter (e.g. fooManChu
  98. * instead of FooManChu).
  99. * @param beanClass the class the get and set methods live in.
  100. *
  101. * @exception IntrospectionException if the methods are not found or
  102. * invalid.
  103. */
  104. public IndexedPropertyDescriptor(String name, Class<?> beanClass)
  105. throws IntrospectionException
  106. {
  107. super(name);
  108. String capitalized;
  109. try
  110. {
  111. capitalized = Character.toUpperCase(name.charAt(0))
  112. + name.substring(1);
  113. }
  114. catch(StringIndexOutOfBoundsException e)
  115. {
  116. capitalized = "";
  117. }
  118. findMethods(beanClass, "get" + capitalized, "set" + capitalized,
  119. "get" + capitalized, "set" + capitalized);
  120. }
  121. /**
  122. * Create a new IndexedPropertyDescriptor by introspection.
  123. * This form of constructor allows you to specify the
  124. * names of the get and set methods to search for.<P>
  125. *
  126. * <B>Implementation note:</B> If there is a get(int) method,
  127. * then the return type of that method is used to find the
  128. * remaining methods. If there is no get method, then the
  129. * set(int) method is searched for exhaustively and that type
  130. * is used to find the others.<P>
  131. *
  132. * <B>Spec note:</B>
  133. * If there is no get(int) method and multiple set(int) methods with
  134. * the same name and the correct parameters (different type of course),
  135. * then an IntrospectionException is thrown. While Sun's spec
  136. * does not state this, it can make Bean behavior different on
  137. * different systems (since method order is not guaranteed) and as
  138. * such, can be treated as a bug in the spec. I am not aware of
  139. * whether Sun's implementation catches this.
  140. *
  141. * @param name the programmatic name of the property, usually
  142. * starting with a lowercase letter (e.g. fooManChu
  143. * instead of FooManChu).
  144. * @param beanClass the class the get and set methods live in.
  145. * @param getMethodName the name of the get array method.
  146. * @param setMethodName the name of the set array method.
  147. * @param getIndexName the name of the get index method.
  148. * @param setIndexName the name of the set index method.
  149. *
  150. * @exception IntrospectionException if the methods are not found or invalid.
  151. */
  152. public IndexedPropertyDescriptor(String name, Class<?> beanClass,
  153. String getMethodName, String setMethodName,
  154. String getIndexName, String setIndexName)
  155. throws IntrospectionException
  156. {
  157. super(name);
  158. findMethods(beanClass, getMethodName, setMethodName, getIndexName,
  159. setIndexName);
  160. }
  161. /**
  162. * Create a new PropertyDescriptor using explicit Methods.
  163. * Note that the methods will be checked for conformance to standard
  164. * Property method rules, as described above at the top of this class.
  165. *
  166. * @param name the programmatic name of the property, usually
  167. * starting with a lowercase letter (e.g. fooManChu
  168. * instead of FooManChu).
  169. * @param getMethod the get array method.
  170. * @param setMethod the set array method.
  171. * @param getIndex the get index method.
  172. * @param setIndex the set index method.
  173. *
  174. * @exception IntrospectionException if the methods are not found or invalid.
  175. */
  176. public IndexedPropertyDescriptor(String name, Method getMethod,
  177. Method setMethod, Method getIndex,
  178. Method setIndex)
  179. throws IntrospectionException
  180. {
  181. super(name);
  182. if(getMethod != null && getMethod.getParameterTypes().length > 0)
  183. throw new IntrospectionException("get method has parameters");
  184. if(getMethod != null && setMethod.getParameterTypes().length != 1)
  185. throw new IntrospectionException("set method does not have exactly one parameter");
  186. if(getMethod != null && setMethod != null)
  187. {
  188. if(!getMethod.getReturnType().equals(setMethod.getParameterTypes()[0]))
  189. {
  190. throw new IntrospectionException("set and get methods do not "
  191. + "share the same type");
  192. }
  193. if(!getMethod.getDeclaringClass().isAssignableFrom
  194. (setMethod.getDeclaringClass())
  195. && !setMethod.getDeclaringClass().isAssignableFrom
  196. (getMethod.getDeclaringClass()))
  197. {
  198. throw new IntrospectionException("set and get methods are not in "
  199. + "the same class.");
  200. }
  201. }
  202. if (getIndex != null
  203. && (getIndex.getParameterTypes().length != 1
  204. || !(getIndex.getParameterTypes()[0]).equals(java.lang.Integer.TYPE)))
  205. {
  206. throw new IntrospectionException("get index method has wrong "
  207. + "parameters");
  208. }
  209. if (setIndex != null
  210. && (setIndex.getParameterTypes().length != 2
  211. || !(setIndex.getParameterTypes()[0]).equals(java.lang.Integer.TYPE)))
  212. {
  213. throw new IntrospectionException("set index method has wrong "
  214. + "parameters");
  215. }
  216. if (getIndex != null && setIndex != null)
  217. {
  218. if(!getIndex.getReturnType().equals(setIndex.getParameterTypes()[1]))
  219. {
  220. throw new IntrospectionException("set index methods do not share "
  221. + "the same type");
  222. }
  223. if(!getIndex.getDeclaringClass().isAssignableFrom
  224. (setIndex.getDeclaringClass())
  225. && !setIndex.getDeclaringClass().isAssignableFrom
  226. (getIndex.getDeclaringClass()))
  227. {
  228. throw new IntrospectionException("get and set index methods are "
  229. + "not in the same class.");
  230. }
  231. }
  232. if (getIndex != null && getMethod != null
  233. && !getIndex.getDeclaringClass().isAssignableFrom
  234. (getMethod.getDeclaringClass())
  235. && !getMethod.getDeclaringClass().isAssignableFrom
  236. (getIndex.getDeclaringClass()))
  237. {
  238. throw new IntrospectionException("methods are not in the same class.");
  239. }
  240. if (getIndex != null && getMethod != null
  241. && !Array.newInstance(getIndex.getReturnType(),0)
  242. .getClass().equals(getMethod.getReturnType()))
  243. {
  244. throw new IntrospectionException("array methods do not match index "
  245. + "methods.");
  246. }
  247. this.getMethod = getMethod;
  248. this.setMethod = setMethod;
  249. this.getIndex = getIndex;
  250. this.setIndex = setIndex;
  251. this.indexedPropertyType = getIndex != null ? getIndex.getReturnType()
  252. : setIndex.getParameterTypes()[1];
  253. this.propertyType = getMethod != null ? getMethod.getReturnType()
  254. : (setMethod != null ? setMethod.getParameterTypes()[0]
  255. : Array.newInstance(this.indexedPropertyType,0).getClass());
  256. }
  257. public Class<?> getIndexedPropertyType()
  258. {
  259. return indexedPropertyType;
  260. }
  261. public Method getIndexedReadMethod()
  262. {
  263. return getIndex;
  264. }
  265. /**
  266. * Sets the method that is used to read an indexed property.
  267. *
  268. * @param m the method to set
  269. */
  270. public void setIndexedReadMethod(Method m) throws IntrospectionException
  271. {
  272. getIndex = m;
  273. }
  274. public Method getIndexedWriteMethod()
  275. {
  276. return setIndex;
  277. }
  278. /**
  279. * Sets the method that is used to write an indexed property.
  280. *
  281. * @param m the method to set
  282. */
  283. public void setIndexedWriteMethod(Method m) throws IntrospectionException
  284. {
  285. setIndex = m;
  286. }
  287. private void findMethods(Class beanClass, String getMethodName,
  288. String setMethodName, String getIndexName,
  289. String setIndexName)
  290. throws IntrospectionException
  291. {
  292. try
  293. {
  294. if(getIndexName != null)
  295. {
  296. try
  297. {
  298. Class[] getArgs = new Class[1];
  299. getArgs[0] = java.lang.Integer.TYPE;
  300. getIndex = beanClass.getMethod(getIndexName,getArgs);
  301. indexedPropertyType = getIndex.getReturnType();
  302. }
  303. catch(NoSuchMethodException E)
  304. {
  305. }
  306. }
  307. if(getIndex != null)
  308. {
  309. if(setIndexName != null)
  310. {
  311. try
  312. {
  313. Class[] setArgs = new Class[2];
  314. setArgs[0] = java.lang.Integer.TYPE;
  315. setArgs[1] = indexedPropertyType;
  316. setIndex = beanClass.getMethod(setIndexName,setArgs);
  317. if(!setIndex.getReturnType().equals(java.lang.Void.TYPE))
  318. {
  319. throw new IntrospectionException(setIndexName
  320. + " has non-void return type");
  321. }
  322. }
  323. catch(NoSuchMethodException E)
  324. {
  325. }
  326. }
  327. }
  328. else if(setIndexName != null)
  329. {
  330. Method[] m = beanClass.getMethods();
  331. for(int i=0;i<m.length;i++)
  332. {
  333. Method current = m[i];
  334. if(current.getName().equals(setIndexName)
  335. && current.getParameterTypes().length == 2
  336. && (current.getParameterTypes()[0])
  337. .equals(java.lang.Integer.TYPE)
  338. && current.getReturnType().equals(java.lang.Void.TYPE))
  339. {
  340. if(setIndex != null)
  341. {
  342. throw new IntrospectionException("Multiple, different "
  343. + "set methods found that fit the bill!");
  344. }
  345. else
  346. {
  347. setIndex = current;
  348. indexedPropertyType = current.getParameterTypes()[1];
  349. }
  350. }
  351. }
  352. if(setIndex == null)
  353. {
  354. throw new IntrospectionException("Cannot find get or set "
  355. + "methods.");
  356. }
  357. }
  358. else
  359. {
  360. throw new IntrospectionException("Cannot find get or set methods.");
  361. }
  362. Class arrayType = Array.newInstance(indexedPropertyType,0).getClass();
  363. Class[] setArgs = new Class[1];
  364. setArgs[0] = arrayType;
  365. try
  366. {
  367. setMethod = beanClass.getMethod(setMethodName,setArgs);
  368. if (!setMethod.getReturnType().equals(java.lang.Void.TYPE))
  369. {
  370. setMethod = null;
  371. }
  372. }
  373. catch(NoSuchMethodException E)
  374. {
  375. }
  376. Class[] getArgs = new Class[0];
  377. try
  378. {
  379. getMethod = beanClass.getMethod(getMethodName,getArgs);
  380. if (!getMethod.getReturnType().equals(arrayType))
  381. {
  382. getMethod = null;
  383. }
  384. }
  385. catch(NoSuchMethodException E)
  386. {
  387. }
  388. }
  389. catch(SecurityException E)
  390. {
  391. throw new IntrospectionException("SecurityException while trying to "
  392. + "find methods.");
  393. }
  394. }
  395. }