Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
MethodUpdater |
|
| 1.5714285714285714;1.571 |
1 | /* |
|
2 | * Copyright 2001-2004 The Apache Software Foundation. |
|
3 | * |
|
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 | * you may not use this file except in compliance with the License. |
|
6 | * You may obtain a copy of the License at |
|
7 | * |
|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | * |
|
10 | * Unless required by applicable law or agreed to in writing, software |
|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | * See the License for the specific language governing permissions and |
|
14 | * limitations under the License. |
|
15 | */ |
|
16 | package org.apache.commons.betwixt.expression; |
|
17 | ||
18 | ||
19 | import java.lang.reflect.Method; |
|
20 | ||
21 | import org.apache.commons.logging.Log; |
|
22 | import org.apache.commons.logging.LogFactory; |
|
23 | ||
24 | /** <p><code>MethodUpdater</code> updates the current bean context |
|
25 | * by calling a WriteMethod with the String value from the XML attribute |
|
26 | * or element.</p> |
|
27 | * |
|
28 | * @author <a href="mailto:jstrachan@apache.org">James Strachan</a> |
|
29 | * @version $Revision: 231498 $ |
|
30 | */ |
|
31 | 832 | public class MethodUpdater extends TypedUpdater { |
32 | ||
33 | /** Logger */ |
|
34 | 832 | private static Log log = LogFactory.getLog( MethodUpdater.class ); |
35 | ||
36 | /** |
|
37 | * Programmatically set log |
|
38 | * @param aLog the implementation to which this class should log |
|
39 | */ |
|
40 | public static void setLog( Log aLog ) { |
|
41 | 0 | log = aLog; |
42 | 0 | } |
43 | ||
44 | /** The method to call on the bean */ |
|
45 | private Method method; |
|
46 | /** Base constructor */ |
|
47 | 0 | public MethodUpdater() { |
48 | 0 | } |
49 | ||
50 | /** |
|
51 | * Convenience constructor sets method property |
|
52 | * @param method the Method to be invoked on the context's bean in the update |
|
53 | */ |
|
54 | 17472 | public MethodUpdater(Method method) { |
55 | 17472 | setMethod( method ); |
56 | 17472 | } |
57 | ||
58 | /** |
|
59 | * Gets the method which will be invoked by the update |
|
60 | * |
|
61 | * @return the Method to be invoked by the update |
|
62 | */ |
|
63 | public Method getMethod() { |
|
64 | 0 | return method; |
65 | } |
|
66 | ||
67 | /** |
|
68 | * Sets the constant value of this expression |
|
69 | * @param method the Method to be invoked by the update |
|
70 | */ |
|
71 | public void setMethod(Method method) { |
|
72 | 17472 | this.method = method; |
73 | 17472 | Class[] types = method.getParameterTypes(); |
74 | 17472 | if ( types == null || types.length <= 0 ) { |
75 | 0 | throw new IllegalArgumentException( "The Method must have at least one parameter" ); |
76 | } |
|
77 | 17472 | setValueType(types[0]); |
78 | 17472 | } |
79 | ||
80 | // Implementation methods |
|
81 | //------------------------------------------------------------------------- |
|
82 | ||
83 | ||
84 | ||
85 | /** |
|
86 | * Returns something useful for logging. |
|
87 | * @return something useful for logging |
|
88 | */ |
|
89 | public String toString() { |
|
90 | 39 | return "MethodUpdater [method=" + method + "]"; |
91 | } |
|
92 | ||
93 | /** |
|
94 | * Updates the bean by method invocation. |
|
95 | * @since 0.7 |
|
96 | */ |
|
97 | protected void executeUpdate(Context context, Object bean, Object newValue) throws Exception { |
|
98 | 7423 | if ( log.isDebugEnabled() ) { |
99 | 0 | log.debug( |
100 | 0 | "Calling setter method: " + method.getName() + " on bean: " + bean |
101 | 0 | + " with new value: " + newValue |
102 | ); |
|
103 | } |
|
104 | 7423 | Object[] arguments = { newValue }; |
105 | try |
|
106 | { |
|
107 | 7423 | method.invoke( bean, arguments ); |
108 | } |
|
109 | 0 | catch (IllegalAccessException e) |
110 | { |
|
111 | 0 | method.setAccessible(true); |
112 | 0 | method.invoke( bean, arguments ); |
113 | } |
|
114 | 7423 | } |
115 | } |