下面列出了java.beans.IndexedPropertyDescriptor#getIndexedPropertyType ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Nullable
private PropertyDescriptor findExistingPropertyDescriptor(String propertyName, Class<?> propertyType) {
for (PropertyDescriptor pd : this.propertyDescriptors) {
final Class<?> candidateType;
final String candidateName = pd.getName();
if (pd instanceof IndexedPropertyDescriptor) {
IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
candidateType = ipd.getIndexedPropertyType();
if (candidateName.equals(propertyName) &&
(candidateType.equals(propertyType) || candidateType.equals(propertyType.getComponentType()))) {
return pd;
}
}
else {
candidateType = pd.getPropertyType();
if (candidateName.equals(propertyName) &&
(candidateType.equals(propertyType) || propertyType.equals(candidateType.getComponentType()))) {
return pd;
}
}
}
return null;
}
private PropertyDescriptor findExistingPropertyDescriptor(String propertyName, Class<?> propertyType) {
for (PropertyDescriptor pd : this.propertyDescriptors) {
final Class<?> candidateType;
final String candidateName = pd.getName();
if (pd instanceof IndexedPropertyDescriptor) {
IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
candidateType = ipd.getIndexedPropertyType();
if (candidateName.equals(propertyName) &&
(candidateType.equals(propertyType) || candidateType.equals(propertyType.getComponentType()))) {
return pd;
}
}
else {
candidateType = pd.getPropertyType();
if (candidateName.equals(propertyName) &&
(candidateType.equals(propertyType) || propertyType.equals(candidateType.getComponentType()))) {
return pd;
}
}
}
return null;
}
private PropertyDescriptor findExistingPropertyDescriptor(String propertyName, Class<?> propertyType) {
for (PropertyDescriptor pd : this.propertyDescriptors) {
final Class<?> candidateType;
final String candidateName = pd.getName();
if (pd instanceof IndexedPropertyDescriptor) {
IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
candidateType = ipd.getIndexedPropertyType();
if (candidateName.equals(propertyName) &&
(candidateType.equals(propertyType) || candidateType.equals(propertyType.getComponentType()))) {
return pd;
}
}
else {
candidateType = pd.getPropertyType();
if (candidateName.equals(propertyName) &&
(candidateType.equals(propertyType) || propertyType.equals(candidateType.getComponentType()))) {
return pd;
}
}
}
return null;
}
private static Class<?> getPropertyType(Class<?> type, boolean indexed) {
PropertyDescriptor pd = BeanUtils.findPropertyDescriptor(type, indexed ? "index" : "value");
if (pd instanceof IndexedPropertyDescriptor) {
IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
return ipd.getIndexedPropertyType();
}
return pd.getPropertyType();
}
public static boolean hasIPD(IndexedPropertyDescriptor ipd) {
if (null == ipd.getIndexedPropertyType()) {
return false;
}
return (null != ipd.getIndexedReadMethod())
|| (null != ipd.getIndexedWriteMethod());
}
private static Class<?> getPropertyType(Class<?> type, boolean indexed) {
PropertyDescriptor pd = BeanUtils.findPropertyDescriptor(type, indexed ? "index" : "value");
if (pd instanceof IndexedPropertyDescriptor) {
IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
return ipd.getIndexedPropertyType();
}
return pd.getPropertyType();
}
public static boolean hasIPD(IndexedPropertyDescriptor ipd) {
if (null == ipd.getIndexedPropertyType()) {
return false;
}
return (null != ipd.getIndexedReadMethod())
|| (null != ipd.getIndexedWriteMethod());
}
public static boolean hasIPD(IndexedPropertyDescriptor ipd) {
if (null == ipd.getIndexedPropertyType()) {
return false;
}
return (null != ipd.getIndexedReadMethod())
|| (null != ipd.getIndexedWriteMethod());
}
private static Class<?> getPropertyType(Class<?> type, boolean indexed) {
PropertyDescriptor pd = BeanUtils.findPropertyDescriptor(type, indexed ? "index" : "value");
if (pd instanceof IndexedPropertyDescriptor) {
IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
return ipd.getIndexedPropertyType();
}
return pd.getPropertyType();
}
public static Class getPropertyType( final PropertyDescriptor pd ) throws BeanException {
final Class typeFromDescriptor = pd.getPropertyType();
if ( typeFromDescriptor != null ) {
return typeFromDescriptor;
}
if ( pd instanceof IndexedPropertyDescriptor ) {
final IndexedPropertyDescriptor idx = (IndexedPropertyDescriptor) pd;
return idx.getIndexedPropertyType();
}
throw new BeanException( "Unable to determine the property type." );
}
private static Class<?> getPropertyType(Class<?> type, boolean indexed) {
PropertyDescriptor pd = BeanUtils.findPropertyDescriptor(type, indexed ? "index" : "value");
if (pd instanceof IndexedPropertyDescriptor) {
IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
return ipd.getIndexedPropertyType();
}
return pd.getPropertyType();
}
private static Class<?> getPropertyType(Class<?> type, boolean indexed) {
PropertyDescriptor pd = BeanUtils.findPropertyDescriptor(type, indexed ? "index" : "value");
if (pd instanceof IndexedPropertyDescriptor) {
IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
return ipd.getIndexedPropertyType();
}
return pd.getPropertyType();
}
public static boolean hasIPD(IndexedPropertyDescriptor ipd) {
if (null == ipd.getIndexedPropertyType()) {
return false;
}
return (null != ipd.getIndexedReadMethod())
|| (null != ipd.getIndexedWriteMethod());
}
public static boolean hasIPD(IndexedPropertyDescriptor ipd) {
if (null == ipd.getIndexedPropertyType()) {
return false;
}
return (null != ipd.getIndexedReadMethod())
|| (null != ipd.getIndexedWriteMethod());
}
public void setProperty(Object instance, String property, int index, Object value)
throws IllegalAccessException, InvocationTargetException {
IndexedPropertyDescriptor desc = (IndexedPropertyDescriptor) propertyMap.get(property);
if (desc == null) {
throw new IllegalArgumentException("Invalid property '" + property + "'");
}
Method meth = desc.getIndexedWriteMethod();
Class propertyType = desc.getIndexedPropertyType();
Object arg = getPropertyTypeValue(propertyType, value);
// System.out.println("Invoking: " + meth.getName() + " Index = " + index + " arg = " + arg);
meth.invoke(instance, new Object[]{new Integer(index), arg});
}
public static boolean hasIPD(IndexedPropertyDescriptor ipd) {
if (null == ipd.getIndexedPropertyType()) {
return false;
}
return (null != ipd.getIndexedReadMethod())
|| (null != ipd.getIndexedWriteMethod());
}
public static boolean hasIPD(IndexedPropertyDescriptor ipd) {
if (null == ipd.getIndexedPropertyType()) {
return false;
}
return (null != ipd.getIndexedReadMethod())
|| (null != ipd.getIndexedWriteMethod());
}
private static Class<?> getPropertyType(Class<?> type, boolean indexed) {
PropertyDescriptor pd = BeanUtils.findPropertyDescriptor(type, indexed ? "index" : "value");
if (pd instanceof IndexedPropertyDescriptor) {
IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
return ipd.getIndexedPropertyType();
}
return pd.getPropertyType();
}
/**
* Set a property of a bean to a given value.
*
* @param target the object whose prop is to be set
* @param propName name of the property to set
* @param index index to set (if property is indexed)
* @param value the property value
* @param valueType the type of the above (needed when its null)
* @param tcr type convertor registry to use to convert value type to
* property type if necessary
*
* @exception IntrospectionException if unable to introspect
* @exception IllegalArgumentException if problems with args: if the
* property is unknown, or if the property is given an index
* when its not, or if the property is not writeable, or if
* the given value cannot be assigned to the it (type mismatch).
* @exception IllegalAccessException if write method is not accessible
* @exception InvocationTargetException if write method excepts
*/
public static void setProperty (Object target, String propName,
Integer index, Object value,
Class valueType, TypeConvertorRegistry tcr)
throws IntrospectionException, IllegalArgumentException,
IllegalAccessException, InvocationTargetException {
// find the property descriptor
BeanInfo bi = Introspector.getBeanInfo (target.getClass ());
PropertyDescriptor pd = (PropertyDescriptor)
findFeatureByName ("property", propName, bi.getPropertyDescriptors ());
if (pd == null) {
throw new IllegalArgumentException ("property '" + propName + "' is " +
"unknown for '" + target + "'");
}
// get write method and type of property
Method wm;
Class propType;
if (index != null) {
// if index != null, then property is indexed - pd better be so too
if (!(pd instanceof IndexedPropertyDescriptor)) {
throw new IllegalArgumentException ("attempt to set non-indexed " +
"property '" + propName +
"' as being indexed");
}
IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
wm = ipd.getIndexedWriteMethod ();
propType = ipd.getIndexedPropertyType ();
} else {
wm = pd.getWriteMethod ();
propType = pd.getPropertyType ();
}
if (wm == null) {
throw new IllegalArgumentException ("property '" + propName +
"' is not writeable");
}
// type convert the value if necessary
Object propVal = null;
boolean okeydokey = true;
if (propType.isAssignableFrom (valueType)) {
propVal = value;
} else if (tcr != null) {
TypeConvertor cvtor = tcr.lookup (valueType, propType);
if (cvtor != null) {
propVal = cvtor.convert (valueType, propType, value);
} else {
okeydokey = false;
}
} else {
okeydokey = false;
}
if (!okeydokey) {
throw new IllegalArgumentException ("unable to assign '" + value +
"' to property '" + propName + "'");
}
// now set the value
if (index != null) {
wm.invoke (target, new Object[] {index, propVal});
} else {
wm.invoke (target, new Object[] {propVal});
}
}
/**
* Get a property of a bean.
*
* @param target the object whose prop is to be gotten
* @param propName name of the property to set
* @param index index to get (if property is indexed)
*
* @exception IntrospectionException if unable to introspect
* @exception IllegalArgumentException if problems with args: if the
* property is unknown, or if the property is given an index
* when its not, or if the property is not writeable, or if
* the given value cannot be assigned to the it (type mismatch).
* @exception IllegalAccessException if read method is not accessible
* @exception InvocationTargetException if read method excepts
*/
public static Bean getProperty (Object target, String propName,
Integer index)
throws IntrospectionException, IllegalArgumentException,
IllegalAccessException, InvocationTargetException {
// find the property descriptor
BeanInfo bi = Introspector.getBeanInfo (target.getClass ());
PropertyDescriptor pd = (PropertyDescriptor)
findFeatureByName ("property", propName, bi.getPropertyDescriptors ());
if (pd == null) {
throw new IllegalArgumentException ("property '" + propName + "' is " +
"unknown for '" + target + "'");
}
// get read method and type of property
Method rm;
Class propType;
if (index != null) {
// if index != null, then property is indexed - pd better be so too
if (!(pd instanceof IndexedPropertyDescriptor)) {
throw new IllegalArgumentException ("attempt to get non-indexed " +
"property '" + propName +
"' as being indexed");
}
IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
rm = ipd.getIndexedReadMethod ();
propType = ipd.getIndexedPropertyType ();
} else {
rm = pd.getReadMethod ();
propType = pd.getPropertyType ();
}
if (rm == null) {
throw new IllegalArgumentException ("property '" + propName +
"' is not readable");
}
// now get the value
Object propVal = null;
if (index != null) {
propVal = rm.invoke (target, new Object[] {index});
} else {
propVal = rm.invoke (target, null);
}
return new Bean (propType, propVal);
}