IndexedPropertyDescriptor.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /* java.beans.IndexedPropertyDescriptor
  2. Copyright (C) 1998 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. ** 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. ** @version 1.1.0, 26 Jul 1998
  67. **/
  68. public class IndexedPropertyDescriptor extends PropertyDescriptor {
  69. private Class indexedPropertyType;
  70. private Method setIndex;
  71. private Method getIndex;
  72. /** Create a new IndexedPropertyDescriptor by introspection.
  73. ** This form of constructor creates the PropertyDescriptor by
  74. ** looking for getter methods named <CODE>get&lt;name&gt;()</CODE>
  75. ** and setter methods named
  76. ** <CODE>set&lt;name&gt;()</CODE> in class
  77. ** <CODE>&lt;beanClass&gt;</CODE>, where &lt;name&gt; has its
  78. ** first letter capitalized by the constructor.<P>
  79. **
  80. ** <B>Implementation note:</B> If there is a get(int) method,
  81. ** then the return type of that method is used to find the
  82. ** remaining methods. If there is no get method, then the
  83. ** set(int) method is searched for exhaustively and that type
  84. ** is used to find the others.<P>
  85. **
  86. ** <B>Spec note:</B>
  87. ** If there is no get(int) method and multiple set(int) methods with
  88. ** the same name and the correct parameters (different type of course),
  89. ** then an IntrospectionException is thrown. While Sun's spec
  90. ** does not state this, it can make Bean behavior different on
  91. ** different systems (since method order is not guaranteed) and as
  92. ** such, can be treated as a bug in the spec. I am not aware of
  93. ** whether Sun's implementation catches this.
  94. **
  95. ** @param name the programmatic name of the property, usually
  96. ** starting with a lowercase letter (e.g. fooManChu
  97. ** instead of FooManChu).
  98. ** @param beanClass the class the get and set methods live in.
  99. ** @exception IntrospectionException if the methods are not found or invalid.
  100. **/
  101. public IndexedPropertyDescriptor(String name, Class beanClass) throws IntrospectionException {
  102. super(name);
  103. String capitalized;
  104. try {
  105. capitalized = Character.toUpperCase(name.charAt(0)) + name.substring(1);
  106. } catch(StringIndexOutOfBoundsException e) {
  107. capitalized = "";
  108. }
  109. findMethods(beanClass, "get" + capitalized, "set" + capitalized, "get" + capitalized, "set" + capitalized);
  110. }
  111. /** Create a new IndexedPropertyDescriptor by introspection.
  112. ** This form of constructor allows you to specify the
  113. ** names of the get and set methods to search for.<P>
  114. **
  115. ** <B>Implementation note:</B> If there is a get(int) method,
  116. ** then the return type of that method is used to find the
  117. ** remaining methods. If there is no get method, then the
  118. ** set(int) method is searched for exhaustively and that type
  119. ** is used to find the others.<P>
  120. **
  121. ** <B>Spec note:</B>
  122. ** If there is no get(int) method and multiple set(int) methods with
  123. ** the same name and the correct parameters (different type of course),
  124. ** then an IntrospectionException is thrown. While Sun's spec
  125. ** does not state this, it can make Bean behavior different on
  126. ** different systems (since method order is not guaranteed) and as
  127. ** such, can be treated as a bug in the spec. I am not aware of
  128. ** whether Sun's implementation catches this.
  129. **
  130. ** @param name the programmatic name of the property, usually
  131. ** starting with a lowercase letter (e.g. fooManChu
  132. ** instead of FooManChu).
  133. ** @param beanClass the class the get and set methods live in.
  134. ** @param getMethodName the name of the get array method.
  135. ** @param setMethodName the name of the set array method.
  136. ** @param getIndexName the name of the get index method.
  137. ** @param setIndexName the name of the set index method.
  138. ** @exception IntrospectionException if the methods are not found or invalid.
  139. **/
  140. public IndexedPropertyDescriptor(String name, Class beanClass, String getMethodName, String setMethodName, String getIndexName, String setIndexName) throws IntrospectionException {
  141. super(name);
  142. findMethods(beanClass, getMethodName, setMethodName, getIndexName, setIndexName);
  143. }
  144. /** Create a new PropertyDescriptor using explicit Methods.
  145. ** Note that the methods will be checked for conformance to standard
  146. ** Property method rules, as described above at the top of this class.
  147. **
  148. ** @param name the programmatic name of the property, usually
  149. ** starting with a lowercase letter (e.g. fooManChu
  150. ** instead of FooManChu).
  151. ** @param getMethod the get array method.
  152. ** @param setMethod the set array method.
  153. ** @param getIndex the get index method.
  154. ** @param setIndex the set index method.
  155. ** @exception IntrospectionException if the methods are not found or invalid.
  156. **/
  157. public IndexedPropertyDescriptor(String name, Method getMethod, Method setMethod, Method getIndex, Method setIndex) throws IntrospectionException {
  158. super(name);
  159. if(getMethod != null && getMethod.getParameterTypes().length > 0) {
  160. throw new IntrospectionException("get method has parameters");
  161. }
  162. if(getMethod != null && setMethod.getParameterTypes().length != 1) {
  163. throw new IntrospectionException("set method does not have exactly one parameter");
  164. }
  165. if(getMethod != null && setMethod != null) {
  166. if(!getMethod.getReturnType().equals(setMethod.getParameterTypes()[0])) {
  167. throw new IntrospectionException("set and get methods do not share the same type");
  168. }
  169. if(!getMethod.getDeclaringClass().isAssignableFrom(setMethod.getDeclaringClass())
  170. && !setMethod.getDeclaringClass().isAssignableFrom(getMethod.getDeclaringClass())) {
  171. throw new IntrospectionException("set and get methods are not in the same class.");
  172. }
  173. }
  174. if(getIndex != null && (getIndex.getParameterTypes().length != 1
  175. || !(getIndex.getParameterTypes()[0]).equals(java.lang.Integer.TYPE))) {
  176. throw new IntrospectionException("get index method has wrong parameters");
  177. }
  178. if(setIndex != null && (setIndex.getParameterTypes().length != 2
  179. || !(setIndex.getParameterTypes()[0]).equals(java.lang.Integer.TYPE))) {
  180. throw new IntrospectionException("set index method has wrong parameters");
  181. }
  182. if(getIndex != null && setIndex != null) {
  183. if(!getIndex.getReturnType().equals(setIndex.getParameterTypes()[1])) {
  184. throw new IntrospectionException("set index methods do not share the same type");
  185. }
  186. if(!getIndex.getDeclaringClass().isAssignableFrom(setIndex.getDeclaringClass())
  187. && !setIndex.getDeclaringClass().isAssignableFrom(getIndex.getDeclaringClass())) {
  188. throw new IntrospectionException("get and set index methods are not in the same class.");
  189. }
  190. }
  191. if(getIndex != null && getMethod != null && !getIndex.getDeclaringClass().isAssignableFrom(getMethod.getDeclaringClass())
  192. && !getMethod.getDeclaringClass().isAssignableFrom(getIndex.getDeclaringClass())) {
  193. throw new IntrospectionException("methods are not in the same class.");
  194. }
  195. if(getIndex != null && getMethod != null && !Array.newInstance(getIndex.getReturnType(),0).getClass().equals(getMethod.getReturnType())) {
  196. throw new IntrospectionException("array methods do not match index methods.");
  197. }
  198. this.getMethod = getMethod;
  199. this.setMethod = setMethod;
  200. this.getIndex = getIndex;
  201. this.setIndex = getIndex;
  202. this.indexedPropertyType = getIndex != null ? getIndex.getReturnType() : setIndex.getParameterTypes()[1];
  203. this.propertyType = getMethod != null ? getMethod.getReturnType() : (setMethod != null ? setMethod.getParameterTypes()[0] : Array.newInstance(this.indexedPropertyType,0).getClass());
  204. }
  205. public Class getIndexedPropertyType() {
  206. return indexedPropertyType;
  207. }
  208. public Method getIndexedReadMethod() {
  209. return getIndex;
  210. }
  211. public Method getIndexedWriteMethod() {
  212. return setIndex;
  213. }
  214. private void findMethods(Class beanClass, String getMethodName, String setMethodName, String getIndexName, String setIndexName) throws IntrospectionException {
  215. try {
  216. if(getIndexName != null) {
  217. try {
  218. Class[] getArgs = new Class[1];
  219. getArgs[0] = java.lang.Integer.TYPE;
  220. getIndex = beanClass.getMethod(getIndexName,getArgs);
  221. indexedPropertyType = getIndex.getReturnType();
  222. } catch(NoSuchMethodException E) {
  223. }
  224. }
  225. if(getIndex != null) {
  226. if(setIndexName != null) {
  227. try {
  228. Class[] setArgs = new Class[2];
  229. setArgs[0] = java.lang.Integer.TYPE;
  230. setArgs[1] = indexedPropertyType;
  231. setIndex = beanClass.getMethod(setIndexName,setArgs);
  232. if(!setIndex.getReturnType().equals(java.lang.Void.TYPE)) {
  233. throw new IntrospectionException(setIndexName + " has non-void return type");
  234. }
  235. } catch(NoSuchMethodException E) {
  236. }
  237. }
  238. } else if(setIndexName != null) {
  239. Method[] m = beanClass.getMethods();
  240. for(int i=0;i<m.length;i++) {
  241. Method current = m[i];
  242. if(current.getName().equals(setIndexName)
  243. && current.getParameterTypes().length == 2
  244. && (current.getParameterTypes()[0]).equals(java.lang.Integer.TYPE)
  245. && current.getReturnType().equals(java.lang.Void.TYPE)) {
  246. if(setIndex != null) {
  247. throw new IntrospectionException("Multiple, different set methods found that fit the bill!");
  248. } else {
  249. setIndex = current;
  250. indexedPropertyType = current.getParameterTypes()[1];
  251. }
  252. }
  253. }
  254. if(setIndex == null) {
  255. throw new IntrospectionException("Cannot find get or set methods.");
  256. }
  257. } else {
  258. throw new IntrospectionException("Cannot find get or set methods.");
  259. }
  260. Class arrayType = Array.newInstance(indexedPropertyType,0).getClass();
  261. Class[] setArgs = new Class[1];
  262. setArgs[0] = arrayType;
  263. try {
  264. setMethod = beanClass.getMethod(setMethodName,setArgs);
  265. if(!setMethod.getReturnType().equals(java.lang.Void.TYPE)) {
  266. setMethod = null;
  267. }
  268. } catch(NoSuchMethodException E) {
  269. }
  270. Class[] getArgs = new Class[0];
  271. try {
  272. getMethod = beanClass.getMethod(getMethodName,getArgs);
  273. if(!getMethod.getReturnType().equals(arrayType)) {
  274. getMethod = null;
  275. }
  276. } catch(NoSuchMethodException E) {
  277. }
  278. } catch(SecurityException E) {
  279. throw new IntrospectionException("SecurityException while trying to find methods.");
  280. }
  281. }
  282. }