KickJava   Java API By Example, From Geeks To Geeks.

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


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.lang.reflect.Modifier JavaDoc;
27
28 import java.util.Iterator JavaDoc;
29 import java.util.Collection JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.HashMap JavaDoc;
33 import java.util.Properties JavaDoc;
34
35 import java.io.Serializable JavaDoc;
36 import java.io.File JavaDoc;
37 import java.io.Writer JavaDoc;
38 import java.io.PrintWriter JavaDoc;
39 import java.io.FileWriter JavaDoc;
40 import java.io.BufferedWriter JavaDoc;
41 import java.io.InputStream JavaDoc;
42 import java.io.BufferedInputStream JavaDoc;
43 import java.io.FileInputStream JavaDoc;
44 import java.io.ObjectOutputStream JavaDoc;
45 import java.io.IOException JavaDoc;
46 import java.io.FileNotFoundException JavaDoc;
47
48 import com.sun.jdo.api.persistence.enhancer.util.Assertion;
49
50 import com.sun.jdo.api.persistence.enhancer.meta.ExtendedJDOMetaData;
51 import com.sun.jdo.api.persistence.enhancer.meta.JDOMetaDataPropertyImpl;
52
53 import com.sun.jdo.spi.persistence.utility.generator.JavaFileWriter;
54 import com.sun.jdo.spi.persistence.utility.generator.JavaClassWriter;
55 import com.sun.jdo.spi.persistence.utility.generator.JavaClassWriterHelper;
56 import com.sun.jdo.spi.persistence.utility.generator.io.IOJavaFileWriter;
57 import com.sun.jdo.spi.persistence.utility.generator.io.IOJavaClassWriter;
58 import com.sun.jdo.spi.persistence.utility.logging.Logger;
59 import com.sun.jdo.api.persistence.enhancer.LogHelperEnhancer;
60 import java.util.ResourceBundle JavaDoc;
61 import com.sun.jdo.spi.persistence.utility.I18NHelper;
62
63
64 /**
65  *
66  */

67 public final class Main
68     extends Assertion
69 {
70     //
71
// CAUTION: THE ENHANCER-GENERATOR DEALS WITH CLASS NAMES IN THE
72
// JVM FORMAT, THAT IS, '/' INSTEAD OF '.' IS USED AS SEPARATOR.
73
//
74
// Class names in the Java language format ('.' as separator) are
75
// referred to as "normalized" in the generator code.
76
//
77
// File names are named as such; they use File.separatorChar as
78
// separator.
79
//
80

81      /** The logger */
82     private static final Logger logger = LogHelperEnhancer.getLogger();
83     private static final String JavaDoc dotLine =
84         "----------------------------------------------------------------------";
85     /**
86      * The stream to write messages to.
87      */

88     private final PrintWriter JavaDoc out = new PrintWriter JavaDoc(System.out, true);
89
90     /**
91      * The stream to write error messages to.
92      */

93     private final PrintWriter JavaDoc err = new PrintWriter JavaDoc(System.err, true);
94
95     /**
96      * The command line options.
97      */

98     private final CmdLineOptions opts = new CmdLineOptions();
99
100     /**
101      * Java Writer Class
102      */

103     private JavaFileWriter fWriter = null;
104     private JavaClassWriter writer = null;
105
106     /**
107      * The MetaData for generating classes.
108      */

109     private ExtendedJDOMetaData meta = null;
110
111     private File JavaDoc destinationDir = null;
112
113
114     /**
115      * I18N message handler
116      */

117     private final static ResourceBundle JavaDoc messages = I18NHelper.loadBundle("com.sun.jdo.api.persistence.enhancer.Bundle"); // NOI18N
118
public Main()
119     {}
120
121     public Main(ExtendedJDOMetaData meta, File JavaDoc destinationDir)
122              throws IOException JavaDoc {
123         this.meta = meta;
124         this.destinationDir = destinationDir;
125         createDestinationDir();
126     }
127
128     /**
129      *
130      */

131     public static final void main(String JavaDoc[] argv)
132     {
133         final Main gen = new Main();
134         try {
135             gen.opts.processArgs(argv);
136             gen.init();
137             gen.generate();
138         } catch(Exception JavaDoc ex) {
139             gen.printError(null, ex);
140         }
141     }
142
143     /**
144      * A class for holding the command line options.
145      */

146     private class CmdLineOptions
147     {
148         // final Collection inputFileNames = new ArrayList();
149
String JavaDoc destinationDirectory = null;
150         String JavaDoc jdoXMLModelFileName = null;
151         String JavaDoc jdoPropertiesFileName = null;
152         boolean verbose = false;
153
154         /**
155          * Print a usage message to System.err
156          */

157         public void usage() {
158             err.println("Usage: Main <options> <arguments>...");
159             err.println("Options:");
160             err.println(" -v, --verbose print verbose output");
161             err.println(" -d, --dest <dir> destination directory for output files");
162             err.println(" -p, --properties <file> use property file for meta data");
163             err.println();
164             err.println("Arguments:");
165             err.println();
166             err.println("Returns a non-zero value in case of errors.");
167             System.exit(1);
168         }
169
170         /**
171          * Process command line options
172          */

173         protected int processArgs(String JavaDoc[] argv)
174         {
175             for (int i = 0; i < argv.length; i++) {
176                 final String JavaDoc arg = argv[i];
177                 if (arg.equals("-v")
178                     || arg.equals("--verbose")) {
179                     verbose = true;
180                     continue;
181                 }
182                 if (arg.equals("-d")
183                     || arg.equals("--dest")) {
184                     if (argv.length - i < 2) {
185                         printError("Missing argument to the -d/-dest option", null);
186                         usage();
187                     }
188                     destinationDirectory = argv[++i];
189                     continue;
190                 }
191                 if (arg.equals("-p") ||
192                     arg.equals("--properties")) {
193                     if (argv.length - i < 2) {
194                         printError("Missing argument to the -p/--properties option", null);
195                         usage();
196                     }
197                     jdoPropertiesFileName = argv[++i];
198                     continue;
199                 }
200                 if (arg.length() > 0 && arg.charAt(0) == '-') {
201                     printError("Unrecognized option:" + arg, null);
202                     usage();
203                 }
204                 if (arg.length() == 0) {
205                     printMessage("Ignoring empty command line argument.");
206                     continue;
207                 }
208
209                 //inputFileNames.add(arg);
210
}
211
212             // The user must specify a destination directory
213
if (jdoPropertiesFileName == null) {
214                 printError("No destination directory specified", null);
215                 usage();
216             }
217
218             // The user must specify a destination directory
219
if (destinationDirectory == null) {
220                 printError("No destination directory specified", null);
221                 usage();
222             }
223
224             return 0;
225         }
226     }
227
228     private void init()
229         throws FileNotFoundException JavaDoc, IOException JavaDoc
230     {
231         // load the properties
232
affirm(opts.jdoPropertiesFileName != null);
233         FileInputStream JavaDoc finput = null;
234         try {
235             final File JavaDoc f = new File JavaDoc(opts.jdoPropertiesFileName);
236             finput = new FileInputStream JavaDoc(f);
237             final Properties JavaDoc props = new Properties JavaDoc();
238             props.load(finput);
239             meta = new JDOMetaDataPropertyImpl(props, out);
240         } finally {
241             if (finput != null) {
242                 try {
243                     finput.close();
244                 } catch(Exception JavaDoc ex) {
245                     printError(ex.getMessage(), ex);
246                 }
247             }
248         }
249
250         affirm(opts.destinationDirectory != null);
251         destinationDir = new File JavaDoc(opts.destinationDirectory);
252         createDestinationDir();
253     }
254
255     private void createDestinationDir()
256         throws IOException JavaDoc
257     {
258         // create the destination directory
259
if (!destinationDir.exists() && !destinationDir.mkdirs()) {
260             throw new IOException JavaDoc(I18NHelper.getMessage(messages,"EXC_DestDirCreateFailure",destinationDir)); //NOI18N
261
}
262     }
263
264     private void generate()
265         throws IOException JavaDoc
266     {
267         final String JavaDoc[] classes = meta.getKnownClasses();
268         for (int i = 0; i < classes.length; i++) {
269             final String JavaDoc className = classes[i];
270             generate(className);
271         }
272     }
273
274     // entry point for EJB TP class generation
275
// The argument is a fully qualified class name expected in the
276
// JVM format, that is, with '/' for '.' as separator. See comment
277
// at the beginning of this class.
278
public File JavaDoc generate(final String JavaDoc className)
279         throws IOException JavaDoc
280     {
281         affirm(className != null);
282         printMessage("generating '" + className + "'...");
283
284         //@olsen, 4653156: fixed file name
285
final String JavaDoc filePath = className.replace('/', File.separatorChar);
286         final String JavaDoc classFileName
287             = filePath + JavaClassWriterHelper.javaExtension_;
288         final File JavaDoc file = new File JavaDoc(destinationDir, classFileName);
289
290         //@olsen: not needed: IOJavaFileWriter takes care of creating file
291
// create the destination directory
292
//final File dir = file.getAbsoluteFile().getParentFile();
293
//if (!dir.exists() && !dir.mkdirs()) {
294
// throw new IOException("unable to create destination directory: "
295
// + "'" + destinationDir + "'");
296
//}
297

298         fWriter = new IOJavaFileWriter(file);
299         writer = new IOJavaClassWriter();
300         generateClass(className);
301         fWriter.addClass(writer);
302         printMessage("DONE generating '" + className + "'...");
303
304         //@olsen: moved from finally{} to main block
305
// by JavaFileWriter, no stale resources remain allocated ever
306
fWriter.save();
307         return file;
308     }
309
310     private void generateClass(final String JavaDoc className)
311         throws IOException JavaDoc
312     {
313         affirm(className != null);
314
315         final String JavaDoc packageName = ImplHelper.getPackageName(className);
316         fWriter.setPackage(packageName, null);
317
318         // write the class header and key class
319
final String JavaDoc oidClassName = meta.getKeyClass(className);
320         if (oidClassName == null) {
321             writeClassHeader(className);
322         } else {
323             final String JavaDoc oidPackageName
324                 = ImplHelper.getPackageName(oidClassName);
325             affirm(packageName.equals(oidPackageName),
326                    "PC class and key class must be in same package.");
327
328             final boolean enclosedOid
329                 = oidClassName.startsWith(className + "$");
330             if (enclosedOid) {
331                 writeClassHeader(className);
332                 writeOidClass(className, ImplHelper.getClassName(oidClassName),
333                     enclosedOid);
334             } else {
335                 writeOidClass(className, ImplHelper.getClassName(oidClassName),
336                     enclosedOid);
337                 writeClassHeader(className);
338             }
339         }
340
341         writeClassMembers(className);
342
343         // write the augmentation
344
final boolean isPC = meta.isPersistenceCapableClass(className);
345         if (isPC) {
346             final boolean isPCRoot
347                 = meta.isPersistenceCapableRootClass(className);
348             if (isPCRoot) {
349                 writePCRootMembers(className);
350             }
351             writePCMembers(className);
352         }
353     }
354
355     private void writeClassHeader(final String JavaDoc className)
356         throws IOException JavaDoc
357     {
358         final boolean isPCRoot = meta.isPersistenceCapableRootClass(className);
359         final String JavaDoc superclass =
360             ImplHelper.normalizeClassName(meta.getSuperClass(className));
361
362         final String JavaDoc[] comments = null;
363         final String JavaDoc[] interfaces
364             = (isPCRoot
365                ? new String JavaDoc[]{ ImplHelper.CLASSNAME_JDO_PERSISTENCE_CAPABLE, "Cloneable" }
366                : null);
367         writer.setClassDeclaration(meta.getClassModifiers(className),
368                 ImplHelper.getClassName(className), comments);
369         writer.setSuperclass(superclass);
370         if (interfaces != null) {
371             for (int i = 0; i < interfaces.length; i++) {
372                 writer.addInterface(interfaces[i]);
373             }
374         }
375     }
376
377     private void writeClassMembers(final String JavaDoc className)
378         throws IOException JavaDoc
379     {
380         final String JavaDoc[] comments = new String JavaDoc[]{
381             dotLine,
382             "Class Members:",
383             dotLine
384         };
385
386         // write default constructor
387
writer.addConstructor(ImplHelper.getClassName(className),
388             Modifier.PUBLIC, null, null, null,
389             ImplHelper.getDefaultConstructorImpl(),
390             comments);
391
392         final String JavaDoc[] fieldNames = meta.getKnownFields(className);
393         final int n = (fieldNames != null ? fieldNames.length : 0);
394
395         // write method clone() for enhancer testing purpose
396
final ArrayList JavaDoc list = new ArrayList JavaDoc();
397         for (int i = 0; i < n; i++) {
398             final String JavaDoc fieldName = (String JavaDoc)fieldNames[i];
399             final int access = meta.getFieldModifiers(className, fieldName);
400             if ((access & Modifier.STATIC) == 0) {
401                 list.add(fieldName);
402             }
403         }
404
405         writer.addMethod(
406             "clone",
407             Modifier.PUBLIC,
408             JavaClassWriterHelper.Object_,
409             null,
410             null,
411             new String JavaDoc[]{ "java.lang.CloneNotSupportedException" },
412             ImplHelper.getCloneImpl(className),
413             ImplHelper.COMMENT_NOT_ENHANCER_ADDED);
414
415         // write the fields and with their bean getters/setters
416
for (int i = 0; i < n; i++) {
417             final String JavaDoc fieldName = (String JavaDoc)fieldNames[i];
418             writeFieldMember(className, fieldName);
419         }
420     }
421
422     private void writeFieldMember(final String JavaDoc className,
423                                   final String JavaDoc fieldName)
424         throws IOException JavaDoc
425     {
426         final String JavaDoc fieldType = meta.getFieldType(className, fieldName);
427         final String JavaDoc normalizedFieldType =
428             ImplHelper.normalizeClassName(fieldType);
429         final int fieldNumber = meta.getFieldNo(className, fieldName);
430         final int flags = meta.getFieldFlags(className, fieldName);
431
432         final int access = meta.getFieldModifiers(className, fieldName);
433
434         // the field
435
writer.addField(
436             fieldName,
437             access,
438             normalizedFieldType,
439             null, null);
440
441         // do not write bean getters and setters for static fields
442
if ((access & Modifier.STATIC) != 0) {
443             return;
444         }
445
446         // accessor
447
{
448             affirm(((flags & meta.CHECK_READ) == 0)
449                    | (flags & meta.MEDIATE_READ) == 0);
450             final String JavaDoc[] impl;
451             if ((flags & meta.CHECK_READ) != 0) {
452                 impl = ImplHelper.getFieldCheckReadImpl(fieldName,
453                                                         fieldType,
454                                                         fieldNumber);
455             } else if ((flags & meta.MEDIATE_READ) != 0) {
456                 impl = ImplHelper.getFieldMediateReadImpl(fieldName,
457                                                           fieldType,
458                                                           fieldNumber);
459             } else {
460                 impl = ImplHelper.getFieldDirectReadImpl(fieldName,
461                                                          fieldType,
462                                                          fieldNumber);
463             }
464             writer.addMethod(
465                 createMethodName(JavaClassWriterHelper.get_, fieldName),
466                 Modifier.PUBLIC,
467                 normalizedFieldType,
468                 null, null, null,
469                 impl,
470                 ImplHelper.COMMENT_ENHANCER_ADDED);
471         }
472
473         // mutator
474
{
475             affirm(((flags & meta.CHECK_WRITE) == 0)
476                    | (flags & meta.MEDIATE_WRITE) == 0);
477             final String JavaDoc[] impl;
478             if ((flags & meta.CHECK_WRITE) != 0) {
479                 impl = ImplHelper.getFieldCheckWriteImpl(fieldName,
480                                                          fieldType,
481                                                          fieldNumber,
482                                                          fieldName);
483             } else if ((flags & meta.MEDIATE_WRITE) != 0
484                     && !meta.isKnownNonManagedField(className, fieldName,
485                     null)) {
486                 impl = ImplHelper.getFieldMediateWriteImpl(fieldName,
487                                                            fieldType,
488                                                            fieldNumber,
489                                                            fieldName);
490             } else {
491                 impl = ImplHelper.getFieldDirectWriteImpl(fieldName,
492                                                           fieldType,
493                                                           fieldNumber,
494                                                           fieldName);
495             }
496             writer.addMethod(
497                 createMethodName(JavaClassWriterHelper.set_, fieldName),
498                 Modifier.PUBLIC,
499                 JavaClassWriterHelper.void_,
500                 new String JavaDoc[]{ fieldName },
501                 new String JavaDoc[]{ normalizedFieldType },
502                 null,
503                 impl,
504                 ImplHelper.COMMENT_ENHANCER_ADDED);
505         }
506
507     }
508
509     private void writePCRootMembers(final String JavaDoc className)
510         throws IOException JavaDoc
511     {
512         final String JavaDoc[] comments = new String JavaDoc[]{
513             dotLine,
514             "Augmentation for Persistence-Capable Root Classes (added by enhancer):",
515             dotLine
516         };
517
518         // write constructor with parameter StateManager
519
writer.addConstructor(
520             ImplHelper.getClassName(className),
521             Modifier.PUBLIC,
522             new String JavaDoc[]{ ImplHelper.FIELDNAME_JDO_STATE_MANAGER },
523             new String JavaDoc[]{ ImplHelper.CLASSNAME_JDO_STATE_MANAGER },
524             null,
525             ImplHelper.getJDOConstructorSMImpl(ImplHelper.FIELDNAME_JDO_STATE_MANAGER),
526             comments);
527
528         // jdoStateManager
529
writer.addField(
530             ImplHelper.FIELDNAME_JDO_STATE_MANAGER,
531             Modifier.PUBLIC | Modifier.TRANSIENT,
532             ImplHelper.CLASSNAME_JDO_STATE_MANAGER,
533             JavaClassWriterHelper.null_,
534             ImplHelper.COMMENT_ENHANCER_ADDED);
535
536         // jdoFlags
537
writer.addField(
538             ImplHelper.FIELDNAME_JDO_FLAGS,
539             Modifier.PUBLIC | Modifier.TRANSIENT,
540             "byte",
541             "0",
542             ImplHelper.COMMENT_ENHANCER_ADDED);
543
544
545         // jdoGetStateManager
546
writer.addMethod(
547             ImplHelper.METHODNAME_JDO_GET_STATE_MANAGER,
548             Modifier.PUBLIC | Modifier.FINAL,
549             ImplHelper.CLASSNAME_JDO_STATE_MANAGER, null, null, null,
550             new String JavaDoc[] { "return " + ImplHelper.FIELDNAME_JDO_STATE_MANAGER + JavaClassWriterHelper.delim_ },
551             ImplHelper.COMMENT_ENHANCER_ADDED);
552
553         // jdoSetStateManager
554
writer.addMethod(
555             ImplHelper.METHODNAME_JDO_SET_STATE_MANAGER,
556             Modifier.PUBLIC | Modifier.FINAL,
557             JavaClassWriterHelper.void_,
558             new String JavaDoc[] { ImplHelper.FIELDNAME_JDO_STATE_MANAGER },
559             new String JavaDoc[] { ImplHelper.CLASSNAME_JDO_STATE_MANAGER }, null,
560             new String JavaDoc[] { "this." + ImplHelper.FIELDNAME_JDO_STATE_MANAGER + " = " + ImplHelper.FIELDNAME_JDO_STATE_MANAGER + JavaClassWriterHelper.delim_ },
561             ImplHelper.COMMENT_ENHANCER_ADDED);
562
563         //jdoGetFlags
564
writer.addMethod(
565             ImplHelper.METHODNAME_JDO_GET_FLAGS,
566             Modifier.PUBLIC | Modifier.FINAL,
567             "byte", null, null, null,
568             new String JavaDoc[] { "return " + ImplHelper.FIELDNAME_JDO_FLAGS +
569                     JavaClassWriterHelper.delim_ },
570             ImplHelper.COMMENT_ENHANCER_ADDED);
571
572         // jdoSetFlags
573
writer.addMethod(
574             ImplHelper.METHODNAME_JDO_SET_FLAGS,
575             Modifier.PUBLIC | Modifier.FINAL,
576             JavaClassWriterHelper.void_,
577             new String JavaDoc[] { ImplHelper.FIELDNAME_JDO_FLAGS },
578             new String JavaDoc[] { "byte" }, null,
579             new String JavaDoc[] { "this." + ImplHelper.FIELDNAME_JDO_FLAGS + " = " + ImplHelper.FIELDNAME_JDO_FLAGS + JavaClassWriterHelper.delim_ },
580             ImplHelper.COMMENT_ENHANCER_ADDED);
581
582         // getPersistenceManager
583
writer.addMethod(
584             ImplHelper.METHODNAME_JDO_GET_PERSISTENCE_MANAGER,
585             Modifier.PUBLIC | Modifier.FINAL,
586             ImplHelper.CLASSNAME_JDO_PERSISTENCE_MANAGER, null, null, null,
587             ImplHelper.getJDOStateManagerObjectDelegationImpl("getPersistenceManager()"),
588             ImplHelper.COMMENT_ENHANCER_ADDED);
589
590         // getObjectId
591
writer.addMethod(
592             ImplHelper.METHODNAME_JDO_GET_OBJECT_ID,
593             Modifier.PUBLIC | Modifier.FINAL,
594             Object JavaDoc.class.getName(), null, null, null,
595             ImplHelper.getJDOStateManagerObjectDelegationImpl("getObjectId()"),
596             ImplHelper.COMMENT_ENHANCER_ADDED);
597
598         // is-methods
599
writer.addMethod(
600             ImplHelper.METHODNAME_JDO_IS_PERSISTENT,
601             Modifier.PUBLIC | Modifier.FINAL,
602             JavaClassWriterHelper.boolean_, null, null, null,
603             ImplHelper.getJDOStateManagerBooleanDelegationImpl("isPersistent()"),
604             ImplHelper.COMMENT_ENHANCER_ADDED);
605
606         writer.addMethod(
607             ImplHelper.METHODNAME_JDO_IS_TRANSACTIONAL,
608             Modifier.PUBLIC | Modifier.FINAL,
609             JavaClassWriterHelper.boolean_, null, null, null,
610             ImplHelper.getJDOStateManagerBooleanDelegationImpl("isTransactional()"),
611             ImplHelper.COMMENT_ENHANCER_ADDED);
612
613         writer.addMethod(
614             ImplHelper.METHODNAME_JDO_IS_NEW,
615             Modifier.PUBLIC | Modifier.FINAL,
616             JavaClassWriterHelper.boolean_, null, null, null,
617             ImplHelper.getJDOStateManagerBooleanDelegationImpl("isNew()"),
618             ImplHelper.COMMENT_ENHANCER_ADDED);
619
620         writer.addMethod(
621             ImplHelper.METHODNAME_JDO_IS_DELETED,
622             Modifier.PUBLIC | Modifier.FINAL,
623             JavaClassWriterHelper.boolean_, null, null, null,
624             ImplHelper.getJDOStateManagerBooleanDelegationImpl("isDeleted()"),
625             ImplHelper.COMMENT_ENHANCER_ADDED);
626
627         writer.addMethod(
628             ImplHelper.METHODNAME_JDO_IS_DIRTY,
629             Modifier.PUBLIC | Modifier.FINAL,
630             JavaClassWriterHelper.boolean_, null, null, null,
631             ImplHelper.getJDOStateManagerBooleanDelegationImpl("isDirty()"),
632             ImplHelper.COMMENT_ENHANCER_ADDED);
633
634         // makeDirty
635
writer.addMethod(
636             ImplHelper.METHODNAME_JDO_MAKE_DIRTY,
637             Modifier.PUBLIC | Modifier.FINAL,
638             JavaClassWriterHelper.void_,
639             new String JavaDoc[]{ "fieldName" },
640             new String JavaDoc[]{ String JavaDoc.class.getName() },
641             null,
642             ImplHelper.getJDOStateManagerVoidDelegationImpl("makeDirty(fieldName)"),
643             ImplHelper.COMMENT_ENHANCER_ADDED);
644
645     }
646
647     private void writePCMembers(final String JavaDoc className)
648         throws IOException JavaDoc
649     {
650         final String JavaDoc[] comments = new String JavaDoc[]{
651             dotLine,
652             "Augmentation for Persistence-Capable Classes (added by enhancer):",
653             dotLine
654         };
655
656         final String JavaDoc[] managedFieldNames
657             = meta.getManagedFields(className);
658         final String JavaDoc[] managedFieldTypes
659             = meta.getFieldType(className, managedFieldNames);
660         final boolean isPCRoot
661             = meta.isPersistenceCapableRootClass(className);
662
663         // jdoGetField
664
writer.addMethod(
665             ImplHelper.METHODNAME_JDO_GET_FIELD,
666             Modifier.PUBLIC,
667             JavaClassWriterHelper.Object_,
668             new String JavaDoc[]{ "fieldNumber" },
669             new String JavaDoc[]{ "int" },
670             null,
671             ImplHelper.getJDOGetFieldImpl("fieldNumber",
672                                           managedFieldNames,
673                                           managedFieldTypes),
674             comments);
675
676         // jdoSetField
677
writer.addMethod(
678             ImplHelper.METHODNAME_JDO_SET_FIELD,
679             Modifier.PUBLIC,
680             JavaClassWriterHelper.void_,
681             new String JavaDoc[]{ "fieldNumber", "obj" },
682             new String JavaDoc[]{ "int", JavaClassWriterHelper.Object_ },
683             null,
684             ImplHelper.getJDOSetFieldImpl("fieldNumber", "obj",
685                                           managedFieldNames,
686                                           managedFieldTypes),
687             ImplHelper.COMMENT_ENHANCER_ADDED);
688
689         // jdoClear
690
writer.addMethod(
691             ImplHelper.METHODNAME_JDO_CLEAR,
692             Modifier.PUBLIC,
693             JavaClassWriterHelper.void_, null, null, null,
694             ImplHelper.getJDOClearImpl(className, meta, managedFieldNames,
695                                        managedFieldTypes),
696             ImplHelper.COMMENT_ENHANCER_ADDED);
697
698         // jdoNewInstance
699
writer.addMethod(
700             ImplHelper.METHODNAME_JDO_NEW_INSTANCE,
701             Modifier.PUBLIC,
702             JavaClassWriterHelper.Object_,
703             new String JavaDoc[]{ "sm" },
704             new String JavaDoc[]{ ImplHelper.CLASSNAME_JDO_STATE_MANAGER },
705             null,
706             ImplHelper.getJDONewInstanceImpl(className, "sm"),
707             ImplHelper.COMMENT_ENHANCER_ADDED);
708
709     }
710
711     private void writeOidClass(final String JavaDoc className,
712                                final String JavaDoc oidClassName,
713                                final boolean enclosedOid)
714         throws IOException JavaDoc
715     {
716         final String JavaDoc[] comments = new String JavaDoc[]{
717             dotLine,
718             "Key Class:",
719             dotLine
720         };
721
722         final String JavaDoc superOidClassName
723             = ImplHelper.normalizeClassName(meta.getSuperKeyClass(className));
724
725         JavaClassWriter oidWriter = new IOJavaClassWriter();
726
727         oidWriter.setClassDeclaration(
728             (enclosedOid ? Modifier.PUBLIC | Modifier.STATIC : 0),
729             oidClassName,
730             ImplHelper.COMMENT_NOT_ENHANCER_ADDED);
731         oidWriter.setSuperclass(superOidClassName);
732         oidWriter.addInterface(Serializable JavaDoc.class.getName());
733
734         final boolean isPCRoot
735             = meta.isPersistenceCapableRootClass(className);
736
737         final String JavaDoc[] pknames = meta.getKeyFields(className);
738         final String JavaDoc[] pktypes = meta.getFieldType(className, pknames);
739
740         // write the PK-fields
741
for (int i = 0; i < pknames.length; i++) {
742             oidWriter.addField(
743                 pknames[i],
744                 Modifier.PUBLIC,
745                 ImplHelper.normalizeClassName(pktypes[i]),
746                 null,
747                 null);
748         }
749
750         // write default constructor
751
oidWriter.addConstructor(
752             oidClassName,
753             Modifier.PUBLIC,
754             null, null, null,
755             ImplHelper.getDefaultConstructorImpl(),
756             ImplHelper.COMMENT_NOT_ENHANCER_ADDED);
757
758         // hashCode
759
oidWriter.addMethod(
760             "hashCode",
761             Modifier.PUBLIC,
762             "int",
763             null,
764             null,
765             null,
766             ImplHelper.getOidHashCodeImpl(pknames,
767                                           pktypes,
768                                           isPCRoot),
769             ImplHelper.COMMENT_NOT_ENHANCER_ADDED);
770
771         // equals
772
oidWriter.addMethod(
773             "equals", Modifier.PUBLIC, JavaClassWriterHelper.boolean_,
774             new String JavaDoc[]{ "pk" },
775             new String JavaDoc[]{ Object JavaDoc.class.getName() },
776             null,
777             ImplHelper.getOidEqualsImpl(oidClassName,
778                                         pknames,
779                                         pktypes,
780                                         "pk",
781                                         isPCRoot),
782             ImplHelper.COMMENT_NOT_ENHANCER_ADDED);
783
784           if (enclosedOid) {
785               writer.addClass(oidWriter);
786           } else {
787               fWriter.addClass(oidWriter);
788           }
789     }
790
791     static private String JavaDoc createMethodName(final String JavaDoc prefix,
792                                            final String JavaDoc fieldName)
793     {
794         return (prefix + Character.toUpperCase(fieldName.charAt(0))
795                 + fieldName.substring(1));
796     }
797
798     //XXX use common logger later
799
private void printMessage(String JavaDoc msg)
800     {
801       logger.finest("TP PCClassGen: " + msg); // NOI18N
802
}
803
804     private void printError(String JavaDoc msg, Throwable JavaDoc ex)
805     {
806         if (msg != null) {
807             String JavaDoc errmsg=msg + (ex != null ? ": " + ex.getMessage() : ""); //NOI18N
808
logger.log(Logger.SEVERE,"CME.generic_exception",errmsg); //NOI18N
809
}
810         if (ex != null) {
811             logger.log(Logger.SEVERE,"CME.generic_exception_stack",ex); //NOI18N
812
}
813     }
814 }
815
Popular Tags