FileDocCategorySizeDatePackage
Main.javaAPI DocGlassfish v2 API30532Fri May 04 22:34:38 BST 2007com.sun.jdo.api.persistence.enhancer.generator

Main

public final class Main extends com.sun.jdo.api.persistence.enhancer.util.Assertion

Fields Summary
private static final com.sun.jdo.spi.persistence.utility.logging.Logger
logger
The logger
private static final String
dotLine
private final PrintWriter
out
The stream to write messages to.
private final PrintWriter
err
The stream to write error messages to.
private final CmdLineOptions
opts
The command line options.
private com.sun.jdo.spi.persistence.utility.generator.JavaFileWriter
fWriter
Java Writer Class
private com.sun.jdo.spi.persistence.utility.generator.JavaClassWriter
writer
private com.sun.jdo.api.persistence.enhancer.meta.ExtendedJDOMetaData
meta
The MetaData for generating classes.
private File
destinationDir
private static final ResourceBundle
messages
I18N message handler
Constructors Summary
public Main()

 // NOI18N
     
    
public Main(com.sun.jdo.api.persistence.enhancer.meta.ExtendedJDOMetaData meta, File destinationDir)

        this.meta = meta;
        this.destinationDir = destinationDir;
        createDestinationDir();
    
Methods Summary
private voidcreateDestinationDir()

        // create the destination directory
        if (!destinationDir.exists() && !destinationDir.mkdirs()) {
            throw new IOException(I18NHelper.getMessage(messages,"EXC_DestDirCreateFailure",destinationDir));  //NOI18N
        }
    
private static java.lang.StringcreateMethodName(java.lang.String prefix, java.lang.String fieldName)

        return (prefix + Character.toUpperCase(fieldName.charAt(0))
                + fieldName.substring(1));
    
private voidgenerate()

        final String[] classes = meta.getKnownClasses();
        for (int i = 0; i < classes.length; i++) {
            final String className = classes[i];
            generate(className);
        }
    
public java.io.Filegenerate(java.lang.String className)

        affirm(className != null);
        printMessage("generating '" + className + "'...");

        //@olsen, 4653156: fixed file name
        final String filePath = className.replace('/", File.separatorChar);
        final String classFileName
            = filePath + JavaClassWriterHelper.javaExtension_;
        final File file = new File(destinationDir, classFileName);

        //@olsen: not needed: IOJavaFileWriter takes care of creating file
        // create the destination directory
        //final File dir = file.getAbsoluteFile().getParentFile();
        //if (!dir.exists() && !dir.mkdirs()) {
        //    throw new IOException("unable to create destination directory: "
        //                          + "'" + destinationDir + "'");
        //}

        fWriter = new IOJavaFileWriter(file);
        writer = new IOJavaClassWriter();
        generateClass(className);
        fWriter.addClass(writer);
        printMessage("DONE generating '" + className + "'...");

        //@olsen: moved from finally{} to main block
        // by JavaFileWriter, no stale resources remain allocated ever
        fWriter.save();
        return file;
    
private voidgenerateClass(java.lang.String className)

        affirm(className != null);

        final String packageName = ImplHelper.getPackageName(className);
        fWriter.setPackage(packageName, null);

        // write the class header and key class
        final String oidClassName = meta.getKeyClass(className);
        if (oidClassName == null) {
            writeClassHeader(className);
        } else {
            final String oidPackageName
                = ImplHelper.getPackageName(oidClassName);
            affirm(packageName.equals(oidPackageName),
                   "PC class and key class must be in same package.");

            final boolean enclosedOid
                = oidClassName.startsWith(className + "$");
            if (enclosedOid) {
                writeClassHeader(className);
                writeOidClass(className, ImplHelper.getClassName(oidClassName),
                    enclosedOid);
            } else {
                writeOidClass(className, ImplHelper.getClassName(oidClassName),
                    enclosedOid);
                writeClassHeader(className);
            }
        }

        writeClassMembers(className);

        // write the augmentation
        final boolean isPC = meta.isPersistenceCapableClass(className);
        if (isPC) {
            final boolean isPCRoot
                = meta.isPersistenceCapableRootClass(className);
            if (isPCRoot) {
                writePCRootMembers(className);
            }
            writePCMembers(className);
        }
    
private voidinit()

        // load the properties
        affirm(opts.jdoPropertiesFileName != null);
        FileInputStream finput = null;
        try {
            final File f = new File(opts.jdoPropertiesFileName);
            finput = new FileInputStream(f);
            final Properties props = new Properties();
            props.load(finput);
            meta = new JDOMetaDataPropertyImpl(props, out);
        } finally {
            if (finput != null) {
                try {
                    finput.close();
                } catch(Exception ex) {
                    printError(ex.getMessage(), ex);
                }
            }
        }

        affirm(opts.destinationDirectory != null);
        destinationDir = new File(opts.destinationDirectory);
        createDestinationDir();
    
public static final voidmain(java.lang.String[] argv)

        final Main gen = new Main();
        try {
            gen.opts.processArgs(argv);
            gen.init();
            gen.generate();
        } catch(Exception ex) {
            gen.printError(null, ex);
        }
    
private voidprintError(java.lang.String msg, java.lang.Throwable ex)

        if (msg != null) {
            String errmsg=msg + (ex != null ? ": " + ex.getMessage() : ""); //NOI18N
            logger.log(Logger.SEVERE,"CME.generic_exception",errmsg); //NOI18N
        }
        if (ex != null) {
            logger.log(Logger.SEVERE,"CME.generic_exception_stack",ex); //NOI18N
        }
    
private voidprintMessage(java.lang.String msg)

	  logger.finest("TP PCClassGen: " + msg); // NOI18N
    
private voidwriteClassHeader(java.lang.String className)

        final boolean isPCRoot = meta.isPersistenceCapableRootClass(className);
        final String superclass =
            ImplHelper.normalizeClassName(meta.getSuperClass(className));

        final String[] comments = null;
        final String[] interfaces
            = (isPCRoot
               ? new String[]{ ImplHelper.CLASSNAME_JDO_PERSISTENCE_CAPABLE, "Cloneable" }
               : null);
        writer.setClassDeclaration(meta.getClassModifiers(className),
                ImplHelper.getClassName(className), comments);
        writer.setSuperclass(superclass);
        if (interfaces != null) {
            for (int i = 0; i < interfaces.length; i++) {
                writer.addInterface(interfaces[i]);
            }
        }
    
private voidwriteClassMembers(java.lang.String className)

        final String[] comments = new String[]{
            dotLine,
            "Class Members:",
            dotLine
        };

        // write default constructor
        writer.addConstructor(ImplHelper.getClassName(className),
            Modifier.PUBLIC, null, null, null,
            ImplHelper.getDefaultConstructorImpl(),
            comments);

        final String[] fieldNames = meta.getKnownFields(className);
        final int n = (fieldNames != null ? fieldNames.length : 0);

        // write method clone() for enhancer testing purpose
        final ArrayList list = new ArrayList();
        for (int i = 0; i < n; i++) {
            final String fieldName = (String)fieldNames[i];
            final int access = meta.getFieldModifiers(className, fieldName);
            if ((access & Modifier.STATIC) == 0) {
                list.add(fieldName);
            }
        }

        writer.addMethod(
            "clone",
            Modifier.PUBLIC,
            JavaClassWriterHelper.Object_,
            null,
            null,
            new String[]{ "java.lang.CloneNotSupportedException" },
            ImplHelper.getCloneImpl(className),
            ImplHelper.COMMENT_NOT_ENHANCER_ADDED);

        // write the fields and with their bean getters/setters
        for (int i = 0; i < n; i++) {
            final String fieldName = (String)fieldNames[i];
            writeFieldMember(className, fieldName);
        }
    
private voidwriteFieldMember(java.lang.String className, java.lang.String fieldName)

        final String fieldType = meta.getFieldType(className, fieldName);
        final String normalizedFieldType =
            ImplHelper.normalizeClassName(fieldType);
        final int fieldNumber = meta.getFieldNo(className, fieldName);
        final int flags = meta.getFieldFlags(className, fieldName);

        final int access = meta.getFieldModifiers(className, fieldName);

        // the field
        writer.addField(
            fieldName,
            access,
            normalizedFieldType,
            null, null);

        // do not write bean getters and setters for static fields
        if ((access & Modifier.STATIC) != 0) {
            return;
        }

        // accessor
        {
            affirm(((flags & meta.CHECK_READ) == 0)
                   | (flags & meta.MEDIATE_READ) == 0);
            final String[] impl;
            if ((flags & meta.CHECK_READ) != 0) {
                impl = ImplHelper.getFieldCheckReadImpl(fieldName,
                                                        fieldType,
                                                        fieldNumber);
            } else if ((flags & meta.MEDIATE_READ) != 0) {
                impl = ImplHelper.getFieldMediateReadImpl(fieldName,
                                                          fieldType,
                                                          fieldNumber);
            } else {
                impl = ImplHelper.getFieldDirectReadImpl(fieldName,
                                                         fieldType,
                                                         fieldNumber);
            }
            writer.addMethod(
                createMethodName(JavaClassWriterHelper.get_, fieldName),
                Modifier.PUBLIC,
                normalizedFieldType,
                null, null, null,
                impl,
                ImplHelper.COMMENT_ENHANCER_ADDED);
        }

        // mutator
        {
            affirm(((flags & meta.CHECK_WRITE) == 0)
                   | (flags & meta.MEDIATE_WRITE) == 0);
            final String[] impl;
            if ((flags & meta.CHECK_WRITE) != 0) {
                impl = ImplHelper.getFieldCheckWriteImpl(fieldName,
                                                         fieldType,
                                                         fieldNumber,
                                                         fieldName);
            } else if ((flags & meta.MEDIATE_WRITE) != 0
                    && !meta.isKnownNonManagedField(className, fieldName,
                    null)) {
                impl = ImplHelper.getFieldMediateWriteImpl(fieldName,
                                                           fieldType,
                                                           fieldNumber,
                                                           fieldName);
            } else {
                impl = ImplHelper.getFieldDirectWriteImpl(fieldName,
                                                          fieldType,
                                                          fieldNumber,
                                                          fieldName);
            }
            writer.addMethod(
                createMethodName(JavaClassWriterHelper.set_, fieldName),
                Modifier.PUBLIC,
                JavaClassWriterHelper.void_,
                new String[]{ fieldName },
                new String[]{ normalizedFieldType },
                null,
                impl,
                ImplHelper.COMMENT_ENHANCER_ADDED);
        }

    
private voidwriteOidClass(java.lang.String className, java.lang.String oidClassName, boolean enclosedOid)

        final String[] comments = new String[]{
            dotLine,
            "Key Class:",
            dotLine
        };

        final String superOidClassName
            = ImplHelper.normalizeClassName(meta.getSuperKeyClass(className));

        JavaClassWriter oidWriter = new IOJavaClassWriter();

        oidWriter.setClassDeclaration(
            (enclosedOid ? Modifier.PUBLIC | Modifier.STATIC : 0),
            oidClassName,
            ImplHelper.COMMENT_NOT_ENHANCER_ADDED);
        oidWriter.setSuperclass(superOidClassName);
        oidWriter.addInterface(Serializable.class.getName());

        final boolean isPCRoot
            = meta.isPersistenceCapableRootClass(className);

        final String[] pknames = meta.getKeyFields(className);
        final String[] pktypes = meta.getFieldType(className, pknames);

        // write the PK-fields
        for (int i = 0; i < pknames.length; i++) {
            oidWriter.addField(
                pknames[i],
                Modifier.PUBLIC,
                ImplHelper.normalizeClassName(pktypes[i]),
                null,
                null);
        }

        // write default constructor
        oidWriter.addConstructor(
            oidClassName,
            Modifier.PUBLIC,
            null, null, null,
            ImplHelper.getDefaultConstructorImpl(),
            ImplHelper.COMMENT_NOT_ENHANCER_ADDED);

        // hashCode
        oidWriter.addMethod(
            "hashCode",
            Modifier.PUBLIC,
            "int",
            null,
            null,
            null,
            ImplHelper.getOidHashCodeImpl(pknames,
                                          pktypes,
                                          isPCRoot),
            ImplHelper.COMMENT_NOT_ENHANCER_ADDED);

        // equals
        oidWriter.addMethod(
            "equals", Modifier.PUBLIC, JavaClassWriterHelper.boolean_,
            new String[]{ "pk" },
            new String[]{ Object.class.getName() },
            null,
            ImplHelper.getOidEqualsImpl(oidClassName,
                                        pknames,
                                        pktypes,
                                        "pk",
                                        isPCRoot),
            ImplHelper.COMMENT_NOT_ENHANCER_ADDED);

          if (enclosedOid) {
              writer.addClass(oidWriter);
          } else {
              fWriter.addClass(oidWriter);
          }
    
private voidwritePCMembers(java.lang.String className)

        final String[] comments = new String[]{
            dotLine,
            "Augmentation for Persistence-Capable Classes (added by enhancer):",
            dotLine
        };

        final String[] managedFieldNames
            = meta.getManagedFields(className);
        final String[] managedFieldTypes
            = meta.getFieldType(className, managedFieldNames);
        final boolean isPCRoot
            = meta.isPersistenceCapableRootClass(className);

        // jdoGetField
        writer.addMethod(
            ImplHelper.METHODNAME_JDO_GET_FIELD,
            Modifier.PUBLIC,
            JavaClassWriterHelper.Object_,
            new String[]{ "fieldNumber" },
            new String[]{ "int" },
            null,
            ImplHelper.getJDOGetFieldImpl("fieldNumber",
                                          managedFieldNames,
                                          managedFieldTypes),
            comments);

        // jdoSetField
        writer.addMethod(
            ImplHelper.METHODNAME_JDO_SET_FIELD,
            Modifier.PUBLIC,
            JavaClassWriterHelper.void_,
            new String[]{ "fieldNumber", "obj" },
            new String[]{ "int", JavaClassWriterHelper.Object_ },
            null,
            ImplHelper.getJDOSetFieldImpl("fieldNumber", "obj",
                                          managedFieldNames,
                                          managedFieldTypes),
            ImplHelper.COMMENT_ENHANCER_ADDED);

        // jdoClear
        writer.addMethod(
            ImplHelper.METHODNAME_JDO_CLEAR,
            Modifier.PUBLIC,
            JavaClassWriterHelper.void_, null, null, null,
            ImplHelper.getJDOClearImpl(className, meta, managedFieldNames,
                                       managedFieldTypes),
            ImplHelper.COMMENT_ENHANCER_ADDED);

        // jdoNewInstance
        writer.addMethod(
            ImplHelper.METHODNAME_JDO_NEW_INSTANCE,
            Modifier.PUBLIC,
            JavaClassWriterHelper.Object_,
            new String[]{ "sm" },
            new String[]{ ImplHelper.CLASSNAME_JDO_STATE_MANAGER },
            null,
            ImplHelper.getJDONewInstanceImpl(className, "sm"),
            ImplHelper.COMMENT_ENHANCER_ADDED);

    
private voidwritePCRootMembers(java.lang.String className)

        final String[] comments = new String[]{
            dotLine,
            "Augmentation for Persistence-Capable Root Classes (added by enhancer):",
            dotLine
        };

        // write constructor with parameter StateManager
        writer.addConstructor(
            ImplHelper.getClassName(className),
            Modifier.PUBLIC,
            new String[]{ ImplHelper.FIELDNAME_JDO_STATE_MANAGER },
            new String[]{ ImplHelper.CLASSNAME_JDO_STATE_MANAGER },
            null,
            ImplHelper.getJDOConstructorSMImpl(ImplHelper.FIELDNAME_JDO_STATE_MANAGER),
            comments);

        // jdoStateManager
        writer.addField(
            ImplHelper.FIELDNAME_JDO_STATE_MANAGER,
            Modifier.PUBLIC | Modifier.TRANSIENT,
            ImplHelper.CLASSNAME_JDO_STATE_MANAGER,
            JavaClassWriterHelper.null_,
            ImplHelper.COMMENT_ENHANCER_ADDED);

        // jdoFlags
        writer.addField(
            ImplHelper.FIELDNAME_JDO_FLAGS,
            Modifier.PUBLIC | Modifier.TRANSIENT,
            "byte",
            "0",
            ImplHelper.COMMENT_ENHANCER_ADDED);


        // jdoGetStateManager
        writer.addMethod(
            ImplHelper.METHODNAME_JDO_GET_STATE_MANAGER,
            Modifier.PUBLIC | Modifier.FINAL,
            ImplHelper.CLASSNAME_JDO_STATE_MANAGER, null, null, null,
            new String[] { "return " +  ImplHelper.FIELDNAME_JDO_STATE_MANAGER + JavaClassWriterHelper.delim_ },
            ImplHelper.COMMENT_ENHANCER_ADDED);

        // jdoSetStateManager
        writer.addMethod(
            ImplHelper.METHODNAME_JDO_SET_STATE_MANAGER,
            Modifier.PUBLIC | Modifier.FINAL,
            JavaClassWriterHelper.void_,
            new String[] { ImplHelper.FIELDNAME_JDO_STATE_MANAGER },
            new String[] { ImplHelper.CLASSNAME_JDO_STATE_MANAGER }, null,
            new String[] { "this." +  ImplHelper.FIELDNAME_JDO_STATE_MANAGER + " = " + ImplHelper.FIELDNAME_JDO_STATE_MANAGER + JavaClassWriterHelper.delim_ },
            ImplHelper.COMMENT_ENHANCER_ADDED);

        //jdoGetFlags
        writer.addMethod(
            ImplHelper.METHODNAME_JDO_GET_FLAGS,
            Modifier.PUBLIC | Modifier.FINAL,
            "byte", null, null, null,
            new String[] { "return " +  ImplHelper.FIELDNAME_JDO_FLAGS +
                    JavaClassWriterHelper.delim_ },
            ImplHelper.COMMENT_ENHANCER_ADDED);

        // jdoSetFlags
        writer.addMethod(
            ImplHelper.METHODNAME_JDO_SET_FLAGS,
            Modifier.PUBLIC | Modifier.FINAL,
            JavaClassWriterHelper.void_,
            new String[] { ImplHelper.FIELDNAME_JDO_FLAGS },
            new String[] { "byte" }, null,
            new String[] { "this." +  ImplHelper.FIELDNAME_JDO_FLAGS + " = " + ImplHelper.FIELDNAME_JDO_FLAGS + JavaClassWriterHelper.delim_ },
            ImplHelper.COMMENT_ENHANCER_ADDED);

        // getPersistenceManager
        writer.addMethod(
            ImplHelper.METHODNAME_JDO_GET_PERSISTENCE_MANAGER,
            Modifier.PUBLIC | Modifier.FINAL,
            ImplHelper.CLASSNAME_JDO_PERSISTENCE_MANAGER, null, null, null,
            ImplHelper.getJDOStateManagerObjectDelegationImpl("getPersistenceManager()"),
            ImplHelper.COMMENT_ENHANCER_ADDED);

        // getObjectId
        writer.addMethod(
            ImplHelper.METHODNAME_JDO_GET_OBJECT_ID,
            Modifier.PUBLIC | Modifier.FINAL,
            Object.class.getName(), null, null, null,
            ImplHelper.getJDOStateManagerObjectDelegationImpl("getObjectId()"),
            ImplHelper.COMMENT_ENHANCER_ADDED);

        // is-methods
        writer.addMethod(
            ImplHelper.METHODNAME_JDO_IS_PERSISTENT,
            Modifier.PUBLIC | Modifier.FINAL,
            JavaClassWriterHelper.boolean_, null, null, null,
            ImplHelper.getJDOStateManagerBooleanDelegationImpl("isPersistent()"),
            ImplHelper.COMMENT_ENHANCER_ADDED);

        writer.addMethod(
            ImplHelper.METHODNAME_JDO_IS_TRANSACTIONAL,
            Modifier.PUBLIC | Modifier.FINAL,
            JavaClassWriterHelper.boolean_, null, null, null,
            ImplHelper.getJDOStateManagerBooleanDelegationImpl("isTransactional()"),
            ImplHelper.COMMENT_ENHANCER_ADDED);

        writer.addMethod(
            ImplHelper.METHODNAME_JDO_IS_NEW,
            Modifier.PUBLIC | Modifier.FINAL,
            JavaClassWriterHelper.boolean_, null, null, null,
            ImplHelper.getJDOStateManagerBooleanDelegationImpl("isNew()"),
            ImplHelper.COMMENT_ENHANCER_ADDED);

        writer.addMethod(
            ImplHelper.METHODNAME_JDO_IS_DELETED,
            Modifier.PUBLIC | Modifier.FINAL,
            JavaClassWriterHelper.boolean_, null, null, null,
            ImplHelper.getJDOStateManagerBooleanDelegationImpl("isDeleted()"),
            ImplHelper.COMMENT_ENHANCER_ADDED);

        writer.addMethod(
            ImplHelper.METHODNAME_JDO_IS_DIRTY,
            Modifier.PUBLIC | Modifier.FINAL,
            JavaClassWriterHelper.boolean_, null, null, null,
            ImplHelper.getJDOStateManagerBooleanDelegationImpl("isDirty()"),
            ImplHelper.COMMENT_ENHANCER_ADDED);

        // makeDirty
        writer.addMethod(
            ImplHelper.METHODNAME_JDO_MAKE_DIRTY,
            Modifier.PUBLIC | Modifier.FINAL,
            JavaClassWriterHelper.void_,
            new String[]{ "fieldName" },
            new String[]{ String.class.getName() },
            null,
            ImplHelper.getJDOStateManagerVoidDelegationImpl("makeDirty(fieldName)"),
            ImplHelper.COMMENT_ENHANCER_ADDED);