📄 beanutilsbean.java
字号:
* required to conform to the type of the destination property.</p>
*
* <p>If the property is read only then the method returns
* without throwing an exception.</p>
*
* <p>If <code>null</code> is passed into a property expecting a primitive value,
* then this will be converted as if it were a <code>null</code> string.</p>
*
* <p><strong>WARNING</strong> - The logic of this method is customized
* to meet the needs of <code>populate()</code>, and is probably not what
* you want for general property copying with type conversion. For that
* purpose, check out the <code>copyProperty()</code> method instead.</p>
*
* <p><strong>WARNING</strong> - PLEASE do not modify the behavior of this
* method without consulting with the Struts developer community. There
* are some subtleties to its functionality that are not documented in the
* Javadoc description above, yet are vital to the way that Struts utilizes
* this method.</p>
*
* @param bean Bean on which setting is to be performed
* @param name Property name (can be nested/indexed/mapped/combo)
* @param value 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
*/
public void setProperty(Object bean, String name, Object value)
throws IllegalAccessException, InvocationTargetException {
// Trace logging (if enabled)
if (log.isTraceEnabled()) {
StringBuffer sb = new StringBuffer(" setProperty(");
sb.append(bean);
sb.append(", ");
sb.append(name);
sb.append(", ");
if (value == null) {
sb.append("<NULL>");
} else if (value instanceof String) {
sb.append((String) value);
} else if (value instanceof String[]) {
String[] values = (String[]) value;
sb.append('[');
for (int i = 0; i < values.length; i++) {
if (i > 0) {
sb.append(',');
}
sb.append(values[i]);
}
sb.append(']');
} else {
sb.append(value.toString());
}
sb.append(')');
log.trace(sb.toString());
}
// Resolve any nested expression to get the actual target bean
Object target = bean;
Resolver resolver = getPropertyUtils().getResolver();
while (resolver.hasNested(name)) {
try {
target = getPropertyUtils().getProperty(target, resolver.next(name));
name = resolver.remove(name);
} catch (NoSuchMethodException e) {
return; // Skip this property setter
}
}
if (log.isTraceEnabled()) {
log.trace(" Target bean = " + target);
log.trace(" Target name = " + name);
}
// Declare local variables we will require
String propName = resolver.getProperty(name); // Simple name of target property
Class type = null; // Java type of target property
int index = resolver.getIndex(name); // Indexed subscript value (if any)
String key = resolver.getKey(name); // Mapped key value (if any)
// Calculate the property type
if (target instanceof DynaBean) {
DynaClass dynaClass = ((DynaBean) target).getDynaClass();
DynaProperty dynaProperty = dynaClass.getDynaProperty(propName);
if (dynaProperty == null) {
return; // Skip this property setter
}
type = dynaProperty.getType();
} else {
PropertyDescriptor descriptor = null;
try {
descriptor =
getPropertyUtils().getPropertyDescriptor(target, name);
if (descriptor == null) {
return; // Skip this property setter
}
} catch (NoSuchMethodException e) {
return; // Skip this property setter
}
if (descriptor instanceof MappedPropertyDescriptor) {
if (((MappedPropertyDescriptor) descriptor).getMappedWriteMethod() == null) {
if (log.isDebugEnabled()) {
log.debug("Skipping read-only property");
}
return; // Read-only, skip this property setter
}
type = ((MappedPropertyDescriptor) descriptor).
getMappedPropertyType();
} else if (index >= 0 && descriptor instanceof IndexedPropertyDescriptor) {
if (((IndexedPropertyDescriptor) descriptor).getIndexedWriteMethod() == null) {
if (log.isDebugEnabled()) {
log.debug("Skipping read-only property");
}
return; // Read-only, skip this property setter
}
type = ((IndexedPropertyDescriptor) descriptor).
getIndexedPropertyType();
} else if (key != null) {
if (descriptor.getReadMethod() == null) {
if (log.isDebugEnabled()) {
log.debug("Skipping read-only property");
}
return; // Read-only, skip this property setter
}
type = (value == null) ? Object.class : value.getClass();
} else {
if (descriptor.getWriteMethod() == null) {
if (log.isDebugEnabled()) {
log.debug("Skipping read-only property");
}
return; // Read-only, skip this property setter
}
type = descriptor.getPropertyType();
}
}
// Convert the specified value to the required type
Object newValue = null;
if (type.isArray() && (index < 0)) { // Scalar value into array
if (value == null) {
String[] values = new String[1];
values[0] = (String) value;
newValue = getConvertUtils().convert((String[]) values, type);
} else if (value instanceof String) {
newValue = getConvertUtils().convert(value, type);
} else if (value instanceof String[]) {
newValue = getConvertUtils().convert((String[]) value, type);
} else {
newValue = convert(value, type);
}
} else if (type.isArray()) { // Indexed value into array
if (value instanceof String || value == null) {
newValue = getConvertUtils().convert((String) value,
type.getComponentType());
} else if (value instanceof String[]) {
newValue = getConvertUtils().convert(((String[]) value)[0],
type.getComponentType());
} else {
newValue = convert(value, type.getComponentType());
}
} else { // Value into scalar
if ((value instanceof String) || (value == null)) {
newValue = getConvertUtils().convert((String) value, type);
} else if (value instanceof String[]) {
newValue = getConvertUtils().convert(((String[]) value)[0],
type);
} else {
newValue = convert(value, type);
}
}
// Invoke the setter method
try {
if (index >= 0) {
getPropertyUtils().setIndexedProperty(target, propName,
index, newValue);
} else if (key != null) {
getPropertyUtils().setMappedProperty(target, propName,
key, newValue);
} else {
getPropertyUtils().setProperty(target, propName, newValue);
}
} catch (NoSuchMethodException e) {
throw new InvocationTargetException
(e, "Cannot set " + propName);
}
}
/**
* Gets the <code>ConvertUtilsBean</code> instance used to perform the conversions.
*
* @return The ConvertUtils bean instance
*/
public ConvertUtilsBean getConvertUtils() {
return convertUtilsBean;
}
/**
* Gets the <code>PropertyUtilsBean</code> instance used to access properties.
*
* @return The ConvertUtils bean instance
*/
public PropertyUtilsBean getPropertyUtils() {
return propertyUtilsBean;
}
/**
* If we're running on JDK 1.4 or later, initialize the cause for the given throwable.
*
* @param throwable The throwable.
* @param cause The cause of the throwable.
* @return true if the cause was initialized, otherwise false.
*/
public boolean initCause(Throwable throwable, Throwable cause) {
if (INIT_CAUSE_METHOD != null && cause != null) {
try {
INIT_CAUSE_METHOD.invoke(throwable, new Object[] { cause });
return true;
} catch (Throwable e) {
return false; // can't initialize cause
}
}
return false;
}
/**
* <p>Convert the value to an object of the specified class (if
* possible).</p>
*
* @param value Value to be converted (may be null)
* @param type Class of the value to be converted to
* @return The converted value
*
* @exception ConversionException if thrown by an underlying Converter
*/
protected Object convert(Object value, Class type) {
Converter converter = getConvertUtils().lookup(type);
if (converter != null) {
log.trace(" USING CONVERTER " + converter);
return converter.convert(type, value);
} else {
return value;
}
}
/**
* Returns a <code>Method<code> allowing access to
* {@link Throwable#initCause(Throwable)} method of {@link Throwable},
* or <code>null</code> if the method
* does not exist.
*
* @return A <code>Method<code> for <code>Throwable.initCause</code>, or
* <code>null</code> if unavailable.
*/
private static Method getInitCauseMethod() {
try {
Class[] paramsClasses = new Class[] { Throwable.class };
return Throwable.class.getMethod("initCause", paramsClasses);
} catch (NoSuchMethodException e) {
Log log = LogFactory.getLog(BeanUtils.class);
if (log.isWarnEnabled()) {
log.warn("Throwable does not have initCause() method in JDK 1.3");
}
return null;
} catch (Throwable e) {
Log log = LogFactory.getLog(BeanUtils.class);
if (log.isWarnEnabled()) {
log.warn("Error getting the Throwable initCause() method", e);
}
return null;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -