propertyutilsbean.java
来自「JAVA 文章管理系统源码」· Java 代码 · 共 1,626 行 · 第 1/5 页
JAVA
1,626 行
} else {
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
*
* @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 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
*
* @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 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");
}
// Handle DynaBean instances specially
if (bean instanceof DynaBean) {
DynaProperty descriptor =
((DynaBean) bean).getDynaClass().getDynaProperty(name);
if (descriptor == null) {
throw new NoSuchMethodException("Unknown property '" +
name + "'");
}
((DynaBean) bean).set(name, index, value);
return;
}
// 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 {
if (log.isTraceEnabled()) {
String valueClassName =
value == null ? "<null>"
: value.getClass().getName();
log.trace("setSimpleProperty: Invoking method "
+ writeMethod +" with index=" + index
+ ", value=" + value
+ " (class " + valueClassName+ ")");
}
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");
}
// 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
*
* @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 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
*
* @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 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");
}
// Handle DynaBean instances specially
if (bean instanceof DynaBean) {
DynaProperty descriptor =
((DynaBean) bean).getDynaClass().getDynaProperty(name);
if (descriptor == null) {
throw new NoSuchMethodException("Unknown property '" +
name + "'");
}
((DynaBean) bean).set(name, key, value);
return;
}
// 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;
if (log.isTraceEnabled()) {
String valueClassName =
value == null ? "<null>" : value.getClass().getName();
log.trace("setSimpleProperty: Invoking method "
+ mappedWriteMethod + " with key=" + key
+ ", value=" + value
+ " (class " + valueClassName +")");
}
invokeMethod(mappedWriteMethod, bean, params);
} else {
throw new NoSuchMethodException
("Property '" + name +
"' has no mapped setter method");
}
} else {
/* means that the result has to be retrieved
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?