1 package org.apache.commons.beanutils2;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import static org.apache.commons.beanutils2.Assertions.checkArgument;
23
24 import java.lang.reflect.InvocationTargetException;
25 import java.lang.reflect.Method;
26
27 final class DefaultIndexedPropertyGetterAccessor<B>
28 implements IndexedPropertyGetterAccessor<B>
29 {
30
31 private final B bean;
32
33 private final String propertyName;
34
35 private final Method indexedReadMethod;
36
37 public DefaultIndexedPropertyGetterAccessor( B bean, String propertyName, Method indexedReadMethod )
38 {
39 this.bean = bean;
40 this.propertyName = propertyName;
41 this.indexedReadMethod = indexedReadMethod;
42 }
43
44
45
46
47 public BeanAccessor<?> at( int index )
48 {
49 checkArgument( index >= 0, "Indexed property '%s' in bean of type %s cannot be get from a negative index %s",
50 propertyName, bean.getClass().getName(), index );
51 Object indexedValue = invokeGetter( index );
52 return new DefaultBeanAccessor<Object>( indexedValue );
53 }
54
55 private Object invokeGetter( int index )
56 {
57 try
58 {
59 return indexedReadMethod.invoke( bean, index );
60 }
61 catch ( IllegalAccessException e )
62 {
63 throw new PropertyGetterNotAccessibleException( propertyName, indexedReadMethod.getName(), bean.getClass(), e );
64 }
65 catch ( InvocationTargetException e )
66 {
67 throw new PropertyGetterInvocationException( propertyName, indexedReadMethod.getName(), bean.getClass(), e );
68 }
69 }
70
71 }