Source code: org/apache/axis/encoding/MethodTarget.java
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
17 package org.apache.axis.encoding;
18
19 import org.apache.axis.components.logger.LogFactory;
20 import org.apache.axis.utils.Messages;
21 import org.apache.commons.logging.Log;
22 import org.xml.sax.SAXException;
23
24 import java.lang.reflect.InvocationTargetException;
25 import java.lang.reflect.Method;
26
27
28 // Target is set via a method call. The set method places the value in the field.
29 public class MethodTarget implements Target
30 {
31 protected static Log log =
32 LogFactory.getLog(MethodTarget.class.getName());
33
34 private Object targetObject;
35 private Method targetMethod;
36 private static final Class [] objArg = new Class [] { Object.class };
37
38 /**
39 * Construct a target whose value is set via a method
40 * @param targetObject is the object containing the value to be set
41 * @param targetMethod is the Method used to set the value
42 */
43 public MethodTarget(Object targetObject, Method targetMethod)
44 {
45 this.targetObject = targetObject;
46 this.targetMethod = targetMethod;
47 }
48
49 /**
50 * Construct a target whose value is set via a method
51 * @param targetObject is the object containing the value to be set
52 * @param methodName is the name of the Method
53 */
54 public MethodTarget(Object targetObject, String methodName)
55 throws NoSuchMethodException
56 {
57 this.targetObject = targetObject;
58 Class cls = targetObject.getClass();
59 targetMethod = cls.getMethod(methodName, objArg);
60 }
61
62 /**
63 * Set the target's value by invoking the targetMethod.
64 * @param value is the new Object value
65 */
66 public void set(Object value) throws SAXException {
67 try {
68 targetMethod.invoke(targetObject, new Object [] { value });
69 } catch (IllegalAccessException accEx) {
70 log.error(Messages.getMessage("illegalAccessException00"),
71 accEx);
72 throw new SAXException(accEx);
73 } catch (IllegalArgumentException argEx) {
74 log.error(Messages.getMessage("illegalArgumentException00"),
75 argEx);
76 throw new SAXException(argEx);
77 } catch (InvocationTargetException targetEx) {
78 log.error(Messages.getMessage("invocationTargetException00"),
79 targetEx);
80 throw new SAXException(targetEx);
81 }
82 }
83 }