KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > db4o > config > annotations > reflect > ConfigurationIntrospector


1 /* Copyright (C) 2004 - 2006 db4objects Inc. http://www.db4o.com
2
3 This file is part of the db4o open source object database.
4
5 db4o is free software; you can redistribute it and/or modify it under
6 the terms of version 2 of the GNU General Public License as published
7 by the Free Software Foundation and as clarified by db4objects' GPL
8 interpretation policy, available at
9 http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10 Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11 Suite 350, San Mateo, CA 94403, USA.
12
13 db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with this program; if not, write to the Free Software Foundation, Inc.,
20 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

21 package com.db4o.config.annotations.reflect;
22
23 import java.lang.annotation.Annotation JavaDoc;
24 import java.lang.reflect.AnnotatedElement JavaDoc;
25 import java.lang.reflect.Field JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28
29 import com.db4o.Config4Class;
30 import com.db4o.config.Configuration;
31 import com.db4o.config.annotations.CalledConstructor;
32 import com.db4o.config.annotations.GeneratedUUIDs;
33 import com.db4o.config.annotations.GeneratedVersionNumbers;
34 import com.db4o.config.annotations.Indexed;
35 import com.db4o.config.annotations.PersistedStaticFieldValues;
36 import com.db4o.config.annotations.StoredTransientFields;
37 import com.db4o.config.annotations.UpdatedDepth;
38
39 /**
40  * sets db4o configurations accordingly annotations
41  *
42  * @exclude
43  */

44 public class ConfigurationIntrospector {
45
46     Map JavaDoc<Class JavaDoc<? extends Annotation JavaDoc>, Db4oConfiguratorFactory> _configurators;
47
48     Config4Class _classConfig;
49
50     Class JavaDoc _clazz;
51
52     Configuration _config;
53
54     public ConfigurationIntrospector(Class JavaDoc clazz, Configuration config,
55             Config4Class classConfig) throws Exception JavaDoc {
56         this._classConfig = classConfig;
57         this._clazz = clazz;
58         this._config = config;
59
60         initMap();
61     }
62
63     private void initMap() throws NoSuchMethodException JavaDoc {
64         _configurators = new HashMap JavaDoc<Class JavaDoc<? extends Annotation JavaDoc>, Db4oConfiguratorFactory>();
65         _configurators.put(UpdatedDepth.class, new UpdatedDepthFactory());
66         _configurators.put(Indexed.class, new NoArgsFieldConfiguratorFactory(
67                 IndexedConfigurator.class));
68         _configurators.put(CalledConstructor.class, new CalledConstructorFactory());
69         _configurators.put(GeneratedUUIDs.class,
70                 new GeneratedUUIDsFactory());
71         _configurators.put(GeneratedVersionNumbers.class,
72                 new GeneratedVersionNumbersFactory());
73         _configurators.put(StoredTransientFields.class,
74                 new StoredTransientFieldsFactory());
75         _configurators.put(PersistedStaticFieldValues.class,
76                 new PersistedStaticFieldValuesFactory());
77     }
78
79     /**
80      * the start methode to reflect user class and fields <br>
81      * in order to set appropriate configurations
82      *
83      * @param clazz
84      * Java class to reflect
85      * @return classConfig configurations of class
86      */

87     public Config4Class apply() {
88         try {
89             reflectClass();
90             reflectFields();
91
92         } catch (SecurityException JavaDoc e) {
93             e.printStackTrace();
94         }
95         return _classConfig;
96     }
97
98     private void reflectClass() {
99         Annotation JavaDoc[] annotations = _clazz.getAnnotations();
100         for (Annotation JavaDoc a : annotations) {
101             applyAnnotation(_clazz, a);
102         }
103     }
104
105     private void reflectFields() {
106
107         Field JavaDoc[] declaredFields;
108         try {
109             declaredFields = _clazz.getDeclaredFields();
110             for (Field JavaDoc f : declaredFields) {
111                 for (Annotation JavaDoc a : f.getAnnotations()) {
112                     applyAnnotation(f, a);
113                 }
114             }
115         } catch (SecurityException JavaDoc e) {
116             e.printStackTrace();
117         }
118     }
119
120     private void applyAnnotation(AnnotatedElement JavaDoc element, Annotation JavaDoc a) {
121         if (_configurators.containsKey(a.annotationType())) {
122             Db4oConfigurator configurator = _configurators.get(
123                     a.annotationType()).configuratorFor(element, a);
124             _classConfig = (Config4Class) configurator.configure(_config);
125         }
126     }
127 }
128
Popular Tags