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.Argument.getParameterTypes;
23  import static org.apache.commons.beanutils2.Argument.getParameters;
24  import static org.apache.commons.beanutils2.Assertions.checkNoneIsNull;
25  import static org.apache.commons.beanutils2.Assertions.checkNotNull;
26  
27  import java.lang.reflect.InvocationTargetException;
28  import java.lang.reflect.Method;
29  
30  final class DefaultArgumentsAccessor
31      implements ArgumentsAccessor
32  {
33  
34      private final AccessibleObjectsRegistry<Method> methodsRegistry =
35                      AccessibleObjectsRegistry.getMethodsRegistry();
36  
37      private final Class<?> beanType;
38  
39      private final boolean isExact;
40  
41      private final String methodName;
42  
43      private final Object bean;
44  
45      public <B> DefaultArgumentsAccessor( Class<B> beanType, boolean isExact, String methodName, B bean )
46      {
47          this.beanType = beanType;
48          this.isExact = isExact;
49          this.methodName = methodName;
50          this.bean = bean;
51      }
52  
53      public BeanAccessor<?> with( Argument<?>... arguments )
54      {
55          arguments = checkNotNull( arguments,
56                                    "Impossible to invoke %s.%s with null arguments, use empty parameters instead",
57                                    beanType.getName(),
58                                    methodName );
59          arguments = checkNoneIsNull( arguments );
60  
61          Method method = methodsRegistry.get( isExact, beanType, methodName, getParameterTypes( arguments ) );
62  
63          if ( method == null )
64          {
65              throw new NoSuchBeanMethodException( methodName, beanType, null );
66          }
67  
68          Object result = invoke( method, arguments );
69  
70          if ( method.getReturnType() == void.class || result == null )
71          {
72              return new NullBeanAccessor<Object>( beanType.getName(), methodName );
73          }
74          return new DefaultBeanAccessor<Object>( result );
75      }
76  
77      private Object invoke( Method method, Argument<?>... arguments )
78      {
79          try
80          {
81              return method.invoke( bean, getParameters( arguments ) );
82          }
83          catch ( IllegalAccessException e )
84          {
85              throw new MethodNotAccessibleException( method.getName(), beanType, e );
86          }
87          catch ( InvocationTargetException e )
88          {
89              throw new MethodInvocationException( method.getName(), beanType, e );
90          }
91      }
92  
93  }