001: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
002:
003: This file is part of the db4o open source object database.
004:
005: db4o is free software; you can redistribute it and/or modify it under
006: the terms of version 2 of the GNU General Public License as published
007: by the Free Software Foundation and as clarified by db4objects' GPL
008: interpretation policy, available at
009: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
010: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
011: Suite 350, San Mateo, CA 94403, USA.
012:
013: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
014: WARRANTY; without even the implied warranty of MERCHANTABILITY or
015: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
016: for more details.
017:
018: You should have received a copy of the GNU General Public License along
019: with this program; if not, write to the Free Software Foundation, Inc.,
020: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
021: package com.db4o.reflect.generic;
022:
023: import com.db4o.foundation.*;
024: import com.db4o.reflect.*;
025:
026: /**
027: * @exclude
028: */
029: public class GenericField implements ReflectField, DeepClone {
030:
031: private final String _name;
032: private final GenericClass _type;
033: private final boolean _primitive;
034:
035: private int _index = -1;
036:
037: public GenericField(String name, ReflectClass clazz,
038: boolean primitive) {
039: _name = name;
040: _type = (GenericClass) clazz;
041: _primitive = primitive;
042: }
043:
044: public Object deepClone(Object obj) {
045: Reflector reflector = (Reflector) obj;
046: ReflectClass newReflectClass = null;
047: if (_type != null) {
048: newReflectClass = reflector.forName(_type.getName());
049: }
050: return new GenericField(_name, newReflectClass, _primitive);
051: }
052:
053: public Object get(Object onObject) {
054: //TODO Consider: Do we need to check that onObject is an instance of the DataClass this field is a member of?
055: return ((GenericObject) onObject).get(_index);
056: }
057:
058: public String getName() {
059: return _name;
060: }
061:
062: public ReflectClass getFieldType() {
063: return _type;
064: }
065:
066: public boolean isPublic() {
067: return true;
068: }
069:
070: public boolean isPrimitive() {
071: return _primitive;
072: }
073:
074: public boolean isStatic() { //FIXME Consider static fields.
075: return false;
076: }
077:
078: public boolean isTransient() {
079: return false;
080: }
081:
082: public void set(Object onObject, Object value) {
083: // FIXME: Consider enabling type checking.
084: // The following will fail with arrays.
085: // if (!_type.isInstance(value)) throw new RuntimeException(); //TODO Consider: is this checking really necessary?
086: ((GenericObject) onObject).set(_index, value);
087: }
088:
089: public void setAccessible() {
090: // do nothing
091: }
092:
093: void setIndex(int index) {
094: _index = index;
095: }
096:
097: public Object indexEntry(Object orig) {
098: return orig;
099: }
100:
101: public ReflectClass indexType() {
102: return getFieldType();
103: }
104: }
|