KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > api > persistence > enhancer > generator > ImplHelper


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.jdo.api.persistence.enhancer.generator;
25
26 import java.util.List JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.HashMap JavaDoc;
29
30 import com.sun.jdo.api.persistence.enhancer.meta.ExtendedJDOMetaData;
31 import com.sun.jdo.api.persistence.enhancer.util.Assertion;
32 import com.sun.jdo.spi.persistence.utility.JavaTypeHelper;
33 import com.sun.jdo.spi.persistence.utility.generator.JavaClassWriterHelper;
34
35 /**
36  *
37  */

38 final class ImplHelper extends Assertion
39 {
40     // string constants
41
static final String JavaDoc[] COMMENT_ENHANCER_ADDED
42     = null; //{ "added by enhancer" };
43
static final String JavaDoc[] COMMENT_NOT_ENHANCER_ADDED
44     = null; //{ "not added by enhancer" };
45

46     static final String JavaDoc CLASSNAME_JDO_PERSISTENCE_CAPABLE
47     = "com.sun.jdo.spi.persistence.support.sqlstore.PersistenceCapable";
48     static final String JavaDoc CLASSNAME_JDO_PERSISTENCE_MANAGER
49     = "com.sun.jdo.api.persistence.support.PersistenceManager";
50     static final String JavaDoc CLASSNAME_JDO_STATE_MANAGER
51     = "com.sun.jdo.spi.persistence.support.sqlstore.StateManager";
52     static final String JavaDoc CLASSNAME_JDO_SCO
53     = "com.sun.jdo.spi.persistence.support.sqlstore.SCO";
54     static final String JavaDoc CLASSNAME_JDO_FATAL_EXCEPTION
55     = "com.sun.jdo.api.persistence.support.JDOFatalException";
56
57     static final String JavaDoc FIELDNAME_JDO_FLAGS
58     = "jdoFlags";
59     static final String JavaDoc FIELDNAME_JDO_STATE_MANAGER
60     = "jdoStateManager";
61     static final String JavaDoc FIELDNAME_JDO_INHERITED_FIELD_COUNT
62     = "jdoInheritedFieldCount";
63     static final String JavaDoc FIELDNAME_JDO_FIELD_NAMES
64     = "jdoFieldNames";
65     static final String JavaDoc FIELDNAME_JDO_FIELD_TYPES
66     = "jdoFieldTypes";
67     static final String JavaDoc FIELDNAME_JDO_FIELD_FLAGS
68     = "jdoFieldFlags";
69     static final String JavaDoc METHODNAME_JDO_NEW_INSTANCE
70     = "jdoNewInstance";
71     static final String JavaDoc METHODNAME_JDO_SET_FIELD
72     = "jdoSetField";
73     static final String JavaDoc METHODNAME_JDO_GET_FIELD
74     = "jdoGetField";
75     static final String JavaDoc METHODNAME_JDO_GET_STATE_MANAGER
76     = "jdoGetStateManager";
77     static final String JavaDoc METHODNAME_JDO_SET_STATE_MANAGER
78     = "jdoSetStateManager";
79     static final String JavaDoc METHODNAME_JDO_GET_FLAGS
80     = "jdoGetFlags";
81     static final String JavaDoc METHODNAME_JDO_SET_FLAGS
82     = "jdoSetFlags";
83     static final String JavaDoc METHODNAME_JDO_GET_PERSISTENCE_MANAGER
84     = "jdoGetPersistenceManager";
85     static final String JavaDoc METHODNAME_JDO_CLEAR
86     = "jdoClear";
87     static final String JavaDoc METHODNAME_JDO_MAKE_DIRTY
88     = "jdoMakeDirty";
89     static final String JavaDoc METHODNAME_JDO_GET_OBJECT_ID
90     = "jdoGetObjectId";
91     static final String JavaDoc METHODNAME_JDO_IS_PERSISTENT
92     = "jdoIsPersistent";
93     static final String JavaDoc METHODNAME_JDO_IS_TRANSACTIONAL
94     = "jdoIsTransactional";
95     static final String JavaDoc METHODNAME_JDO_IS_NEW
96     = "jdoIsNew";
97     static final String JavaDoc METHODNAME_JDO_IS_DIRTY
98     = "jdoIsDirty";
99     static final String JavaDoc METHODNAME_JDO_IS_DELETED
100     = "jdoIsDeleted";
101
102     static private final HashMap JavaDoc typeNameConversion = new HashMap JavaDoc();
103     static
104     {
105         typeNameConversion.put(int.class.getName(), "Int");
106         typeNameConversion.put(long.class.getName(), "Long");
107         typeNameConversion.put(byte.class.getName(), "Byte");
108         typeNameConversion.put(char.class.getName(), "Char");
109         typeNameConversion.put(boolean.class.getName(), "Boolean");
110         typeNameConversion.put(short.class.getName(), "Short");
111         typeNameConversion.put(float.class.getName(), "Float");
112         typeNameConversion.put(double.class.getName(), "Double");
113         typeNameConversion.put("String", "String");
114     }
115
116     static private boolean isPrimitiveClass(String JavaDoc className)
117     {
118         affirm(!className.equals("void")); // NOI18N
119

120         return JavaTypeHelper.getWrapperName(className) != null;
121     }
122
123     static private String JavaDoc getConvertedTypeName(String JavaDoc fieldType)
124     {
125         final String JavaDoc name = (String JavaDoc)typeNameConversion.get(fieldType);
126         return (name != null ? name : JavaClassWriterHelper.Object_);
127     }
128
129     static private String JavaDoc getMethodNameGetField(String JavaDoc fieldType)
130     {
131         return JavaClassWriterHelper.get_ +
132                getConvertedTypeName(fieldType) + "Field";
133     }
134
135     static private String JavaDoc getMethodNameSetField(String JavaDoc fieldType)
136     {
137         return JavaClassWriterHelper.set_ +
138                getConvertedTypeName(fieldType) + "Field";
139     }
140
141
142     // Create bodies of methods.
143

144     static String JavaDoc[] getJDOManagedFieldCountImpl(int fieldcount)
145     {
146         return new String JavaDoc[] {
147             FIELDNAME_JDO_INHERITED_FIELD_COUNT + " + " + fieldcount + JavaClassWriterHelper.delim_
148         };
149     }
150
151     static String JavaDoc[] getDefaultConstructorImpl()
152     {
153         return JavaClassWriterHelper.super_;
154     }
155
156     static String JavaDoc[] getCloneImpl(String JavaDoc className)
157     {
158         className = normalizeClassName(className);
159         String JavaDoc[] bodies = new String JavaDoc[4];
160         int i = 0;
161         bodies[i++] = (new StringBuffer JavaDoc(className)).append(" clone = (")
162                  .append(className).append(")super.clone();").toString();
163         bodies[i++] = "clone.jdoFlags = (byte)0;";
164         bodies[i++] = "clone.jdoStateManager = null;";
165         bodies[i++] = "return clone;";
166         return bodies;
167     }
168
169     static String JavaDoc[] getJDOConstructorSMImpl(String JavaDoc statemanager)
170     {
171         String JavaDoc[] bodies = new String JavaDoc[2];
172         int i = 0;
173         bodies[i++] = FIELDNAME_JDO_FLAGS + " = (byte)1; // == LOAD_REQUIRED";
174         bodies[i++] = "this." + FIELDNAME_JDO_STATE_MANAGER
175                  + " = " + statemanager + JavaClassWriterHelper.delim_;
176         return bodies;
177     }
178
179     static String JavaDoc[] getJDONewInstanceImpl(String JavaDoc className,
180                                       String JavaDoc statemanager)
181     {
182         className = getClassName(className);
183         return new String JavaDoc[] { (new StringBuffer JavaDoc("return new "))
184                   .append(className).append("(").append(statemanager)
185                   .append(");").toString() };
186     }
187
188     static String JavaDoc[] getFieldDirectReadImpl(String JavaDoc fieldName,
189                                        String JavaDoc fieldType,
190                                        int fieldNumber)
191     {
192         fieldType = normalizeClassName(fieldType);
193         return new String JavaDoc[] {
194             "// annotation: grant direct read access",
195             "return " + fieldName + JavaClassWriterHelper.delim_
196         };
197     }
198
199     static String JavaDoc[] getFieldMediateReadImpl(String JavaDoc fieldName,
200                                         String JavaDoc fieldType,
201                                         int fieldNumber)
202     {
203         fieldType = normalizeClassName(fieldType);
204         String JavaDoc[] bodies = new String JavaDoc[6];
205         int i = 0;
206         bodies[i++] = "// annotation: mediate read access";
207         bodies[i++] = (new StringBuffer JavaDoc("final "))
208                  .append(CLASSNAME_JDO_STATE_MANAGER)
209                  .append(" stateManager = this.")
210                  .append(FIELDNAME_JDO_STATE_MANAGER)
211                  .append(JavaClassWriterHelper.delim_).toString();
212         bodies[i++] = "if (stateManager != null) {";
213         bodies[i++] = (new StringBuffer JavaDoc(" "))
214                 .append("stateManager.prepareGetField(").append(fieldNumber)
215                 .append(");").toString();
216         bodies[i++] = "}";
217         bodies[i++] = "return " + fieldName + JavaClassWriterHelper.delim_;
218         return bodies;
219     }
220
221     static String JavaDoc[] getFieldCheckReadImpl(String JavaDoc fieldName,
222                                       String JavaDoc fieldType,
223                                       int fieldNumber)
224     {
225         String JavaDoc[] bodies = new String JavaDoc[5];
226         int i = 0;
227         fieldType = normalizeClassName(fieldType);
228         bodies[i++] = "// annotation: check read access";
229         bodies[i++] = (new StringBuffer JavaDoc("if ("))
230                  .append(FIELDNAME_JDO_FLAGS).append(" > 0) {").toString();
231         bodies[i++] = " " + FIELDNAME_JDO_STATE_MANAGER + ".loadForRead();";
232         bodies[i++] = "}";
233         bodies[i++] = "return " + fieldName + JavaClassWriterHelper.delim_;
234         return bodies;
235     }
236
237     static String JavaDoc[] getFieldDirectWriteImpl(String JavaDoc fieldName,
238                                            String JavaDoc fieldType,
239                                            int fieldNumber,
240                                            String JavaDoc newvalue)
241     {
242         fieldType = normalizeClassName(fieldType);
243         return new String JavaDoc[] {
244             "// annotation: grant direct write access",
245             (new StringBuffer JavaDoc("this."))
246                  .append(fieldName).append(" = ").append(newvalue)
247                  .append(JavaClassWriterHelper.delim_).toString()
248         };
249     }
250
251     static String JavaDoc[] getFieldMediateWriteImpl(String JavaDoc fieldName,
252                                          String JavaDoc fieldType,
253                                          int fieldNumber,
254                                          String JavaDoc newvalue)
255     {
256         String JavaDoc[] bodies = new String JavaDoc[7];
257         int i = 0;
258         fieldType = normalizeClassName(fieldType);
259         bodies[i++] = "// annotation: mediate write access";
260         bodies[i++] = (new StringBuffer JavaDoc("final "))
261                  .append(CLASSNAME_JDO_STATE_MANAGER)
262                  .append(" stateManager = this.")
263                  .append(FIELDNAME_JDO_STATE_MANAGER)
264                  .append(JavaClassWriterHelper.delim_).toString();
265         bodies[i++] = "if (stateManager == null) {";
266         bodies[i++] = (new StringBuffer JavaDoc(" this."))
267                  .append(fieldName).append(" = ")
268                  .append(newvalue).append(JavaClassWriterHelper.delim_)
269                  .toString();
270         bodies[i++] = "} else {";
271         bodies[i++] = (new StringBuffer JavaDoc(" stateManager."))
272                  .append(getMethodNameSetField(fieldType)).append('(')
273                  .append(fieldNumber).append(", ").append(newvalue)
274                  .append(");").toString();
275         bodies[i++] = "}";
276         return bodies;
277     }
278
279     static String JavaDoc[] getFieldCheckWriteImpl(String JavaDoc fieldName,
280                                        String JavaDoc fieldType,
281                                        int fieldNumber,
282                                        String JavaDoc newvalue)
283     {
284         String JavaDoc[] bodies = new String JavaDoc[5];
285         int i = 0;
286         fieldType = normalizeClassName(fieldType);
287         bodies[i++] = "// annotation: check write access";
288         bodies[i++] = "if (" + FIELDNAME_JDO_FLAGS + " != 0) {";
289         bodies[i++] = " " + FIELDNAME_JDO_STATE_MANAGER + ".loadForUpdate();";
290         bodies[i++] = "}";
291         bodies[i++] = (new StringBuffer JavaDoc("this.")).append(fieldName)
292                  .append(" = ").append(newvalue)
293                  .append(JavaClassWriterHelper.delim_).toString();
294         return bodies;
295     }
296
297     static String JavaDoc[] getJDOClearImpl(String JavaDoc className, ExtendedJDOMetaData meta,
298             String JavaDoc[] fieldNames, String JavaDoc[] fieldTypes) {
299         final List JavaDoc impl = new ArrayList JavaDoc(20);
300         for (int i = 0; i < fieldNames.length; i++) {
301             String JavaDoc fieldTypeClassPath = fieldTypes[i];
302             String JavaDoc fieldType = normalizeClassName(fieldTypes[i]);
303             String JavaDoc fieldName = fieldNames[i];
304             if (meta.isKeyField(className, fieldName)) {
305                 continue;
306             }
307             String JavaDoc primClass = JavaTypeHelper.getWrapperName(fieldType);
308             if (meta.isMutableSecondClassObjectType(fieldTypeClassPath)) {
309                 impl.add((new StringBuffer JavaDoc("if ("))
310                         .append(fieldName).append(" instanceof ")
311                         .append(CLASSNAME_JDO_SCO).append(") {")
312                         .toString());
313                 impl.add((new StringBuffer JavaDoc(" (("))
314                         .append(CLASSNAME_JDO_SCO).append(")")
315                         .append(fieldName).append(").unsetOwner();")
316                         .toString());
317                 impl.add("}");
318                 impl.add(fieldName + " = null;");
319             } else if (primClass == null) {
320                 impl.add(fieldName + " = null;");
321             } else if (JavaClassWriterHelper.boolean_.equals(fieldType)) {
322                 impl.add(fieldName + " = false;");
323             } else {
324                 impl.add(fieldName + " = 0;");
325             }
326         }
327         String JavaDoc[] strArr = new String JavaDoc[impl.size()];
328         return (String JavaDoc[])impl.toArray(strArr);
329     }
330
331     static String JavaDoc[] getJDOGetFieldImpl(String JavaDoc fieldNumber,
332                                    String JavaDoc[] fieldNames,
333                                    String JavaDoc[] fieldTypes)
334     {
335         final List JavaDoc impl = new ArrayList JavaDoc(20);
336         impl.add("switch (" + fieldNumber + ") {");
337         for (int i = 0; i < fieldNames.length; i++) {
338             String JavaDoc fieldType = normalizeClassName(fieldTypes[i]);
339             impl.add("case " + i + ':');
340             String JavaDoc primClass = JavaTypeHelper.getWrapperName(fieldType);
341             if (primClass == null) {
342                 impl.add(" return " + fieldNames[i] + ";");
343             } else {
344                 impl.add((new StringBuffer JavaDoc(" return new "))
345                           .append(primClass).append("(")
346                           .append(fieldNames[i]).append(");").toString());
347             }
348         }
349         impl.add("default:");
350         impl.add(" throw new " + CLASSNAME_JDO_FATAL_EXCEPTION + "();");
351         impl.add("}");
352         String JavaDoc[] strArr = new String JavaDoc[impl.size()];
353         return (String JavaDoc[])impl.toArray(strArr);
354     }
355
356     static String JavaDoc[] getJDOSetFieldImpl(String JavaDoc fieldNumber, String JavaDoc objName,
357                                        String JavaDoc[] fieldNames,
358                                        String JavaDoc[] fieldTypes)
359     {
360         final List JavaDoc impl = new ArrayList JavaDoc(20);
361         impl.add("switch (" + fieldNumber + ") {");
362         for (int i = 0; i < fieldNames.length; i++) {
363             String JavaDoc fieldType = normalizeClassName(fieldTypes[i]);
364             impl.add("case " + i + ':');
365             String JavaDoc primClass = JavaTypeHelper.getWrapperName(fieldType);
366             if (primClass == null) {
367                 impl.add((new StringBuffer JavaDoc(" this."))
368                           .append(fieldNames[i])
369                           .append(" = (").append(fieldType)
370                           .append(")").append(objName)
371                           .append(";").toString());
372             } else {
373                 impl.add((new StringBuffer JavaDoc(" this."))
374                           .append(fieldNames[i])
375                           .append(" = ((").append(primClass)
376                           .append(")").append(objName).append(").")
377                           .append(((String JavaDoc)typeNameConversion.get(fieldType)).toLowerCase())
378                           .append("Value();").toString());
379             }
380             impl.add(" return;");
381         }
382         impl.add("default:");
383         impl.add(" throw new " + CLASSNAME_JDO_FATAL_EXCEPTION + "();");
384         impl.add("}");
385         String JavaDoc[] strArr = new String JavaDoc[impl.size()];
386         return (String JavaDoc[])impl.toArray(strArr);
387     }
388
389     // returnType = null means void
390
private static String JavaDoc[] getJDOStateManagerDelegationImpl(
391             String JavaDoc delegation, String JavaDoc returnType) {
392         final List JavaDoc impl = new ArrayList JavaDoc(7);
393         impl.add((new StringBuffer JavaDoc("final "))
394                  .append(CLASSNAME_JDO_STATE_MANAGER)
395                  .append(" stateManager = this.")
396                  .append(FIELDNAME_JDO_STATE_MANAGER)
397                  .append(JavaClassWriterHelper.delim_)
398                  .toString());
399         impl.add("if (stateManager != null) {");
400         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(" ");
401         if (returnType != null) {
402             buf.append("return ");
403         }
404         impl.add(((buf.append("stateManager."))
405                   .append(delegation).append(";")).toString());
406         impl.add("}");
407         if (returnType != null) {
408             impl.add((new StringBuffer JavaDoc("return ")).append(returnType)
409                         .append(";").toString());
410         };
411         String JavaDoc[] strArr = new String JavaDoc[impl.size()];
412         return (String JavaDoc[])impl.toArray(strArr);
413     }
414
415     static String JavaDoc[] getJDOStateManagerVoidDelegationImpl(String JavaDoc delegation)
416     {
417         return getJDOStateManagerDelegationImpl(delegation, null);
418     }
419
420     static String JavaDoc[] getJDOStateManagerObjectDelegationImpl(String JavaDoc delegation)
421     {
422         return getJDOStateManagerDelegationImpl(delegation,
423                 JavaClassWriterHelper.null_);
424     }
425
426     static String JavaDoc[] getJDOStateManagerBooleanDelegationImpl(String JavaDoc delegation)
427     {
428         return getJDOStateManagerDelegationImpl(delegation,
429                 JavaClassWriterHelper.false_);
430     }
431
432     static String JavaDoc[] getOidHashCodeImpl(String JavaDoc[] pknames,
433                                    String JavaDoc[] pktypes,
434                                    boolean isRoot)
435     {
436         final List JavaDoc impl = new ArrayList JavaDoc(3);
437         if (isRoot) {
438             impl.add("int hash = 0;");
439         } else {
440             impl.add("int hash = super.hashCode();");
441         }
442         for (int i = 0; i < pknames.length; i++) {
443             if (isPrimitiveClass(pktypes[i])) {
444                 if (pktypes[i].equals("boolean")) {
445                     impl.add("hash += (" + pknames[i] + " ? 1 : 0);");
446                 } else {
447                     impl.add("hash += (int)" + pknames[i] +
448                             JavaClassWriterHelper.delim_);
449                 }
450             } else {
451                 impl.add("hash += (this." + pknames[i]
452                          + " != null ? this." + pknames[i]
453                          + ".hashCode() : 0);");
454             }
455         }
456         impl.add("return hash;");
457         String JavaDoc[] strArr = new String JavaDoc[impl.size()];
458         return (String JavaDoc[])impl.toArray(strArr);
459     }
460
461     static String JavaDoc[] getOidEqualsImpl(String JavaDoc oidClassName,
462                                  String JavaDoc[] pknames,
463                                  String JavaDoc[] pktypes,
464                                  String JavaDoc pk,
465                                  boolean isRoot)
466     {
467         final List JavaDoc impl = new ArrayList JavaDoc(31);
468         if (isRoot) {
469             impl.add("if (" + pk + " == null || !this.getClass().equals("
470                      + pk + ".getClass())) {");
471         } else {
472             impl.add("if (!super.equals(" + pk + ")) {");
473         }
474         impl.add(" return false;");
475         impl.add("}");
476         oidClassName = getClassName(oidClassName);
477         impl.add(oidClassName + " oid = (" + oidClassName + ")" + pk +
478                 JavaClassWriterHelper.delim_);
479         for (int i = 0; i < pknames.length; i++) {
480             if (isPrimitiveClass(pktypes[i])) {
481                 impl.add("if (this." + pknames[i] + " != oid."
482                          + pknames[i] + ") return false;");
483             } else {
484                 impl.add("if (this." + pknames[i] + " != oid."
485                          + pknames[i] + " && (this." + pknames[i]
486                          + " == null || " + "!this." + pknames[i]
487                          + ".equals(oid." + pknames[i]
488                          + "))) return false;");
489             }
490         }
491         impl.add("return true;");
492         String JavaDoc[] strArr = new String JavaDoc[impl.size()];
493         return (String JavaDoc[])impl.toArray(strArr);
494     }
495
496
497     //The following three methods are originated from NameHelper.
498
/**
499      * The input parameter can be a classPath with "/" or a valid
500      * full className with package name.
501      */

502     static String JavaDoc normalizeClassName(String JavaDoc className)
503     {
504         if (className == null) {
505             return null;
506         }
507         return className.replace('/', '.').replace('$', '.');
508     }
509
510     /**
511      * The inner class must be represented by $ in className.
512      * The input className can be a classPath with "/".
513      */

514     static String JavaDoc getPackageName(String JavaDoc className)
515     {
516         if (className == null) {
517             return null;
518         }
519         return JavaTypeHelper.getPackageName(className.replace('/', '.'));
520     }
521
522     /**
523      * The input parameter can be a classPath with "/" or a valid
524      * full className with package name.
525      */

526     static String JavaDoc getClassName(String JavaDoc className) {
527         return JavaTypeHelper.getShortClassName(normalizeClassName(className));
528     }
529 }
530
Popular Tags