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.internal;
022:
023: import com.db4o.reflect.*;
024:
025: /**
026: * @exclude
027: */
028: class ObjectAnalyzer {
029:
030: private final PartialObjectContainer _container;
031:
032: private final Object _obj;
033:
034: private ClassMetadata _classMetadata;
035:
036: private ObjectReference _ref;
037:
038: private boolean _notStorable;
039:
040: ObjectAnalyzer(PartialObjectContainer container, Object obj) {
041: _container = container;
042: _obj = obj;
043: }
044:
045: void analyze(Transaction trans) {
046: _ref = trans.referenceForObject(_obj);
047: if (_ref == null) {
048: ReflectClass claxx = _container.reflector().forObject(_obj);
049: if (claxx == null) {
050: notStorable(_obj, claxx);
051: return;
052: }
053: if (!detectClassMetadata(trans, claxx)) {
054: return;
055: }
056: } else {
057: _classMetadata = _ref.classMetadata();
058: }
059:
060: if (isPlainObjectOrPrimitive(_classMetadata)) {
061: notStorable(_obj, _classMetadata.classReflector());
062: }
063:
064: }
065:
066: private boolean detectClassMetadata(Transaction trans,
067: ReflectClass claxx) {
068: _classMetadata = _container.getActiveClassMetadata(claxx);
069: if (_classMetadata == null) {
070: _classMetadata = _container.produceClassMetadata(claxx);
071: if (_classMetadata == null) {
072: notStorable(_obj, claxx);
073: return false;
074: }
075:
076: // The following may return a reference if the object is held
077: // in a static variable somewhere ( often: Enums) that gets
078: // stored or associated on initialization of the ClassMetadata.
079:
080: _ref = trans.referenceForObject(_obj);
081: }
082: return true;
083: }
084:
085: private void notStorable(Object obj, ReflectClass claxx) {
086: _container.notStorable(claxx, obj);
087: _notStorable = true;
088: }
089:
090: boolean notStorable() {
091: return _notStorable;
092: }
093:
094: private final boolean isPlainObjectOrPrimitive(
095: ClassMetadata classMetadata) {
096: return classMetadata.getID() == Handlers4.UNTYPED_ID
097: || classMetadata.isPrimitive();
098: }
099:
100: ObjectReference objectReference() {
101: return _ref;
102: }
103:
104: public ClassMetadata classMetadata() {
105: return _classMetadata;
106: }
107:
108: }
|