1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.jdo.impl.enhancer.meta.util;
20
21 import java.io.PrintWriter;
22
23 import java.util.Iterator;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.ArrayList;
27
28 import org.apache.jdo.impl.enhancer.meta.EnhancerMetaData;
29 import org.apache.jdo.impl.enhancer.meta.EnhancerMetaDataFatalError;
30 import org.apache.jdo.impl.enhancer.meta.EnhancerMetaDataUserException;
31 import org.apache.jdo.impl.enhancer.util.Support;
32
33
34
35 /***
36 * Provides the JDO meta information based on a JDO meta model.
37 */
38 public abstract class EnhancerMetaDataBaseModel
39 extends Support
40 implements EnhancerMetaData
41 {
42
43 protected boolean verbose = true;
44 protected final PrintWriter out;
45
46
47 static protected final HashSet unenhancableTypePrefixes = new HashSet();
48 static
49 {
50 unenhancableTypePrefixes.add("java/");
51 unenhancableTypePrefixes.add("javax/");
52 }
53
54 /***
55 * Creates an instance.
56 */
57 public EnhancerMetaDataBaseModel(PrintWriter out,
58 boolean verbose)
59 throws EnhancerMetaDataUserException, EnhancerMetaDataFatalError
60 {
61 affirm(out != null);
62 this.out = out;
63 }
64
65 /***
66 * Prints out a warning message.
67 *
68 * @param msg the message
69 */
70 public void printWarning(String msg)
71 {
72 out.println(getI18N("enhancer.metadata.warning", msg));
73 }
74
75 /***
76 * Prints out a verbose message.
77 *
78 * @param msg the message
79 */
80 public void printMessage(String msg)
81 {
82 if (verbose) {
83 out.println(getI18N("enhancer.metadata.message", msg));
84 }
85 }
86
87 /***
88 * Returns whether a class is not to be modified by the enhancer.
89 *
90 * @see EnhancerMetaData#isKnownUnenhancableClass(String)
91 */
92 public boolean isKnownUnenhancableClass(String classPath)
93 throws EnhancerMetaDataUserException, EnhancerMetaDataFatalError
94 {
95
96 for (Iterator i = unenhancableTypePrefixes.iterator(); i.hasNext();) {
97 final String typePrefix = (String)i.next();
98 if (classPath.startsWith(typePrefix))
99 return true;
100 }
101 return false;
102 }
103
104 /***
105 * Returns whether a class is persistence-capable root class.
106 *
107 * @see EnhancerMetaData#isPersistenceCapableRootClass(String)
108 */
109 public boolean isPersistenceCapableRootClass(String classPath)
110 throws EnhancerMetaDataUserException, EnhancerMetaDataFatalError
111 {
112 return (isPersistenceCapableClass(classPath)
113 && (getPersistenceCapableSuperClass(classPath) == null));
114 }
115
116 /***
117 * Returns the name of the persistence-capable root class of a class.
118 *
119 * @see EnhancerMetaData#getPersistenceCapableRootClass(String)
120 */
121 public String getPersistenceCapableRootClass(String classPath)
122 throws EnhancerMetaDataUserException, EnhancerMetaDataFatalError
123 {
124 if (!isPersistenceCapableClass(classPath)) {
125 return null;
126 }
127
128 String pcRootClass;
129 String clazz = classPath;
130 do {
131 pcRootClass = clazz;
132 clazz = getPersistenceCapableSuperClass(clazz);
133 } while (clazz != null);
134 return pcRootClass;
135 }
136
137 /***
138 * Returns the name of the key class of the next persistence-capable
139 * superclass that defines one.
140 *
141 * @see EnhancerMetaData#getSuperKeyClass(String)
142 */
143 public String getSuperKeyClass(String classPath)
144 throws EnhancerMetaDataUserException, EnhancerMetaDataFatalError
145 {
146 for (String superClass = getPersistenceCapableSuperClass(classPath);
147 superClass != null;
148 superClass = getPersistenceCapableSuperClass(superClass)) {
149 final String superKeyClass = getKeyClass(superClass);
150 if (superKeyClass != null) {
151 return superKeyClass;
152 }
153 }
154 return null;
155 }
156
157 /***
158 * Returns whether a field of a class is known to be either transient
159 * transactional or persistent.
160 *
161 * @see EnhancerMetaData#isManagedField(String, String)
162 */
163 public boolean isManagedField(String classPath, String fieldName)
164 throws EnhancerMetaDataUserException, EnhancerMetaDataFatalError
165 {
166 return (isPersistentField(classPath, fieldName)
167 || isTransactionalField(classPath, fieldName));
168 }
169
170 /***
171 * Returns the field flags of a declared, managed field of a class.
172 *
173 * @see EnhancerMetaData#getFieldFlags(String, String)
174 */
175 public int getFieldFlags(String classPath, String fieldName)
176 throws EnhancerMetaDataUserException, EnhancerMetaDataFatalError
177 {
178 if (!isManagedField(classPath, fieldName)) {
179 affirm(!isTransactionalField(classPath, fieldName));
180 affirm(!isPersistentField(classPath, fieldName));
181 affirm(!isKeyField(classPath, fieldName));
182 affirm(!isDefaultFetchGroupField(classPath, fieldName));
183 return 0;
184 }
185
186
187 if (isTransactionalField(classPath, fieldName)) {
188 affirm(!isPersistentField(classPath, fieldName));
189 affirm(!isKeyField(classPath, fieldName));
190
191
192 return CHECK_WRITE;
193 }
194
195 affirm(isPersistentField(classPath, fieldName));
196
197 if (isKeyField(classPath, fieldName)) {
198
199
200 return MEDIATE_WRITE;
201 }
202
203
204 if (isDefaultFetchGroupField(classPath, fieldName)) {
205 return CHECK_READ | CHECK_WRITE;
206 }
207
208
209 return MEDIATE_READ | MEDIATE_WRITE;
210 }
211
212 /***
213 * Returns an array of field names of all key fields of a class.
214 *
215 * @see EnhancerMetaData#getKeyFields(String)
216 */
217 public String[] getKeyFields(String classPath)
218 throws EnhancerMetaDataUserException, EnhancerMetaDataFatalError
219 {
220 final List keys = new ArrayList();
221 final String[] fieldNames = getManagedFields(classPath);
222 final int n = fieldNames.length;
223 for (int i = 0; i < n; i++) {
224 if (isKeyField(classPath, fieldNames[i])) {
225 keys.add(fieldNames[i]);
226 }
227 }
228 return (String[])keys.toArray(new String[keys.size()]);
229 }
230
231 /***
232 * Returns the field flags for some declared, managed fields of a class.
233 *
234 * @see EnhancerMetaData#getFieldFlags(String, String[])
235 */
236 public int[] getFieldFlags(String classPath, String[] fieldNames)
237 throws EnhancerMetaDataUserException, EnhancerMetaDataFatalError
238 {
239 final int n = (fieldNames != null ? fieldNames.length : 0);
240 final int[] flags = new int[n];
241 for (int i = 0; i < n; i++) {
242 flags[i] = getFieldFlags(classPath, fieldNames[i]);
243 }
244 return flags;
245 }
246
247 /***
248 * Returns the unique field index of some declared, managed fields of a
249 * class.
250 *
251 * @see EnhancerMetaData#getFieldNumber(String, String[])
252 */
253 public int[] getFieldNumber(String classPath, String[] fieldNames)
254 throws EnhancerMetaDataUserException, EnhancerMetaDataFatalError
255 {
256 final int n = (fieldNames != null ? fieldNames.length : 0);
257 final int[] flags = new int[n];
258 for (int i = 0; i < n; i++) {
259 flags[i] = getFieldNumber(classPath, fieldNames[i]);
260 }
261 return flags;
262 }
263 }