home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / IndexedPropertyDescriptor.java < prev    next >
Text File  |  1997-05-20  |  9KB  |  235 lines

  1. /*
  2.  * @(#)IndexedPropertyDescriptor.java    1.17 96/12/06  
  3.  * 
  4.  * Copyright (c) 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion bdk_beta
  20.  * 
  21.  */
  22.  
  23. package java.beans;
  24.  
  25. import java.lang.reflect.*;
  26.  
  27. /**
  28.  * An IndexedPropertyDescriptor describes a property that acts like an
  29.  * array and has an indexed read and/or indexed write method to access
  30.  * specific elements of the array.
  31.  * <p>
  32.  * An indexed property may also provide simple non-indexed read and write
  33.  * methods.  If these are present, they read and write arrays of the type
  34.  * returned by the indexed read method.
  35.  */
  36.  
  37. public class IndexedPropertyDescriptor extends PropertyDescriptor {
  38.  
  39.     /**
  40.      * This constructor constructs an IndexedPropertyDescriptor for a property
  41.      * that follows the standard Java conventions by having getFoo and setFoo 
  42.      * accessor methods, for both indexed access and array access.
  43.      * <p>
  44.      * Thus if the argument name is "fred", it will assume that there
  45.      * is an indexed reader method "getFred", a non-indexed (array) reader
  46.      * method also called "getFred", an indexed writer method "setFred",
  47.      * and finally a non-indexed writer method "setFred".
  48.      *
  49.      * @param propertyName The programmatic name of the property.
  50.      * @param beanClass The Class object for the target bean.
  51.      * @exception IntrospectionException if an exception occurs during
  52.      *              introspection.
  53.      */
  54.     public IndexedPropertyDescriptor(String propertyName, Class beanClass)
  55.         throws IntrospectionException {
  56.     this(propertyName, beanClass,
  57.              "get" + capitalize(propertyName),
  58.              "set" + capitalize(propertyName),
  59.              "get" + capitalize(propertyName),
  60.              "set" + capitalize(propertyName));
  61.     }
  62.  
  63.     /**
  64.      * This constructor takes the name of a simple property, and method
  65.      * names for reading and writing the property, both indexed
  66.      * and non-indexed.
  67.      *
  68.      * @param propertyName The programmatic name of the property.
  69.      * @param beanClass  The Class object for the target bean.
  70.      * @param getterName The name of the method used for reading the property
  71.      *         values as an array.  May be null if the property is write-only
  72.      *         or must be indexed.
  73.      * @param setterName The name of the method used for writing the property
  74.      *         values as an array.  May be null if the property is read-only
  75.      *         or must be indexed.
  76.      * @param indexedGetterName The name of the method used for reading
  77.      *        an indexed property value.
  78.      *        May be null if the property is write-only.
  79.      * @param indexedSetterName The name of the method used for writing
  80.      *        an indexed property value.  
  81.      *        May be null if the property is read-only.
  82.      * @exception IntrospectionException if an exception occurs during
  83.      *              introspection.
  84.      */
  85.     public IndexedPropertyDescriptor(String propertyName, Class beanClass,
  86.         String getterName, String setterName,
  87.         String indexedGetterName, String indexedSetterName)
  88.         throws IntrospectionException {
  89.     super(propertyName, beanClass, getterName, setterName);
  90.     indexedReadMethod = Introspector.findMethod(beanClass, indexedGetterName, 1);
  91.     indexedWriteMethod = Introspector.findMethod(beanClass, indexedSetterName, 2);
  92.     findIndexedPropertyType();
  93.     }
  94.  
  95.     /**
  96.      * This constructor takes the name of a simple property, and Method
  97.      * objects for reading and writing the property.
  98.      *
  99.      * @param propertyName The programmatic name of the property.
  100.      * @param getter The method used for reading the property values as an array.
  101.      *        May be null if the property is write-only or must be indexed.
  102.      * @param setter The method used for writing the property values as an array.
  103.      *        May be null if the property is read-only or must be indexed.
  104.      * @param indexedGetter The method used for reading an indexed property value.
  105.      *        May be null if the property is write-only.
  106.      * @param indexedSetter The method used for writing an indexed property value.  
  107.      *        May be null if the property is read-only.
  108.      * @exception IntrospectionException if an exception occurs during
  109.      *              introspection.
  110.      */
  111.     public IndexedPropertyDescriptor(String propertyName, Method getter, Method setter,
  112.                          Method indexedGetter, Method indexedSetter)
  113.         throws IntrospectionException {
  114.     super(propertyName, getter, setter);
  115.     indexedReadMethod = indexedGetter;
  116.     indexedWriteMethod = indexedSetter;
  117.     findIndexedPropertyType();
  118.     }
  119.     
  120.     /**
  121.      * @return The method that should be used to read an indexed
  122.      * property value.
  123.      * May return null if the property isn't indexed or is write-only.
  124.      */
  125.     public Method getIndexedReadMethod() {
  126.     return indexedReadMethod;
  127.     }
  128.  
  129.     /**
  130.      * @return The method that should be used to write an indexed
  131.      * property value.
  132.      * May return null if the property isn't indexed or is read-only.
  133.      */
  134.     public Method getIndexedWriteMethod() {
  135.     return indexedWriteMethod;
  136.     }
  137.  
  138.     /**
  139.      * @return The Java Class for the indexed properties type.  Note that
  140.      * the Class may describe a primitive Java type such as "int".
  141.      * <p>
  142.      * This is the type that will be returned by the indexedReadMethod.
  143.      */
  144.     public Class getIndexedPropertyType() {
  145.     return indexedPropertyType;
  146.     }
  147.  
  148.  
  149.     private void findIndexedPropertyType() throws IntrospectionException {
  150.     try {
  151.         indexedPropertyType = null;
  152.         if (indexedReadMethod != null) {
  153.         Class params[] = indexedReadMethod.getParameterTypes();
  154.         if (params.length != 1) {
  155.             throw new IntrospectionException("bad indexed read method arg count");
  156.         }
  157.         if (params[0] != Integer.TYPE) {
  158.             throw new IntrospectionException("non int index to indexed read method");
  159.         }
  160.         indexedPropertyType = indexedReadMethod.getReturnType();
  161.         if (indexedPropertyType == Void.TYPE) {
  162.             throw new IntrospectionException("indexed read method returns void");
  163.         }
  164.         }
  165.         if (indexedWriteMethod != null) {
  166.         Class params[] = indexedWriteMethod.getParameterTypes();
  167.         if (params.length != 2) {
  168.             throw new IntrospectionException("bad indexed write method arg count");
  169.         }
  170.         if (params[0] != Integer.TYPE) {
  171.             throw new IntrospectionException("non int index to indexed write method");
  172.         }
  173.         if (indexedPropertyType != null && indexedPropertyType != params[1]) {
  174.             throw new IntrospectionException(
  175.             "type mismatch between indexed read and indexed write methods");
  176.         }
  177.         indexedPropertyType = params[1];
  178.         }
  179.         if (indexedPropertyType == null) {
  180.             throw new IntrospectionException(
  181.             "no indexed getter or setter");
  182.         }
  183.         Class propertyType = getPropertyType();
  184.         if (propertyType != null && (!propertyType.isArray() ||
  185.             propertyType.getComponentType() != indexedPropertyType)) {
  186.             throw new IntrospectionException(
  187.             "type mismatch between indexed and non-indexed methods");
  188.         }
  189.     } catch (IntrospectionException ex) {
  190.         throw ex;
  191.     }
  192.     }
  193.  
  194.  
  195.     /*
  196.      * Package-private constructor.
  197.      * Merge two property descriptors.  Where they conflict, give the
  198.      * second argument (y) priority over the first argumnnt (x).
  199.      * @param x  The first (lower priority) PropertyDescriptor
  200.      * @param y  The second (higher priority) PropertyDescriptor
  201.      */
  202.  
  203.     IndexedPropertyDescriptor(PropertyDescriptor x, PropertyDescriptor y) {
  204.     super(x,y);
  205.     if (x instanceof IndexedPropertyDescriptor) {
  206.         IndexedPropertyDescriptor ix = (IndexedPropertyDescriptor)x;
  207.         indexedReadMethod = ix.indexedReadMethod;
  208.         indexedWriteMethod = ix.indexedWriteMethod;
  209.         indexedPropertyType = ix.indexedPropertyType;
  210.     }
  211.     if (y instanceof IndexedPropertyDescriptor) {
  212.         IndexedPropertyDescriptor iy = (IndexedPropertyDescriptor)y;
  213.         if (iy.indexedReadMethod != null) {
  214.             indexedReadMethod = iy.indexedReadMethod;
  215.         }
  216.         if (iy.indexedWriteMethod != null) {
  217.             indexedWriteMethod = iy.indexedWriteMethod;
  218.         }
  219.         indexedPropertyType = iy.indexedPropertyType;
  220.     }
  221.     
  222.     }
  223.  
  224.  
  225.     private static String capitalize(String s) {
  226.     char chars[] = s.toCharArray();
  227.     chars[0] = Character.toUpperCase(chars[0]);
  228.     return new String(chars);
  229.     }
  230.  
  231.     private Class indexedPropertyType;
  232.     private Method indexedReadMethod;
  233.     private Method indexedWriteMethod;
  234. }
  235.