Esempio n. 1
0
    /**
     * Creates a SourceGenerator using the specific field info Factory.
     * @param infoFactory the FieldInfoFactory to use.
    */
    public SourceGenerator(FieldInfoFactory infoFactory) {
        super();

        dialog = new ConsoleDialog();

        if (infoFactory == null)
            this.infoFactory = new FieldInfoFactory();
        else
            this.infoFactory = infoFactory;

        this.sourceFactory = new SourceFactory(infoFactory);

        header = new JComment(JComment.HEADER_STYLE);
        header.appendComment(DEFAULT_HEADER);

    } //-- SourceGenerator
Esempio n. 2
0
  /**
   * Prints the source code for this JClass
   *
   * @param lineSeparator the line separator to use at the end of each line. If null, then the
   *     default line separator for the runtime platform will be used.
   */
  public void print(String lineSeparator) {

    // -- open output file
    String name = getLocalName();
    String filename = name + ".java";

    if ((packageName != null) && (packageName.length() > 0)) {
      String path = packageName.replace('.', File.separatorChar);
      File pathFile = new File(path);
      if (!pathFile.exists()) {
        pathFile.mkdirs();
      }
      filename = path + File.separator + filename;
    }

    File file = new File(filename);
    JSourceWriter jsw = null;
    try {
      jsw = new JSourceWriter(new FileWriter(file));
    } catch (java.io.IOException ioe) {
      System.out.println("unable to create class file: " + filename);
      return;
    }

    if (lineSeparator == null) {
      lineSeparator = System.getProperty("line.separator");
    }
    jsw.setLineSeparator(lineSeparator);

    StringBuffer buffer = new StringBuffer();

    // -- write class header
    if (header != null) header.print(jsw);
    else {
      jsw.writeln("/*");
      jsw.writeln(" * " + DEFAULT_HEADER);
      jsw.writeln("*/");
    }
    jsw.writeln();
    jsw.flush();

    // -- print package name
    if ((packageName != null) && (packageName.length() > 0)) {

      buffer.setLength(0);
      buffer.append("package ");
      buffer.append(packageName);
      buffer.append(';');
      jsw.writeln(buffer.toString());
      jsw.writeln();
    }

    // -- print imports
    jsw.writeln("  //---------------------------------/");
    jsw.writeln(" //- Imported classes and packages -/");
    jsw.writeln("//---------------------------------/");
    jsw.writeln();
    for (int i = 0; i < imports.size(); i++) {
      jsw.write("import ");
      jsw.write(imports.elementAt(i));
      jsw.writeln(';');
    }
    jsw.writeln();

    // ------------/
    // - Java Doc -/
    // ------------/

    jdc.print(jsw);

    // -- print class information
    // -- we need to add some JavaDoc API adding comments

    buffer.setLength(0);

    if (modifiers.isPrivate()) {
      buffer.append("private ");
    } else if (modifiers.isPublic()) {
      buffer.append("public ");
    }

    if (modifiers.isAbstract()) {
      buffer.append("abstract ");
    }

    buffer.append("class ");
    buffer.append(getLocalName());
    buffer.append(' ');
    if (superClass != null) {
      buffer.append("extends ");
      buffer.append(superClass);
      buffer.append(' ');
    }
    if (interfaces.size() > 0) {
      int iSize = interfaces.size();
      boolean endl = false;
      if ((iSize > 1) || (superClass != null)) {
        jsw.writeln(buffer.toString());
        buffer.setLength(0);
        endl = true;
      }
      buffer.append("implements ");
      for (int i = 0; i < iSize; i++) {
        if (i > 0) buffer.append(", ");
        buffer.append(interfaces.elementAt(i));
      }
      if (endl) {
        jsw.writeln(buffer.toString());
        buffer.setLength(0);
      } else buffer.append(' ');
    }

    buffer.append('{');
    jsw.writeln(buffer.toString());
    buffer.setLength(0);
    jsw.writeln();

    jsw.indent();

    // -- declare members

    if (members.size() > 0) {
      jsw.writeln();
      jsw.writeln("  //--------------------/");
      jsw.writeln(" //- Member Variables -/");
      jsw.writeln("//--------------------/");
      jsw.writeln();
    }

    for (int i = 0; i < members.size(); i++) {

      JMember jMember = (JMember) members.get(i);

      // -- print Java comment
      JDocComment comment = jMember.getComment();
      if (comment != null) comment.print(jsw);

      // -- print member
      jsw.write(jMember.getModifiers().toString());
      jsw.write(' ');

      JType type = jMember.getType();
      String typeName = type.toString();
      // -- for esthetics use short name in some cases
      if (typeName.equals(toString())) {
        typeName = type.getLocalName();
      }
      jsw.write(typeName);
      jsw.write(' ');
      jsw.write(jMember.getName());

      String init = jMember.getInitString();
      if (init != null) {
        jsw.write(" = ");
        jsw.write(init);
      }

      jsw.writeln(';');
      jsw.writeln();
    }

    // -- print constructors
    if (constructors.size() > 0) {
      jsw.writeln();
      jsw.writeln("  //----------------/");
      jsw.writeln(" //- Constructors -/");
      jsw.writeln("//----------------/");
      jsw.writeln();
    }
    for (int i = 0; i < constructors.size(); i++) {
      JConstructor jConstructor = (JConstructor) constructors.elementAt(i);
      jConstructor.print(jsw);
      jsw.writeln();
    }

    // -- print methods
    if (methods.size() > 0) {
      jsw.writeln();
      jsw.writeln("  //-----------/");
      jsw.writeln(" //- Methods -/");
      jsw.writeln("//-----------/");
      jsw.writeln();
    }

    for (int i = 0; i < methods.size(); i++) {
      JMethod jMethod = (JMethod) methods.elementAt(i);
      jMethod.print(jsw);
      jsw.writeln();
    }

    jsw.unindent();
    jsw.writeln('}');
    jsw.flush();
    jsw.close();
  } // -- printSource