Search in sources :

Example 1 with IOJavaClassWriter

use of com.sun.jdo.spi.persistence.utility.generator.io.IOJavaClassWriter in project Payara by payara.

the class JDOConcreteBeanGenerator method generate.

/**
 * Generate all required classes for this CMP bean.
 * @param methodHelper the AbstractMethodHelper instance that contains
 * all categorized methods and some other convenience methods for this bean.
 * @param beanName the ejb-name of this bean.
 * @param appName the name of the application that contains this bean.
 * @param srcout that path to the generated source files.
 * @param classout that path to the compiled class files.
 * @return a Collection of generated source files.
 */
Collection<File> generate(AbstractMethodHelper methodHelper, String beanName, String appName, File srcout, File classout) throws IOException {
    Collection<File> files = new ArrayList<File>();
    this.beanName = beanName;
    this.appName = appName;
    this.abstractBean = nameMapper.getAbstractBeanClassForEjbName(beanName);
    String pkgName = CMPTemplateFormatter.getPackageName(abstractBean);
    concreteImplName = nameMapper.getConcreteBeanClassForEjbName(beanName);
    String shortCmpName = CMPTemplateFormatter.getShortClassName(concreteImplName);
    pcname = nameMapper.getPersistenceClassForEjbName(beanName);
    pcnameParam[0] = pcname;
    PersistenceClassElement pcClassElement = model.getPersistenceClass(pcname);
    pkClass = nameMapper.getKeyClassForEjbName(beanName).replace('$', '.');
    pkClassParam[0] = pkClass;
    PersistenceFieldElement[] allFields = pcClassElement.getFields();
    String prefix = srcout.getPath() + File.separator + concreteImplName.replace('.', File.separatorChar);
    String cmp_file_name = prefix + CMPTemplateFormatter.javaExtension_;
    String hlp_file_name = prefix + CMPTemplateFormatter.Helper_ + CMPTemplateFormatter.javaExtension_;
    hasLocalInterface = (nameMapper.getLocalInterfaceForEjbName(beanName) != null);
    hasRemoteInterface = (nameMapper.getRemoteInterfaceForEjbName(beanName) != null);
    if (logger.isLoggable(Logger.FINE)) {
        logger.fine(// NOI18N
        "allFields: " + ((allFields != null) ? allFields.length : 0));
        // NOI18N
        logger.fine("cmp_file_name: " + cmp_file_name);
        // NOI18N
        logger.fine("hlp_file_name: " + hlp_file_name);
        // NOI18N
        logger.fine("cmp_name: " + concreteImplName);
        // NOI18N
        logger.fine("pkClass: " + pkClass);
        // NOI18N
        logger.fine("PCname: " + pcname);
    }
    File cmp_file = new File(cmp_file_name);
    JavaFileWriter concreteImplFileWriter = new IOJavaFileWriter(cmp_file);
    concreteImplWriter = new IOJavaClassWriter();
    File hlp_file = new File(hlp_file_name);
    JavaFileWriter helperFileWriter = new IOJavaFileWriter(hlp_file);
    jdoHelperWriter = new IOJavaClassWriter();
    // Add package statement to both classes.
    if (pkgName != null && pkgName.length() > 0) {
        concreteImplFileWriter.setPackage(pkgName, null);
        helperFileWriter.setPackage(pkgName, null);
    }
    // Add imports statements to both classes.
    addImportStatements(concreteImplFileWriter, helperFileWriter);
    // Generate class name for the concrete impl.
    oneParam[0] = CMPTemplateFormatter.cmpImplCommentsTemplate;
    concreteImplWriter.setClassDeclaration(Modifier.PUBLIC, shortCmpName, oneParam);
    // Add interfaces to the class declarations.
    addInterfaces();
    concreteImplWriter.setSuperclass(abstractBean);
    // Add no-arg constructor.
    concreteImplWriter.addConstructor(shortCmpName, Modifier.PUBLIC, null, null, null, CMPTemplateFormatter.super_, null);
    // Add helper class.
    helperName = shortCmpName + CMPTemplateFormatter.Helper_;
    oneParam[0] = shortCmpName;
    jdoHelperWriter.setClassDeclaration(Modifier.PUBLIC, helperName, CMPTemplateFormatter.getBodyAsStrings(CMPTemplateFormatter.hcomformatter.format(oneParam)));
    setHelperSuperclass();
    // Print internal variables.
    generateFields();
    // Generate type specific methods.
    generateTypeSpecificMethods(allFields, methodHelper);
    // Add finders for all types and selectors for CMP2.0 only.
    generateFinders(methodHelper);
    // Add ejbCreate<XXX> methods.
    generateCreateMethods(methodHelper.getCreateMethods());
    // Add other required methods.
    generateKnownMethods(methodHelper);
    // Add helper methods for the helper class.
    generateHelperClassMethods();
    // Add conversion methods to the helper class.
    generateConversions();
    // Add ObjectId/PrimaryKey conversion methods.
    generatePKObjectIdConversion(getKeyFields(allFields));
    // Print end of classes.
    concreteImplFileWriter.addClass(concreteImplWriter);
    concreteImplFileWriter.save();
    helperFileWriter.addClass(jdoHelperWriter);
    helperFileWriter.save();
    files.add(cmp_file);
    files.add(hlp_file);
    return files;
}
Also used : IOJavaFileWriter(com.sun.jdo.spi.persistence.utility.generator.io.IOJavaFileWriter) JavaFileWriter(com.sun.jdo.spi.persistence.utility.generator.JavaFileWriter) IOJavaFileWriter(com.sun.jdo.spi.persistence.utility.generator.io.IOJavaFileWriter) IOJavaClassWriter(com.sun.jdo.spi.persistence.utility.generator.io.IOJavaClassWriter) PersistenceFieldElement(com.sun.jdo.api.persistence.model.jdo.PersistenceFieldElement) PersistenceClassElement(com.sun.jdo.api.persistence.model.jdo.PersistenceClassElement) File(java.io.File)

Example 2 with IOJavaClassWriter

use of com.sun.jdo.spi.persistence.utility.generator.io.IOJavaClassWriter in project Payara by payara.

the class Main method writeOidClass.

private void writeOidClass(final String className, final String oidClassName, final boolean enclosedOid) throws IOException {
    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);
    }
}
Also used : Serializable(java.io.Serializable) IOJavaClassWriter(com.sun.jdo.spi.persistence.utility.generator.io.IOJavaClassWriter) IOJavaClassWriter(com.sun.jdo.spi.persistence.utility.generator.io.IOJavaClassWriter) JavaClassWriter(com.sun.jdo.spi.persistence.utility.generator.JavaClassWriter)

Example 3 with IOJavaClassWriter

use of com.sun.jdo.spi.persistence.utility.generator.io.IOJavaClassWriter in project Payara by payara.

the class Main method generate.

// entry point for EJB TP class generation
// The argument is a fully qualified class name expected in the
// JVM format, that is, with '/' for '.' as separator.  See comment
// at the beginning of this class.
public File generate(final String className) throws IOException {
    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;
}
Also used : IOJavaFileWriter(com.sun.jdo.spi.persistence.utility.generator.io.IOJavaFileWriter) IOJavaClassWriter(com.sun.jdo.spi.persistence.utility.generator.io.IOJavaClassWriter) File(java.io.File)

Aggregations

IOJavaClassWriter (com.sun.jdo.spi.persistence.utility.generator.io.IOJavaClassWriter)3 IOJavaFileWriter (com.sun.jdo.spi.persistence.utility.generator.io.IOJavaFileWriter)2 File (java.io.File)2 PersistenceClassElement (com.sun.jdo.api.persistence.model.jdo.PersistenceClassElement)1 PersistenceFieldElement (com.sun.jdo.api.persistence.model.jdo.PersistenceFieldElement)1 JavaClassWriter (com.sun.jdo.spi.persistence.utility.generator.JavaClassWriter)1 JavaFileWriter (com.sun.jdo.spi.persistence.utility.generator.JavaFileWriter)1 Serializable (java.io.Serializable)1