コード例 #1
0
ファイル: JInterface.java プロジェクト: sonatype/modello
  /**
   * Prints the source code for this JInterface to the given JSourceWriter
   *
   * @param jsw the JSourceWriter to print to. [May not be null]
   */
  public void print(JSourceWriter jsw, boolean classOnly) {

    if (jsw == null) {
      throw new IllegalArgumentException("argument 'jsw' should not be null.");
    }

    StringBuilder buffer = new StringBuilder();

    if (!classOnly) {
      printHeader(jsw);
      printPackageDeclaration(jsw);
      printImportDeclarations(jsw);
    }

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

    getJDocComment().print(jsw);

    JAnnotations annotations = getAnnotations();
    if (annotations != null) annotations.print(jsw);

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

    buffer.setLength(0);

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

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

    buffer.append("interface ");
    buffer.append(getLocalName());
    jsw.writeln(buffer.toString());
    buffer.setLength(0);
    jsw.indent();

    if (getInterfaceCount() > 0) {
      Enumeration<String> e = getInterfaces();
      buffer.append("extends ");
      while (e.hasMoreElements()) {
        buffer.append(e.nextElement());
        if (e.hasMoreElements()) buffer.append(", ");
      }

      jsw.writeln(buffer.toString());
      buffer.setLength(0);
    }

    jsw.unindent();

    jsw.writeln('{');

    jsw.indent();

    // -- declare static members

    if (fields != null) {
      if (fields.size() > 0) {
        jsw.writeln();
        jsw.writeln("  //--------------------------/");
        jsw.writeln(" //- Class/Member Variables -/");
        jsw.writeln("//--------------------------/");
        jsw.writeln();
      }

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

        JField jField = (JField) fields.get(i);

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

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

        JType type = jField.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(jField.getName());

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

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

    // -- print method signatures

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

    for (int i = 0; i < methods.size(); i++) {
      JMethodSignature signature = (JMethodSignature) methods.elementAt(i);
      signature.print(jsw);
      jsw.writeln(';');
    }

    for (String sourceCodeEntry : sourceCodeEntries) {
      jsw.writeln(sourceCodeEntry);
    }

    jsw.unindent();
    jsw.writeln('}');
    jsw.flush();
    jsw.close();
  } // -- printSource
コード例 #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