?? beanutilsbean.java
字號:
}
}
return ((String[]) values.toArray(new String[values.size()]));
} else if (value.getClass().isArray()) {
int n = Array.getLength(value);
String[] results = new String[n];
for (int i = 0; i < n; i++) {
Object item = Array.get(value, i);
if (item == null) {
results[i] = null;
} else {
// convert to string using convert utils
results[i] = getConvertUtils().convert(item);
}
}
return (results);
} else {
String[] results = new String[1];
results[0] = getConvertUtils().convert(value);
return (results);
}
}
/**
* Return the value of the specified indexed property of the specified
* bean, as a String. 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.
*
* @param bean Bean whose property is to be extracted
* @param name <code>propertyname[index]</code> of the property value
* to be extracted
* @return The indexed property's value, converted to a String
*
* @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
* property cannot be found
*/
public String getIndexedProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
Object value = getPropertyUtils().getIndexedProperty(bean, name);
return (getConvertUtils().convert(value));
}
/**
* Return the value of the specified indexed property of the specified
* bean, as a String. The index is specified as a method parameter and
* must *not* be included in the property name expression
*
* @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
* @return The indexed property's value, converted to a String
*
* @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
* property cannot be found
*/
public String getIndexedProperty(Object bean,
String name, int index)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
Object value = getPropertyUtils().getIndexedProperty(bean, name, index);
return (getConvertUtils().convert(value));
}
/**
* Return the value of the specified indexed property of the specified
* bean, as a String. The String-valued key of the required value
* must be included (in parentheses) 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(index)</code> of the property value
* to be extracted
* @return The mapped property's value, converted to a String
*
* @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
* property cannot be found
*/
public String getMappedProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
Object value = getPropertyUtils().getMappedProperty(bean, name);
return (getConvertUtils().convert(value));
}
/**
* Return the value of the specified mapped property of the specified
* bean, as a String. The key is specified as a method parameter and
* must *not* be included in the property name expression
*
* @param bean Bean whose property is to be extracted
* @param name Simple property name of the property value to be extracted
* @param key Lookup key of the property value to be extracted
* @return The mapped property's value, converted to a String
*
* @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
* property cannot be found
*/
public String getMappedProperty(Object bean,
String name, String key)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
Object value = getPropertyUtils().getMappedProperty(bean, name, key);
return (getConvertUtils().convert(value));
}
/**
* Return the value of the (possibly nested) property of the specified
* name, for the specified bean, as a String.
*
* @param bean Bean whose property is to be extracted
* @param name Possibly nested name of the property to be extracted
* @return The nested property's value, converted to a String
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception IllegalArgumentException 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
* property cannot be found
*/
public String getNestedProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
Object value = getPropertyUtils().getNestedProperty(bean, name);
return (getConvertUtils().convert(value));
}
/**
* Return the value of the specified property of the specified bean,
* no matter which property reference format is used, as a String.
*
* @param bean Bean whose property is to be extracted
* @param name Possibly indexed and/or nested name of the property
* to be extracted
* @return The property's value, converted to a String
*
* @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
* property cannot be found
*/
public String getProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
return (getNestedProperty(bean, name));
}
/**
* Return the value of the specified simple property of the specified
* bean, converted to a String.
*
* @param bean Bean whose property is to be extracted
* @param name Name of the property to be extracted
* @return The property's value, converted to a String
*
* @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
* property cannot be found
*/
public String getSimpleProperty(Object bean, String name)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
Object value = getPropertyUtils().getSimpleProperty(bean, name);
return (getConvertUtils().convert(value));
}
/**
* <p>Populate the JavaBeans properties of the specified bean, based on
* the specified name/value pairs. This method uses Java reflection APIs
* to identify corresponding "property setter" method names, and deals
* with setter arguments of type <code>String</code>, <code>boolean</code>,
* <code>int</code>, <code>long</code>, <code>float</code>, and
* <code>double</code>. In addition, array setters for these types (or the
* corresponding primitive types) can also be identified.</p>
*
* <p>The particular setter method to be called for each property is
* determined using the usual JavaBeans introspection mechanisms. Thus,
* you may identify custom setter methods using a BeanInfo class that is
* associated with the class of the bean itself. If no such BeanInfo
* class is available, the standard method name conversion ("set" plus
* the capitalized name of the property in question) is used.</p>
*
* <p><strong>NOTE</strong>: It is contrary to the JavaBeans Specification
* to have more than one setter method (with different argument
* signatures) for the same property.</p>
*
* <p><strong>WARNING</strong> - The logic of this method is customized
* for extracting String-based request parameters from an HTTP request.
* It is probably not what you want for general property copying with
* type conversion. For that purpose, check out the
* <code>copyProperties()</code> method instead.</p>
*
* @param bean JavaBean whose properties are being populated
* @param properties Map keyed by property name, with the
* corresponding (String or String[]) value(s) to be set
*
* @exception IllegalAccessException if the caller does not have
* access to the property accessor method
* @exception InvocationTargetException if the property accessor method
* throws an exception
*/
public void populate(Object bean, Map properties)
throws IllegalAccessException, InvocationTargetException {
// Do nothing unless both arguments have been specified
if ((bean == null) || (properties == null)) {
return;
}
if (log.isDebugEnabled()) {
log.debug("BeanUtils.populate(" + bean + ", " +
properties + ")");
}
// Loop through the property name/value pairs to be set
Iterator entries = properties.entrySet().iterator();
while (entries.hasNext()) {
// Identify the property name and value(s) to be assigned
Map.Entry entry = (Map.Entry)entries.next();
String name = (String) entry.getKey();
if (name == null) {
continue;
}
// Perform the assignment for this property
setProperty(bean, name, entry.getValue());
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -