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.config.annotations.reflect;
022:
023: import java.lang.annotation.Annotation;
024: import java.lang.reflect.AnnotatedElement;
025: import java.lang.reflect.Field;
026: import java.util.HashMap;
027: import java.util.Map;
028:
029: import com.db4o.config.Configuration;
030: import com.db4o.config.annotations.CalledConstructor;
031: import com.db4o.config.annotations.GeneratedUUIDs;
032: import com.db4o.config.annotations.GeneratedVersionNumbers;
033: import com.db4o.config.annotations.Indexed;
034: import com.db4o.config.annotations.PersistedStaticFieldValues;
035: import com.db4o.config.annotations.StoredTransientFields;
036: import com.db4o.config.annotations.UpdatedDepth;
037: import com.db4o.internal.*;
038:
039: /**
040: * sets db4o configurations accordingly annotations
041: *
042: * @exclude
043: */
044: public class ConfigurationIntrospector {
045:
046: Map<Class<? extends Annotation>, Db4oConfiguratorFactory> _configurators;
047:
048: Config4Class _classConfig;
049:
050: Class _clazz;
051:
052: Configuration _config;
053:
054: public ConfigurationIntrospector(Class clazz, Configuration config,
055: Config4Class classConfig) throws Exception {
056: this ._classConfig = classConfig;
057: this ._clazz = clazz;
058: this ._config = config;
059:
060: initMap();
061: }
062:
063: private void initMap() throws NoSuchMethodException {
064: _configurators = new HashMap<Class<? extends Annotation>, Db4oConfiguratorFactory>();
065: _configurators.put(UpdatedDepth.class,
066: new UpdatedDepthFactory());
067: _configurators.put(Indexed.class,
068: new NoArgsFieldConfiguratorFactory(
069: IndexedConfigurator.class));
070: _configurators.put(CalledConstructor.class,
071: new CalledConstructorFactory());
072: _configurators.put(GeneratedUUIDs.class,
073: new GeneratedUUIDsFactory());
074: _configurators.put(GeneratedVersionNumbers.class,
075: new GeneratedVersionNumbersFactory());
076: _configurators.put(StoredTransientFields.class,
077: new StoredTransientFieldsFactory());
078: _configurators.put(PersistedStaticFieldValues.class,
079: new PersistedStaticFieldValuesFactory());
080: }
081:
082: /**
083: * the start methode to reflect user class and fields <br>
084: * in order to set appropriate configurations
085: *
086: * @param clazz
087: * Java class to reflect
088: * @return classConfig configurations of class
089: */
090: public Config4Class apply() {
091: try {
092: reflectClass();
093: reflectFields();
094:
095: } catch (SecurityException e) {
096: e.printStackTrace();
097: }
098: return _classConfig;
099: }
100:
101: private void reflectClass() {
102: Annotation[] annotations = _clazz.getAnnotations();
103: for (Annotation a : annotations) {
104: applyAnnotation(_clazz, a);
105: }
106: }
107:
108: private void reflectFields() {
109:
110: Field[] declaredFields;
111: try {
112: declaredFields = _clazz.getDeclaredFields();
113: for (Field f : declaredFields) {
114: for (Annotation a : f.getAnnotations()) {
115: applyAnnotation(f, a);
116: }
117: }
118: } catch (SecurityException e) {
119: e.printStackTrace();
120: }
121: }
122:
123: private void applyAnnotation(AnnotatedElement element, Annotation a) {
124: if (_configurators.containsKey(a.annotationType())) {
125: Db4oConfigurator configurator = _configurators.get(
126: a.annotationType()).configuratorFor(element, a);
127: _classConfig = (Config4Class) configurator
128: .configure(_config);
129: }
130: }
131: }
|