📄 propertyutilsbean.java
字号:
} catch (NumberFormatException e) { throw new IllegalArgumentException("Invalid indexed property '" + name + "'"); } name = name.substring(0, delim); // Request the specified indexed property value return (getIndexedProperty(bean, name, index)); } /** * Return 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 extracted * @param name Simple property name of the property value to be extracted * @param index Index of the property value to be extracted * * @exception ArrayIndexOutOfBoundsException if the specified index * is outside the valid range for the underlying array * @exception IllegalAccessException if the caller does not have * access to the property accessor method * @exception IllegalArgumentException if <code>bean</code> or * <code>name</code> is null * @exception InvocationTargetException if the property accessor method * throws an exception * @exception NoSuchMethodException if an accessor method for this * propety cannot be found */ public Object getIndexedProperty(Object bean, String name, int index) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { if (bean == null) { throw new IllegalArgumentException("No bean specified"); } if (name == null) { throw new IllegalArgumentException("No name specified"); } // Handle DynaBean instances specially if (bean instanceof DynaBean) { DynaProperty descriptor = ((DynaBean) bean).getDynaClass().getDynaProperty(name); if (descriptor == null) { throw new NoSuchMethodException("Unknown property '" + name + "'"); } return (((DynaBean) bean).get(name, index)); } // 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 getter method if there is one if (descriptor instanceof IndexedPropertyDescriptor) { Method readMethod = ((IndexedPropertyDescriptor) descriptor). getIndexedReadMethod(); if (readMethod != null) { Object subscript[] = new Object[1]; subscript[0] = new Integer(index); try { return (invokeMethod(readMethod,bean, subscript)); } catch (InvocationTargetException e) { if (e.getTargetException() instanceof ArrayIndexOutOfBoundsException) { throw (ArrayIndexOutOfBoundsException) e.getTargetException(); } else { throw e; } } } } // Otherwise, the underlying property must be an array 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]); if (!value.getClass().isArray()) { if (!(value instanceof java.util.List)) { throw new IllegalArgumentException("Property '" + name + "' is not indexed"); } else { //get the List's value return ((java.util.List) value).get(index); } } else { //get the array's value return (Array.get(value, index)); } } /** * Return the value of the specified mapped property of the * specified bean, with no type conversions. The key of the * required value 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 extracted * @param name <code>propertyname(key)</code> of the property value * to be extracted * * @exception IllegalAccessException if the caller does not have * access to the property accessor method * @exception InvocationTargetException if the property accessor method * throws an exception * @exception NoSuchMethodException if an accessor method for this * propety cannot be found */ public Object getMappedProperty(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"); } // 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 return (getMappedProperty(bean, name, key)); } /** * Return the value of the specified mapped property of the specified * bean, with no type conversions. * * @param bean Bean whose property is to be extracted * @param name Mapped property name of the property value to be extracted * @param key Key of the property value to be extracted * * @exception IllegalAccessException if the caller does not have * access to the property accessor method * @exception InvocationTargetException if the property accessor method * throws an exception * @exception NoSuchMethodException if an accessor method for this * propety cannot be found */ public Object getMappedProperty(Object bean, String name, String key) 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"); } // Handle DynaBean instances specially if (bean instanceof DynaBean) { DynaProperty descriptor = ((DynaBean) bean).getDynaClass().getDynaProperty(name); if (descriptor == null) { throw new NoSuchMethodException("Unknown property '" + name + "'"); } return (((DynaBean) bean).get(name, key)); } Object result = null; // 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 getter method if there is one Method readMethod = ((MappedPropertyDescriptor) descriptor). getMappedReadMethod(); if (readMethod != null) { Object keyArray[] = new Object[1]; keyArray[0] = key; result = invokeMethod(readMethod, bean, keyArray); } else { throw new NoSuchMethodException("Property '" + name + "' has no mapped getter 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) { result = ((java.util.Map)invokeResult).get(key); } } else { throw new NoSuchMethodException("Property '" + name + "' has no mapped getter method"); } } return result; } /** * <p>Return the mapped property descriptors for this bean class.</p> * * <p><strong>FIXME</strong> - Does not work with DynaBeans.</p> * * @param beanClass Bean class to be introspected * @deprecated This method should not be exposed */ public FastHashMap getMappedPropertyDescriptors(Class beanClass) { if (beanClass == null) { return null; } // Look up any cached descriptors for this bean class return (FastHashMap) mappedDescriptorsCache.get(beanClass); } /** * <p>Return the mapped property descriptors for this bean.</p> * * <p><strong>FIXME</strong> - Does not work with DynaBeans.</p> * * @param bean Bean to be introspected * @deprecated This method should not be exposed */ public FastHashMap getMappedPropertyDescriptors(Object bean) { if (bean == null) { return null; } return (getMappedPropertyDescriptors(bean.getClass())); } /** * Return 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 extracted * @param name Possibly nested name of the property to be extracted * * @exception IllegalAccessException if the caller does not have * access to the property accessor method * @exception IllegalArgumentException if <code>bean</code> or * <code>name</code> is null * @exception NestedNullException if a nested reference to a * property returns null * @exception InvocationTargetException * if the property accessor method throws an exception * @exception NoSuchMethodException if an accessor method for this * propety cannot be found */ public Object getNestedProperty(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"); } int indexOfINDEXED_DELIM = -1; int indexOfMAPPED_DELIM = -1; int indexOfMAPPED_DELIM2 = -1; int indexOfNESTED_DELIM = -1; while (true) { indexOfNESTED_DELIM = name.indexOf(PropertyUtils.NESTED_DELIM); indexOfMAPPED_DELIM = name.indexOf(PropertyUtils.MAPPED_DELIM); indexOfMAPPED_DELIM2 = name.indexOf(PropertyUtils.MAPPED_DELIM2); if (indexOfMAPPED_DELIM2 >= 0 && indexOfMAPPED_DELIM >=0 && (indexOfNESTED_DELIM < 0 || indexOfNESTED_DELIM > indexOfMAPPED_DELIM)) { indexOfNESTED_DELIM = name.indexOf(PropertyUtils.NESTED_DELIM, indexOfMAPPED_DELIM2); } else { indexOfNESTED_DELIM = name.indexOf(PropertyUtils.NESTED_DELIM); } if (indexOfNESTED_DELIM < 0) { break; } String next = name.substring(0, indexOfNESTED_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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -