⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 propertyutilsbean.java

📁 osworkflow修改版本
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
    // Call the property getter to get the array or list    Object array = invokeMethod(readMethod, bean, new Object[0]);    if(!array.getClass().isArray())    {      if(array instanceof List)      {        // Modify the specified value in the List        ((List)array).set(index, value);      }      else      {        throw new IllegalArgumentException("Property '" + name + "' is not indexed");      }    }    else    {      // Modify the specified value in the array      Array.set(array, index, value);    }  }  /**   * Set the value of the specified mapped property of the   * specified bean, with no type conversions.  The key of the   * value to set must be included (in brackets) as a suffix to   * the property name, or <code>IllegalArgumentException</code> will be   * thrown.   *   * @param bean  Bean whose property is to be set   * @param name  <code>propertyname(key)</code> of the property value   *              to be set   * @param value The property value to be set   * @throws IllegalAccessException    if the caller does not have   *                                   access to the property accessor method   * @throws InvocationTargetException if the property accessor method   *                                   throws an exception   * @throws NoSuchMethodException     if an accessor method for this   *                                   propety cannot be found   */  public void setMappedProperty(Object bean, String name, Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException  {    if(bean == null)    {      throw new IllegalArgumentException("No bean specified");    }    if(name == null)    {      throw new IllegalArgumentException("No name specified");    }    // Identify the index of the requested individual property    int delim = name.indexOf(PropertyUtils.MAPPED_DELIM);    int delim2 = name.indexOf(PropertyUtils.MAPPED_DELIM2);    if((delim < 0) || (delim2 <= delim))    {      throw new IllegalArgumentException("Invalid mapped property '" + name + "'");    }    // Isolate the name and the key    String key = name.substring(delim + 1, delim2);    name = name.substring(0, delim);    // Request the specified indexed property value    setMappedProperty(bean, name, key, value);  }  /**   * Set the value of the specified mapped property of the specified   * bean, with no type conversions.   *   * @param bean  Bean whose property is to be set   * @param name  Mapped property name of the property value to be set   * @param key   Key of the property value to be set   * @param value The property value to be set   * @throws IllegalAccessException    if the caller does not have   *                                   access to the property accessor method   * @throws InvocationTargetException if the property accessor method   *                                   throws an exception   * @throws NoSuchMethodException     if an accessor method for this   *                                   propety cannot be found   */  public void setMappedProperty(Object bean, String name, String key, Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException  {    if(bean == null)    {      throw new IllegalArgumentException("No bean specified");    }    if(name == null)    {      throw new IllegalArgumentException("No name specified");    }    if(key == null)    {      throw new IllegalArgumentException("No key specified");    }    // Retrieve the property descriptor for the specified property    PropertyDescriptor descriptor = getPropertyDescriptor(bean, name);    if(descriptor == null)    {      throw new NoSuchMethodException("Unknown property '" + name + "'");    }    if(descriptor instanceof MappedPropertyDescriptor)    {      // Call the keyed setter method if there is one      Method mappedWriteMethod = ((MappedPropertyDescriptor)descriptor).getMappedWriteMethod();      if(mappedWriteMethod != null)      {        Object params[] = new Object[2];        params[0] = key;        params[1] = value;        invokeMethod(mappedWriteMethod, bean, params);      }      else      {        throw new NoSuchMethodException("Property '" + name + "' has no mapped setter method");      }    }    else    {      /* means that the result has to be retrieved from a map */      Method readMethod = descriptor.getReadMethod();      if(readMethod != null)      {        Object invokeResult = invokeMethod(readMethod, bean, new Object[0]);        /* test and fetch from the map */        if(invokeResult instanceof java.util.Map)        {          ((java.util.Map)invokeResult).put(key, value);        }      }      else      {        throw new NoSuchMethodException("Property '" + name + "' has no mapped getter method");      }    }  }  /**   * Set the value of the (possibly nested) property of the specified   * name, for the specified bean, with no type conversions.   *   * @param bean  Bean whose property is to be modified   * @param name  Possibly nested name of the property to be modified   * @param value Value to which the property is to be set   * @throws IllegalAccessException    if the caller does not have   *                                   access to the property accessor method   * @throws IllegalArgumentException  if <code>bean</code> or   *                                   <code>name</code> is null   * @throws IllegalArgumentException  if a nested reference to a   *                                   property returns null   * @throws InvocationTargetException if the property accessor method   *                                   throws an exception   * @throws NoSuchMethodException     if an accessor method for this   *                                   propety cannot be found   */  public void setNestedProperty(Object bean, String name, Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException  {    if(bean == null)    {      throw new IllegalArgumentException("No bean specified");    }    if(name == null)    {      throw new IllegalArgumentException("No name specified");    }    int indexOfINDEXED_DELIM = -1;    int indexOfMAPPED_DELIM = -1;    while(true)    {      int delim = name.indexOf(PropertyUtils.NESTED_DELIM);      if(delim < 0)      {        break;      }      String next = name.substring(0, delim);      indexOfINDEXED_DELIM = next.indexOf(PropertyUtils.INDEXED_DELIM);      indexOfMAPPED_DELIM = next.indexOf(PropertyUtils.MAPPED_DELIM);      if(bean instanceof Map)      {        bean = ((Map)bean).get(next);      }      else if(indexOfMAPPED_DELIM >= 0)      {        bean = getMappedProperty(bean, next);      }      else if(indexOfINDEXED_DELIM >= 0)      {        bean = getIndexedProperty(bean, next);      }      else      {        bean = getSimpleProperty(bean, next);      }      if(bean == null)      {        throw new IllegalArgumentException("Null property value for '" + name.substring(0, delim) + "'");      }      name = name.substring(delim + 1);    }    indexOfINDEXED_DELIM = name.indexOf(PropertyUtils.INDEXED_DELIM);    indexOfMAPPED_DELIM = name.indexOf(PropertyUtils.MAPPED_DELIM);    if(bean instanceof Map)    {      // check to see if the class has a standard property      PropertyDescriptor descriptor = getPropertyDescriptor(bean, name);      if(descriptor == null)      {        // no - then put the value into the map        ((Map)bean).put(name, value);      }      else      {        // yes - use that instead        setSimpleProperty(bean, name, value);      }    }    else if(indexOfMAPPED_DELIM >= 0)    {      setMappedProperty(bean, name, value);    }    else if(indexOfINDEXED_DELIM >= 0)    {      setIndexedProperty(bean, name, value);    }    else    {      setSimpleProperty(bean, name, value);    }  }  /**   * Set the value of the specified property of the specified bean,   * no matter which property reference format is used, with no   * type conversions.   *   * @param bean  Bean whose property is to be modified   * @param name  Possibly indexed and/or nested name of the property   *              to be modified   * @param value Value to which this property is to be set   * @throws IllegalAccessException    if the caller does not have   *                                   access to the property accessor method   * @throws IllegalArgumentException  if <code>bean</code> or   *                                   <code>name</code> is null   * @throws InvocationTargetException if the property accessor method   *                                   throws an exception   * @throws NoSuchMethodException     if an accessor method for this   *                                   propety cannot be found   */  public void setProperty(Object bean, String name, Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException  {    setNestedProperty(bean, name, value);  }  /**   * Set the value of the specified simple property of the specified bean,   * with no type conversions.   *   * @param bean  Bean whose property is to be modified   * @param name  Name of the property to be modified   * @param value Value to which the property should be set   * @throws IllegalAccessException    if the caller does not have   *                                   access to the property accessor method   * @throws IllegalArgumentException  if <code>bean</code> or   *                                   <code>name</code> is null   * @throws IllegalArgumentException  if the property name is   *                                   nested or indexed   * @throws InvocationTargetException if the property accessor method   *                                   throws an exception   * @throws NoSuchMethodException     if an accessor method for this   *                                   propety cannot be found   */  public void setSimpleProperty(Object bean, String name, Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException  {    if(bean == null)    {      throw new IllegalArgumentException("No bean specified");    }    if(name == null)    {      throw new IllegalArgumentException("No name specified");    }    // Validate the syntax of the property name    if(name.indexOf(PropertyUtils.NESTED_DELIM) >= 0)    {      throw new IllegalArgumentException("Nested property names are not allowed");    }    else if(name.indexOf(PropertyUtils.INDEXED_DELIM) >= 0)    {      throw new IllegalArgumentException("Indexed property names are not allowed");    }    else if(name.indexOf(PropertyUtils.MAPPED_DELIM) >= 0)    {      throw new IllegalArgumentException("Mapped property names are not allowed");    }    // Retrieve the property setter method for the specified property    PropertyDescriptor descriptor = getPropertyDescriptor(bean, name);    if(descriptor == null)    {      throw new NoSuchMethodException("Unknown property '" + name + "'");    }    Method writeMethod = getWriteMethod(descriptor);    if(writeMethod == null)    {      throw new NoSuchMethodException("Property '" + name + "' has no setter method");    }    // Call the property setter method    Object values[] = new Object[1];    values[0] = value;    invokeMethod(writeMethod, bean, values);  }  /**   * This just catches and wraps IllegalArgumentException.   */  private Object invokeMethod(Method method, Object bean, Object[] values) throws IllegalAccessException, InvocationTargetException  {    try    {      return method.invoke(bean, values);    }    catch(IllegalArgumentException e)    {      throw new IllegalArgumentException("Cannot invoke " + method.getDeclaringClass().getName() + "." + method.getName() + " - " + e.getMessage());    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -