KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > encoding > MethodTarget


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 JavaDoc;
23
24 import java.lang.reflect.InvocationTargetException JavaDoc;
25 import java.lang.reflect.Method JavaDoc;
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 JavaDoc targetObject;
35     private Method JavaDoc targetMethod;
36     private static final Class JavaDoc [] objArg = new Class JavaDoc [] { Object JavaDoc.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 JavaDoc targetObject, Method JavaDoc 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 JavaDoc targetObject, String JavaDoc methodName)
55         throws NoSuchMethodException JavaDoc
56     {
57         this.targetObject = targetObject;
58         Class JavaDoc 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 JavaDoc value) throws SAXException JavaDoc {
67         try {
68             targetMethod.invoke(targetObject, new Object JavaDoc [] { value });
69         } catch (IllegalAccessException JavaDoc accEx) {
70             log.error(Messages.getMessage("illegalAccessException00"),
71                       accEx);
72             throw new SAXException JavaDoc(accEx);
73         } catch (IllegalArgumentException JavaDoc argEx) {
74             log.error(Messages.getMessage("illegalArgumentException00"),
75                       argEx);
76             throw new SAXException JavaDoc(argEx);
77         } catch (InvocationTargetException JavaDoc targetEx) {
78             log.error(Messages.getMessage("invocationTargetException00"),
79                       targetEx);
80             throw new SAXException JavaDoc(targetEx);
81         }
82     }
83 }
84
Popular Tags