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.checkNotNull;
23
24 import java.lang.reflect.InvocationTargetException;
25 import java.lang.reflect.Method;
26
27 class DefaultMappedPropertyGetterAccessor<B>
28 implements MappedPropertyGetterAccessor
29 {
30
31 private final B bean;
32 private final String propertyName;
33 private final Method mappedReadMethod;
34
35
36
37
38
39
40
41
42 public DefaultMappedPropertyGetterAccessor( B bean, String propertyName, Method mappedReadMethod )
43 {
44 this.bean = bean;
45 this.propertyName = propertyName;
46 this.mappedReadMethod = mappedReadMethod;
47 }
48
49
50
51
52 public BeanAccessor<?> of( String key )
53 {
54 checkNotNull( key, "Can not get mapped property '%s' in bean of type '%s' for null!", propertyName,
55 bean.getClass().getName() );
56 Object mappedValue = invokeGetter( key );
57 return wrapInAccessor( mappedValue );
58 }
59
60 private Object invokeGetter( String key )
61 {
62 try
63 {
64 return mappedReadMethod.invoke( bean, key );
65 }
66 catch ( IllegalAccessException e )
67 {
68 throw new PropertyGetterNotAccessibleException( propertyName, mappedReadMethod.getName(), bean.getClass(), e );
69 }
70 catch ( InvocationTargetException e )
71 {
72 throw new PropertyGetterInvocationException( propertyName, mappedReadMethod.getName(), bean.getClass(), e );
73 }
74 }
75
76 private BeanAccessor<?> wrapInAccessor( Object mappedValue )
77 {
78 if ( mappedValue != null )
79 {
80 return new DefaultBeanAccessor<Object>( mappedValue );
81 }
82 else
83 {
84 return new NullBeanAccessor<Object>( bean.getClass().getName(), mappedReadMethod.getName() );
85 }
86 }
87
88 }