home *** CD-ROM | disk | FTP | other *** search
/ Java 1.2 How-To / JavaHowTo.iso / 3rdParty / jbuilder / unsupported / JDK1.2beta3 / SOURCE / SRC.ZIP / java / beans / IndexedPropertyDescriptor.java < prev    next >
Encoding:
Java Source  |  1998-03-20  |  9.1 KB  |  245 lines

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