propertyutilsbean.java
来自「JAVA 文章管理系统源码」· Java 代码 · 共 1,626 行 · 第 1/5 页
JAVA
1,626 行
}
/**
* <p>Retrieve the property descriptors for the specified bean,
* introspecting and caching them the first time a particular bean class
* is encountered.</p>
*
* <p><strong>FIXME</strong> - Does not work with DynaBeans.</p>
*
* @param bean Bean for which property descriptors are requested
*
* @exception IllegalArgumentException if <code>bean</code> is null
*/
public PropertyDescriptor[] getPropertyDescriptors(Object bean) {
if (bean == null) {
throw new IllegalArgumentException("No bean specified");
}
return (getPropertyDescriptors(bean.getClass()));
}
/**
* <p>Return the Java Class repesenting the property editor class that has
* been registered for this property (if any). This method follows the
* same name resolution rules used by <code>getPropertyDescriptor()</code>,
* so if the last element of a name reference is indexed, the property
* editor for the underlying property's class is returned.</p>
*
* <p>Note that <code>null</code> will be returned if there is no property,
* or if there is no registered property editor class. Because this
* return value is ambiguous, you should determine the existence of the
* property itself by other means.</p>
*
* <p><strong>FIXME</strong> - Does not work with DynaBeans.</p>
*
* @param bean Bean for which a property descriptor is requested
* @param name Possibly indexed and/or nested name of the property for
* which a property descriptor is requested
*
* @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 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
* propety cannot be found
*/
public Class getPropertyEditorClass(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");
}
PropertyDescriptor descriptor =
getPropertyDescriptor(bean, name);
if (descriptor != null) {
return (descriptor.getPropertyEditorClass());
} else {
return (null);
}
}
/**
* Return the Java Class representing the property type of the specified
* property, or <code>null</code> if there is no such property for the
* specified bean. This method follows the same name resolution rules
* used by <code>getPropertyDescriptor()</code>, so if the last element
* of a name reference is indexed, the type of the property itself will
* be returned. If the last (or only) element has no property with the
* specified name, <code>null</code> is returned.
*
* @param bean Bean for which a property descriptor is requested
* @param name Possibly indexed and/or nested name of the property for
* which a property descriptor is requested
*
* @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 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
* propety cannot be found
*/
public Class getPropertyType(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");
}
// Special handling for DynaBeans
if (bean instanceof DynaBean) {
DynaProperty descriptor =
((DynaBean) bean).getDynaClass().getDynaProperty(name);
if (descriptor == null) {
return (null);
}
Class type = descriptor.getType();
if (type == null) {
return (null);
} else if (type.isArray()) {
return (type.getComponentType());
} else {
return (type);
}
}
PropertyDescriptor descriptor =
getPropertyDescriptor(bean, name);
if (descriptor == null) {
return (null);
} else if (descriptor instanceof IndexedPropertyDescriptor) {
return (((IndexedPropertyDescriptor) descriptor).
getIndexedPropertyType());
} else if (descriptor instanceof MappedPropertyDescriptor) {
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><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
*
* @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 IllegalArgumentException if the property name
* is nested or indexed
* @exception InvocationTargetException if the property accessor method
* throws an exception
* @exception 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");
}
// 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));
}
// 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><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 (may be a {@link DynaBean}
* @param name Property name to be evaluated
*
* @exception 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
if (bean instanceof DynaBean) {
// All DynaBean properties are readable
return (((DynaBean) bean).getDynaClass().getDynaProperty(name) != null);
} else {
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 (may be a {@link DynaBean}
* @param name Property name to be evaluated
*
* @exception IllegalPointerException 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
if (bean instanceof DynaBean) {
// All DynaBean properties are writeable
return (((DynaBean) bean).getDynaClass().getDynaProperty(name) != null);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?