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.db4ounit.common.reflect.custom;
022:
023: import com.db4o.foundation.*;
024: import com.db4o.internal.*;
025: import com.db4o.reflect.*;
026:
027: /**
028: * Type information is handled by CustomClassRepository.
029: */
030: public class CustomReflector implements Reflector {
031:
032: private final Reflector _delegate = Platform4
033: .reflectorForType(Object.class);
034: private final CustomClassRepository _classRepository;
035: private Reflector _parent;
036:
037: public CustomReflector(CustomClassRepository classRepository) {
038: classRepository.initialize(this );
039: _classRepository = classRepository;
040: }
041:
042: public ReflectArray array() {
043: return _delegate.array();
044: }
045:
046: public boolean constructorCallsSupported() {
047: return false;
048: }
049:
050: public ReflectClass forClass(Class clazz) {
051: return _delegate.forClass(clazz);
052: }
053:
054: public ReflectClass forName(String className) {
055: logMethodCall("forName", className);
056:
057: ReflectClass klass = repositoryForName(className);
058: if (null != klass) {
059: return klass;
060: }
061: return _delegate.forName(className);
062: }
063:
064: private ReflectClass repositoryForName(String className) {
065: if (_classRepository == null) {
066: return null;
067: }
068: return _classRepository.forName(className);
069: }
070:
071: public ReflectClass forObject(Object obj) {
072: logMethodCall("forObject", obj);
073:
074: ReflectClass klass = repositoryForObject(obj);
075: if (null != klass) {
076: return klass;
077: }
078: return _delegate.forObject(obj);
079: }
080:
081: private ReflectClass repositoryForObject(Object obj) {
082: if (_classRepository == null) {
083: return null;
084: }
085:
086: if (!(obj instanceof PersistentEntry)) {
087: return null;
088: }
089:
090: PersistentEntry entry = (PersistentEntry) obj;
091: return _classRepository.forName(entry.className);
092: }
093:
094: public boolean isCollection(ReflectClass clazz) {
095: return _delegate.isCollection(clazz);
096: }
097:
098: public void setParent(Reflector reflector) {
099: logMethodCall("setParent", reflector);
100: _parent = reflector;
101: _delegate.setParent(reflector);
102: }
103:
104: public Object deepClone(Object context) {
105: logMethodCall("deepClone", context);
106: throw new NotImplementedException();
107: }
108:
109: public CustomClass defineClass(String className,
110: String[] fieldNames, String[] fieldTypes) {
111: return _classRepository.defineClass(className, fieldNames,
112: fieldTypes);
113: }
114:
115: public String toString() {
116: return "CustomReflector(" + _classRepository + ")";
117: }
118:
119: private void logMethodCall(String methodName, Object arg) {
120: Logger.logMethodCall(this , methodName, arg);
121: }
122:
123: public ReflectClass forFieldType(Class type) {
124: return _parent.forClass(type);
125: }
126:
127: public Iterator4 customClasses() {
128: return _classRepository.iterator();
129: }
130: }
|