Search in sources :

Example 1 with IOJavaFileWriter

use of com.sun.jdo.spi.persistence.utility.generator.io.IOJavaFileWriter 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 IOJavaFileWriter

use of com.sun.jdo.spi.persistence.utility.generator.io.IOJavaFileWriter 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)2 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 JavaFileWriter (com.sun.jdo.spi.persistence.utility.generator.JavaFileWriter)1