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.prop;
19
20 import java.lang.reflect.Modifier;
21
22 import java.util.Comparator;
23 import java.util.List;
24 import java.util.Collections;
25 import java.util.ArrayList;
26
27
28 /***
29 * A class to hold all parsed attributes of a class.
30 */
31 final class JDOClass
32 {
33 /***
34 * The name of the class.
35 */
36 private final String name;
37
38 /***
39 * The name of the superclass.
40 */
41 private String superClassName = null;
42
43 /***
44 * The name of the oid class.
45 */
46 private String oidClassName = null;
47
48 /***
49 * The access modifier of the class.
50 */
51 private int modifiers = Modifier.PUBLIC;
52
53 /***
54 * The persistence modifier of the class.
55 */
56 private boolean isPersistent = true;
57
58 /***
59 * Flag indicating whether this class is serializable.
60 */
61 private boolean isSerializable = true;
62
63 /***
64 * A list of all parsed fields.
65 */
66 private final List fields = new ArrayList();
67
68 /***
69 * The names of all managed fields this class.
70 */
71 private String[] managedFieldNames = null;
72
73 /***
74 * The names of all fields this class.
75 */
76 private String[] fieldNames = null;
77
78 /***
79 * Constructs a new object with the given name.
80 *
81 * @param name The name of the class.
82 */
83 JDOClass(String name)
84 {
85 this.name = name;
86 }
87
88 /***
89 * Returns the name of the class.
90 *
91 * @return The name of the class.
92 */
93 public String getName()
94 {
95 return name;
96 }
97
98 /***
99 * Returns the modifiers of the class.
100 *
101 * @param modifiers The modifiers of the class.
102 */
103 public void setModifiers(int modifiers)
104 {
105 this.modifiers = modifiers;
106 }
107
108 /***
109 * Returns the modifiers of the class.
110 *
111 * @return The modifiers of the class.
112 */
113 public int getModifiers()
114 {
115 return modifiers;
116 }
117
118 /***
119 * Sets the superclassname. The given classname should have a canonical
120 * form (with dots). It is converted to the CM-similar notation
121 * (with slashes).
122 *
123 * @param classname The superclassname.
124 */
125 public void setSuperClassName(String classname)
126 {
127 superClassName = NameHelper.fromCanonicalClassName(classname);
128 }
129
130 /***
131 * Returns the superclassname.
132 *
133 * @return The superclassname.
134 */
135 public String getSuperClassName()
136 {
137 return superClassName;
138 }
139
140 /***
141 * Sets the oid classname. The given classname should have a canonical
142 * form (with dots). It is converted to the CM-similar notation
143 * (with slashes).
144 *
145 * @param classname The oid classname
146 */
147 public void setOidClassName(String classname)
148 {
149 oidClassName = NameHelper.fromCanonicalClassName(classname);
150 }
151
152 /***
153 * Returns the oid classname.
154 *
155 * @return The oid classname
156 */
157 public String getOidClassName()
158 {
159 return oidClassName;
160 }
161
162 /***
163 * Sets the persistence modifier of the class.
164 *
165 * @param persistent the persistence modifer
166 * @see #isPersistent
167 */
168 public void setPersistent(boolean persistent)
169 {
170 this.isPersistent = persistent;
171 }
172
173 /***
174 * Returns whether the class is persistent.
175 *
176 * @return true if persistent class.
177 * @see #isPersistent
178 */
179 public boolean isPersistent()
180 {
181 return isPersistent;
182 }
183
184 /***
185 * Returns whether the class is transient.
186 *
187 * @return true if transient class.
188 * @see #isPersistent
189 */
190 public boolean isTransient()
191 {
192 return !isPersistent();
193 }
194
195 /***
196 * Returns whether the class is serializable.
197 *
198 * @return true if serializable class.
199 * @see #isSerializable
200 */
201 public boolean isSerializable()
202 {
203 return isSerializable;
204 }
205
206 /***
207 * Sets the serializable flag of the class.
208 *
209 * @param serializable the serializable flag
210 * @see #isSerializable
211 */
212 public void setSerializable(boolean serializable)
213 {
214 this.isSerializable = serializable;
215 }
216 /***
217 * Adds a new field.
218 *
219 * @param field The new field.
220 */
221 public void addField(JDOField field)
222 {
223 fields.add(field);
224 }
225
226 /***
227 * Returns the field with the given name.
228 *
229 * @param name The name of the requested field.
230 * @return The field or <code>null</code> if not found.
231 */
232 public JDOField getField(String name)
233 {
234 int idx = getIndexOfField(name);
235 return (idx > -1 ? (JDOField) fields.get(idx) : null);
236 }
237
238 /***
239 * Returns the index of the field with the given name.
240 *
241 * @param name The name of the field.
242 * @return The index or <code>-1</code> if the field was not found.
243 */
244 public int getIndexOfField(String name)
245 {
246 for (int i = 0; i < fields.size(); i++) {
247 JDOField field = (JDOField)fields.get(i);
248 if (field.getName().equals(name)) {
249 return i;
250 }
251 }
252
253 return -1;
254 }
255
256 /***
257 * Returns all fields of the class.
258 *
259 * @return The fields
260 */
261 public List getFields()
262 {
263 return fields;
264 }
265
266 /***
267 * Returns the names of all fields of the class.
268 *
269 * @return The field names
270 */
271 public String[] getFieldNames()
272 {
273 if (fieldNames == null) {
274 final int n = fields.size();
275 String[] fields = new String[n];
276 for (int i = 0; i < n; i++) {
277 fields[i] = ((JDOField)this.fields.get(i)).getName();
278 }
279 fieldNames = fields;
280 }
281
282 return fieldNames;
283 }
284
285 /***
286 * Sorts the fields of this class according to the names. This method
287 * should be called if all fields are added. It is necessary to
288 * establish an order on the fields.
289 *
290 */
291 final void sortFields()
292 {
293 Collections.sort(
294 fields,
295 new Comparator() {
296 public int compare(Object f1, Object f2)
297 {
298 JDOField field1 = (JDOField)f1;
299 JDOField field2 = (JDOField)f2;
300
301 if (!(field1.isManaged() && field2.isManaged()))
302 {
303 return (field1.isManaged() ? -1 : 1);
304 }
305 return field1.getName().compareTo(field2.getName());
306 }
307 });
308 }
309
310 /***
311 * Returns the names of all managed fields this class.
312 *
313 * @return The persistent fieldnames.
314 */
315 public String[] getManagedFieldNames()
316 {
317 if (managedFieldNames == null) {
318 final int n = fields.size();
319 List tmp = new ArrayList(n);
320 for (int i = 0; i < n; i++) {
321 JDOField field = (JDOField)fields.get(i);
322 if (field.isManaged()) {
323 tmp.add(field.getName());
324 }
325 }
326 managedFieldNames
327 = (String[])tmp.toArray(new String[tmp.size()]);
328 }
329
330 return managedFieldNames;
331 }
332
333 /***
334 * Creates a string-representation for this object.
335 *
336 * @return The string-representation of this object.
337 */
338 public String toString()
339 {
340 return ('<' + MetaDataProperties.PROPERTY_SUPER_CLASSNAME
341 + ':' + superClassName
342 + MetaDataProperties.PROPERTY_OID_CLASSNAME
343 + ':' + oidClassName
344 + ',' + MetaDataProperties.PROPERTY_ACCESS_MODIFIER
345 + ':' + Modifier.toString(modifiers)
346 + ',' + MetaDataProperties.PROPERTY_JDO_MODIFIER
347 + ':' + isPersistent
348 + ',' + "fields:" + fields + '>');
349 }
350 }