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.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 }