KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jaxb > skeleton > FieldAccessor


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Emil Ong
28  */

29
30 package com.caucho.jaxb.skeleton;
31
32 import com.caucho.jaxb.JAXBContextImpl;
33 import com.caucho.util.L10N;
34
35 import javax.xml.bind.JAXBException;
36 import javax.xml.bind.annotation.XmlAttribute;
37 import javax.xml.bind.annotation.XmlElement;
38 import javax.xml.bind.annotation.XmlType;
39 import javax.xml.namespace.QName JavaDoc;
40 import java.beans.PropertyDescriptor JavaDoc;
41 import java.lang.annotation.Annotation JavaDoc;
42 import java.lang.reflect.Field JavaDoc;
43 import java.lang.reflect.GenericArrayType JavaDoc;
44 import java.lang.reflect.Method JavaDoc;
45 import java.lang.reflect.ParameterizedType JavaDoc;
46 import java.lang.reflect.Type JavaDoc;
47 import java.util.Map JavaDoc;
48
49 public class FieldAccessor extends Accessor {
50   private Field JavaDoc _field;
51   private Class JavaDoc _type;
52   private Type _genericType;
53   private String JavaDoc _name;
54
55   public FieldAccessor(Field JavaDoc f, JAXBContextImpl context)
56     throws JAXBException
57   {
58     super(context);
59
60     _field = f;
61     _type = _field.getType();
62     _genericType = _field.getGenericType();
63     _name = _field.getName();
64     _property = _context.createProperty(_genericType);
65
66     if (_field.isAnnotationPresent(XmlElement.class)) {
67       XmlElement element = _field.getAnnotation(XmlElement.class);
68
69       if (! "##default".equals(element.name()))
70         _name = element.name();
71     }
72     else if (_field.isAnnotationPresent(XmlAttribute.class)) {
73       XmlAttribute attribute = _field.getAnnotation(XmlAttribute.class);
74
75       if (! "##default".equals(attribute.name()))
76         _name = attribute.name();
77     }
78   }
79
80   public Object JavaDoc get(Object JavaDoc o)
81     throws JAXBException
82   {
83     try {
84       return _field.get(o);
85     }
86     catch (Exception JavaDoc e) {
87       throw new JAXBException(e);
88     }
89   }
90
91   public void set(Object JavaDoc o, Object JavaDoc value)
92     throws JAXBException
93   {
94     try {
95       _field.set(o, value);
96     }
97     catch (Exception JavaDoc e) {
98       throw new JAXBException(e);
99     }
100   }
101
102   public <A extends Annotation JavaDoc> A getAnnotation(Class JavaDoc<A> c)
103   {
104     return _field.getAnnotation(c);
105   }
106
107   public Class JavaDoc getType()
108   {
109     return _type;
110   }
111
112   public Type getGenericType()
113   {
114     return _genericType;
115   }
116
117   public String JavaDoc getName()
118   {
119     return _name;
120   }
121 }
122
Popular Tags