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 com.caucho.jaxb.JAXBContextImpl;
033: import com.caucho.util.L10N;
034:
035: import javax.xml.bind.JAXBException;
036: import java.lang.annotation.Annotation;
037: import java.lang.reflect.Field;
038: import java.lang.reflect.Type;
039:
040: public class FieldAccessor extends Accessor {
041: private final Package _package;
042: private final Field _field;
043: private final Class _type;
044: private final Type _genericType;
045:
046: public FieldAccessor(Field f) throws JAXBException {
047: Class declarer = f.getDeclaringClass();
048:
049: _package = declarer.getPackage();
050: _field = f;
051: _type = _field.getType();
052: _genericType = _field.getGenericType();
053: _name = _field.getName();
054: }
055:
056: public Object get(Object o) throws JAXBException {
057: try {
058: return _field.get(o);
059: } catch (Exception e) {
060: throw new JAXBException(e);
061: }
062: }
063:
064: public void set(Object o, Object value) throws JAXBException {
065: try {
066: _field.set(o, value);
067: } catch (Exception e) {
068: throw new JAXBException(e);
069: }
070: }
071:
072: public <A extends Annotation> A getAnnotation(Class<A> c) {
073: return _field.getAnnotation(c);
074: }
075:
076: public <A extends Annotation> A getPackageAnnotation(Class<A> c) {
077: return _package.getAnnotation(c);
078: }
079:
080: public Package getPackage() {
081: return _package;
082: }
083:
084: public Class getType() {
085: return _type;
086: }
087:
088: public Type getGenericType() {
089: return _genericType;
090: }
091:
092: public String getName() {
093: return _name;
094: }
095:
096: public String toString() {
097: return "FieldAccessor[" + _field.getDeclaringClass().getName()
098: + "." + _field.getName() + "]";
099: }
100: }
|