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 DefaultBeanPropertySetter<B>
29      implements BeanPropertySetter<B>
30  {
31  
32      private final B bean;
33  
34      private final Method setterMethod;
35  
36      private final String propertyName;
37  
38      public DefaultBeanPropertySetter( B bean, Method setterMethod, String propertyName )
39      {
40          this.bean = bean;
41          this.setterMethod = setterMethod;
42          this.propertyName = propertyName;
43      }
44  
45      /**
46       * {@inheritDoc}
47       */
48      public <V> BeanAccessor<B> with( V value )
49      {
50          Class<?> paramType = setterMethod.getParameterTypes()[0];
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!",
60                             value.getClass().getName(), 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              setterMethod.invoke( bean, value );
73          }
74          catch ( IllegalAccessException e )
75          {
76              throw new PropertySetterNotAccessibleException( propertyName, setterMethod.getName(), bean.getClass(), e );
77          }
78          catch ( InvocationTargetException e )
79          {
80              throw new PropertySetterInvocationException( propertyName, setterMethod.getName(), bean.getClass(), e );
81          }
82      }
83  
84  }