KickJava   Java API By Example, From Geeks To Geeks.

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


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 GetterSetterAccessor extends Accessor {
50   private Method JavaDoc _get;
51   private Method JavaDoc _set;
52   private Class JavaDoc _type;
53   private Type _genericType;
54   private String JavaDoc _name;
55
56   private PropertyDescriptor JavaDoc _propertyDescriptor;
57
58   public GetterSetterAccessor(PropertyDescriptor JavaDoc propertyDescriptor,
59                               JAXBContextImpl context)
60     throws JAXBException
61   {
62     super(context);
63
64     _propertyDescriptor = propertyDescriptor;
65
66     _get = _propertyDescriptor.getReadMethod();
67     _set = _propertyDescriptor.getWriteMethod();
68     _name = _propertyDescriptor.getName();
69     _genericType = _get.getGenericReturnType();
70     _property = _context.createProperty(_genericType);
71
72     if ("clazz".equals(_name))
73       _name = "class";
74   }
75
76   public Object JavaDoc get(Object JavaDoc o)
77     throws JAXBException
78   {
79     try {
80       if (_get == null)
81         return null;
82
83       return _get.invoke(o);
84     }
85     catch (Exception JavaDoc e) {
86       throw new JAXBException(e);
87     }
88   }
89
90   public void set(Object JavaDoc o, Object JavaDoc value)
91     throws JAXBException
92   {
93     try {
94       if (_set == null)
95         return;
96
97       _set.invoke(o, value);
98     }
99     catch (Exception JavaDoc e) {
100       throw new JAXBException(e);
101     }
102   }
103
104   public <A extends Annotation JavaDoc> A getAnnotation(Class JavaDoc<A> c)
105   {
106     A a = null;
107
108     if (_get != null)
109       a = _get.getAnnotation(c);
110     else if (_set != null)
111       a = _set.getAnnotation(c);
112
113     return a;
114   }
115
116   public Class JavaDoc getType()
117   {
118     return _propertyDescriptor.getPropertyType();
119   }
120
121   public Type getGenericType()
122   {
123     return getType();
124   }
125
126   public String JavaDoc getName()
127   {
128     return _name;
129   }
130 }
131
Popular Tags