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