in src/main/java/org/apache/commons/beanutils2/PropertyUtilsBean.java [433:518]
public Object getIndexedProperty(final Object bean,
final String name, final int index)
throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException {
if (bean == null) {
throw new IllegalArgumentException("No bean specified");
}
if (name == null || name.isEmpty()) {
if (bean.getClass().isArray()) {
return Array.get(bean, index);
}
if (bean instanceof List) {
return ((List<?>)bean).get(index);
}
}
if (name == null) {
throw new IllegalArgumentException("No name specified for bean class '" +
bean.getClass() + "'");
}
// Handle DynaBean instances specially
if (bean instanceof DynaBean) {
final DynaProperty descriptor =
((DynaBean) bean).getDynaClass().getDynaProperty(name);
if (descriptor == null) {
throw new NoSuchMethodException("Unknown property '" +
name + "' on bean class '" + bean.getClass() + "'");
}
return ((DynaBean) bean).get(name, index);
}
// Retrieve the property descriptor for the specified property
final PropertyDescriptor descriptor =
getPropertyDescriptor(bean, name);
if (descriptor == null) {
throw new NoSuchMethodException("Unknown property '" +
name + "' on bean class '" + bean.getClass() + "'");
}
// Call the indexed getter method if there is one
if (descriptor instanceof IndexedPropertyDescriptor) {
Method readMethod = ((IndexedPropertyDescriptor) descriptor).
getIndexedReadMethod();
readMethod = MethodUtils.getAccessibleMethod(bean.getClass(), readMethod);
if (readMethod != null) {
final Object[] subscript = new Object[1];
subscript[0] = Integer.valueOf(index);
try {
return invokeMethod(readMethod, bean, subscript);
} catch (final InvocationTargetException e) {
if (e.getTargetException() instanceof
IndexOutOfBoundsException) {
throw (IndexOutOfBoundsException)
e.getTargetException();
}
throw e;
}
}
}
// Otherwise, the underlying property must be an array
final Method readMethod = getReadMethod(bean.getClass(), descriptor);
if (readMethod == null) {
throw new NoSuchMethodException("Property '" + name + "' has no " +
"getter method on bean class '" + bean.getClass() + "'");
}
// Call the property getter and return the value
final Object value = invokeMethod(readMethod, bean, BeanUtils.EMPTY_OBJECT_ARRAY);
if (!value.getClass().isArray()) {
if (!(value instanceof java.util.List)) {
throw new IllegalArgumentException("Property '" + name +
"' is not indexed on bean class '" + bean.getClass() + "'");
}
//get the List's value
return ((java.util.List<?>) value).get(index);
}
//get the array's value
try {
return Array.get(value, index);
} catch (final ArrayIndexOutOfBoundsException e) {
throw new ArrayIndexOutOfBoundsException("Index: " +
index + ", Size: " + Array.getLength(value) +
" for property '" + name + "'");
}
}