001: /*
002: * Copyright (c) 1998-2007 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Emil Ong
028: */
029:
030: package com.caucho.jaxb.accessor;
031:
032: import javax.xml.bind.JAXBException;
033: import java.lang.annotation.Annotation;
034: import java.lang.reflect.Method;
035: import java.lang.reflect.Type;
036:
037: public class GetterSetterAccessor extends Accessor {
038: private final Method _get;
039: private final Method _set;
040: private final Class _type;
041: private final Type _genericType;
042: private final Package _package;
043:
044: public GetterSetterAccessor(String name, Method get, Method set)
045: throws JAXBException {
046: Class declarer = get.getDeclaringClass();
047:
048: _package = declarer.getPackage();
049: _get = get;
050: _set = set;
051: _name = name;
052: _type = _get.getReturnType();
053: _genericType = _get.getGenericReturnType();
054:
055: if ("clazz".equals(_name))
056: _name = "class";
057: }
058:
059: public Object get(Object o) throws JAXBException {
060: try {
061: if (_get == null)
062: return null;
063:
064: return _get.invoke(o);
065: } catch (Exception e) {
066: throw new JAXBException(e);
067: }
068: }
069:
070: public void set(Object o, Object value) throws JAXBException {
071: try {
072: if (_set == null)
073: return;
074:
075: _set.invoke(o, value);
076: } catch (Exception e) {
077: throw new JAXBException(e);
078: }
079: }
080:
081: public <A extends Annotation> A getAnnotation(Class<A> c) {
082: A a = null;
083:
084: if (_get != null)
085: a = _get.getAnnotation(c);
086:
087: if (a == null && _set != null)
088: a = _set.getAnnotation(c);
089:
090: return a;
091: }
092:
093: public <A extends Annotation> A getPackageAnnotation(Class<A> c) {
094: return _package.getAnnotation(c);
095: }
096:
097: public Package getPackage() {
098: return _package;
099: }
100:
101: public Class getType() {
102: return _get.getReturnType();
103: }
104:
105: public Type getGenericType() {
106: return _get.getGenericReturnType();
107: }
108:
109: public String getName() {
110: return _name;
111: }
112:
113: public String toString() {
114: return "GetterSetterAccessor["
115: + _get.getDeclaringClass().getName() + "." + getName()
116: + "]";
117: }
118: }
|