View Javadoc
1   package org.apache.commons.beanutils2;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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       * Constructs a new instance of DefaultMappedPropertyGetterAccessor.
37       *
38       * @param bean
39       * @param propertyName
40       * @param mappedReadMethod
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       * {@inheritDoc}
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  }