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.checkArgument;
23  import static org.apache.commons.beanutils2.TypeUtils.isAssignmentCompatible;
24  
25  import java.lang.reflect.InvocationTargetException;
26  import java.lang.reflect.Method;
27  
28  final class DefaultMappedPropertySetter<B>
29      implements BeanPropertySetter<B>
30  {
31      // FIXME Code in this class is almost identical with DefaultIndexedPropertySetter
32      private final B bean;
33  
34      private final String propertyName;
35  
36      private final Method mappedSetterMethod;
37  
38      private final String key;
39  
40      public DefaultMappedPropertySetter( B bean, String propertyName, Method mappedSetterMethod, String key )
41      {
42          this.bean = bean;
43          this.propertyName = propertyName;
44          this.mappedSetterMethod = mappedSetterMethod;
45          this.key = key;
46      }
47  
48      public <V> BeanAccessor<B> with( V value )
49      {
50          Class<?> paramType = mappedSetterMethod.getParameterTypes()[1];
51  
52          if ( value == null )
53          {
54              checkArgument( !paramType.isPrimitive(), "Null can not be assigned to a primitive property!" );
55          }
56          else
57          {
58              checkArgument( isAssignmentCompatible( paramType, value.getClass() ),
59                             "Value of type %s is not compatible to parameter type %s!", value.getClass().getName(),
60                             paramType.getName() );
61          }
62  
63          invokeSetter( value );
64  
65          return new DefaultBeanAccessor<B>( bean );
66      }
67  
68      private <V> void invokeSetter( V value )
69      {
70          try
71          {
72              mappedSetterMethod.invoke( bean, key, value );
73          }
74          catch ( IllegalAccessException e )
75          {
76              throw new PropertySetterNotAccessibleException( propertyName, mappedSetterMethod.getName(),
77                                                              bean.getClass(), e );
78          }
79          catch ( InvocationTargetException e )
80          {
81              throw new PropertySetterInvocationException( propertyName, mappedSetterMethod.getName(), bean.getClass(), e );
82          }
83      }
84  
85  }