1
2
3
4
5
6
7
8
9
10
11
12
13
14
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: 190515 $
30 */
31 public class MethodUpdater extends TypedUpdater {
32
33 /*** Logger */
34 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 log = aLog;
42 }
43
44 /*** The method to call on the bean */
45 private Method method;
46 /*** Base constructor */
47 public MethodUpdater() {
48 }
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 public MethodUpdater(Method method) {
55 setMethod( method );
56 }
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 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 this.method = method;
73 Class[] types = method.getParameterTypes();
74 if ( types == null || types.length <= 0 ) {
75 throw new IllegalArgumentException( "The Method must have at least one parameter" );
76 }
77 setValueType(types[0]);
78 }
79
80
81
82
83
84
85 /***
86 * Returns something useful for logging.
87 * @return something useful for logging
88 */
89 public String toString() {
90 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 if ( log.isDebugEnabled() ) {
99 log.debug(
100 "Calling setter method: " + method.getName() + " on bean: " + bean
101 + " with new value: " + newValue
102 );
103 }
104 Object[] arguments = { newValue };
105 method.invoke( bean, arguments );
106 }
107 }