📄 propertyutilsbean.java
字号:
return (((MappedPropertyDescriptor)descriptor).getMappedPropertyType()); } else { return (descriptor.getPropertyType()); } } /** * <p>Return an accessible property getter method for this property, * if there is one; otherwise return <code>null</code>.</p> * <p/> * <p><strong>FIXME</strong> - Does not work with DynaBeans.</p> * * @param descriptor Property descriptor to return a getter for */ public Method getReadMethod(PropertyDescriptor descriptor) { return (MethodUtils.getAccessibleMethod(descriptor.getReadMethod())); } /** * Return the value of the specified simple property of the specified * bean, with no type conversions. * * @param bean Bean whose property is to be extracted * @param name Name of the property to be extracted * @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 Object getSimpleProperty(Object bean, String name) 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 getter method for the specified property PropertyDescriptor descriptor = getPropertyDescriptor(bean, name); if(descriptor == null) { throw new NoSuchMethodException("Unknown property '" + name + "'"); } Method readMethod = getReadMethod(descriptor); if(readMethod == null) { throw new NoSuchMethodException("Property '" + name + "' has no getter method"); } // Call the property getter and return the value Object value = invokeMethod(readMethod, bean, new Object[0]); return (value); } /** * <p>Return an accessible property setter method for this property, * if there is one; otherwise return <code>null</code>.</p> * <p/> * <p><strong>FIXME</strong> - Does not work with DynaBeans.</p> * * @param descriptor Property descriptor to return a setter for */ public Method getWriteMethod(PropertyDescriptor descriptor) { return (MethodUtils.getAccessibleMethod(descriptor.getWriteMethod())); } /** * <p>Return <code>true</code> if the specified property name identifies * a readable property on the specified bean; otherwise, return * <code>false</code>. * * @param bean Bean to be examined * @param name Property name to be evaluated * @throws IllegalArgumentException if <code>bean</code> * or <code>name</code> is <code>null</code> * @since BeanUtils 1.6 */ public boolean isReadable(Object bean, String name) { // Validate method parameters if(bean == null) { throw new IllegalArgumentException("No bean specified"); } if(name == null) { throw new IllegalArgumentException("No name specified"); } // Return the requested result try { PropertyDescriptor desc = getPropertyDescriptor(bean, name); if(desc != null) { Method readMethod = desc.getReadMethod(); if((readMethod == null) && (desc instanceof IndexedPropertyDescriptor)) { readMethod = ((IndexedPropertyDescriptor)desc).getIndexedReadMethod(); } return (readMethod != null); } else { return (false); } } catch(IllegalAccessException e) { return (false); } catch(InvocationTargetException e) { return (false); } catch(NoSuchMethodException e) { return (false); } } /** * <p>Return <code>true</code> if the specified property name identifies * a writeable property on the specified bean; otherwise, return * <code>false</code>. * * @param bean Bean to be examined * @param name Property name to be evaluated * @throws IllegalArgumentException if <code>bean</code> * or <code>name</code> is <code>null</code> * @since BeanUtils 1.6 */ public boolean isWriteable(Object bean, String name) { // Validate method parameters if(bean == null) { throw new IllegalArgumentException("No bean specified"); } if(name == null) { throw new IllegalArgumentException("No name specified"); } // Return the requested result try { PropertyDescriptor desc = getPropertyDescriptor(bean, name); if(desc != null) { Method writeMethod = desc.getWriteMethod(); if((writeMethod == null) && (desc instanceof IndexedPropertyDescriptor)) { writeMethod = ((IndexedPropertyDescriptor)desc).getIndexedWriteMethod(); } return (writeMethod != null); } else { return (false); } } catch(IllegalAccessException e) { return (false); } catch(InvocationTargetException e) { return (false); } catch(NoSuchMethodException e) { return (false); } } /** * Set the value of the specified indexed property of the specified * bean, with no type conversions. The zero-relative index of the * required value must be included (in square brackets) as a suffix to * the property name, or <code>IllegalArgumentException</code> will be * thrown. In addition to supporting the JavaBeans specification, this * method has been extended to support <code>List</code> objects as well. * * @param bean Bean whose property is to be modified * @param name <code>propertyname[index]</code> of the property value * to be modified * @param value Value to which the specified property element * should be set * @throws ArrayIndexOutOfBoundsException if the specified index * is outside the valid range for the underlying array * @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 setIndexedProperty(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.INDEXED_DELIM); int delim2 = name.indexOf(PropertyUtils.INDEXED_DELIM2); if((delim < 0) || (delim2 <= delim)) { throw new IllegalArgumentException("Invalid indexed property '" + name + "'"); } int index = -1; try { String subscript = name.substring(delim + 1, delim2); index = Integer.parseInt(subscript); } catch(NumberFormatException e) { throw new IllegalArgumentException("Invalid indexed property '" + name + "'"); } name = name.substring(0, delim); // Set the specified indexed property value setIndexedProperty(bean, name, index, value); } /** * Set the value of the specified indexed property of the specified * bean, with no type conversions. In addition to supporting the JavaBeans * specification, this method has been extended to support * <code>List</code> objects as well. * * @param bean Bean whose property is to be set * @param name Simple property name of the property value to be set * @param index Index of the property value to be set * @param value Value to which the indexed property element is to be set * @throws ArrayIndexOutOfBoundsException if the specified index * is outside the valid range for the underlying array * @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 setIndexedProperty(Object bean, String name, int index, Object value) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { if(bean == null) { throw new IllegalArgumentException("No bean specified"); } if(name == null) { throw new IllegalArgumentException("No name specified"); } // Retrieve the property descriptor for the specified property PropertyDescriptor descriptor = getPropertyDescriptor(bean, name); if(descriptor == null) { throw new NoSuchMethodException("Unknown property '" + name + "'"); } // Call the indexed setter method if there is one if(descriptor instanceof IndexedPropertyDescriptor) { Method writeMethod = ((IndexedPropertyDescriptor)descriptor).getIndexedWriteMethod(); if(writeMethod != null) { Object subscript[] = new Object[2]; subscript[0] = new Integer(index); subscript[1] = value; try { invokeMethod(writeMethod, bean, subscript); } catch(InvocationTargetException e) { if(e.getTargetException() instanceof ArrayIndexOutOfBoundsException) { throw (ArrayIndexOutOfBoundsException)e.getTargetException(); } else { throw e; } } return; } } // Otherwise, the underlying property must be an array or a list Method readMethod = descriptor.getReadMethod(); if(readMethod == null) { throw new NoSuchMethodException("Property '" + name + "' has no getter method"); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -