KickJava   Java API By Example, From Geeks To Geeks.

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


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.Field JavaDoc;
25
26
27 // Target is a field. The set method places the value in the field.
28
public class FieldTarget implements Target
29 {
30     protected static Log log =
31         LogFactory.getLog(FieldTarget.class.getName());
32
33     private Object JavaDoc targetObject;
34     private Field JavaDoc targetField;
35     
36     public FieldTarget(Object JavaDoc targetObject, Field JavaDoc targetField)
37     {
38         this.targetObject = targetObject;
39         this.targetField = targetField;
40     }
41     
42     public FieldTarget(Object JavaDoc targetObject, String JavaDoc fieldName)
43         throws NoSuchFieldException JavaDoc
44     {
45         Class JavaDoc cls = targetObject.getClass();
46         targetField = cls.getField(fieldName);
47         this.targetObject = targetObject;
48     }
49     
50     public void set(Object JavaDoc value) throws SAXException JavaDoc {
51         try {
52             targetField.set(targetObject, value);
53         } catch (IllegalAccessException JavaDoc accEx) {
54             log.error(Messages.getMessage("illegalAccessException00"),
55                       accEx);
56             throw new SAXException JavaDoc(accEx);
57         } catch (IllegalArgumentException JavaDoc argEx) {
58             log.error(Messages.getMessage("illegalArgumentException00"),
59                       argEx);
60             throw new SAXException JavaDoc(argEx);
61         }
62     }
63 }
64
Popular Tags