public String getFormattedContent(int indentLevel) {
    StringBuilder sb = new StringBuilder();

    for (String javaDocLine : javaDocLines) {
      OutputUtilities.javaIndent(sb, indentLevel);
      sb.append(javaDocLine);
      OutputUtilities.newLine(sb);
    }

    OutputUtilities.javaIndent(sb, indentLevel);

    if (isStatic) {
      sb.append("static "); // $NON-NLS-1$
    }

    sb.append('{');
    indentLevel++;

    ListIterator<String> listIter = bodyLines.listIterator();
    while (listIter.hasNext()) {
      String line = listIter.next();
      if (line.startsWith("}")) { // $NON-NLS-1$
        indentLevel--;
      }

      OutputUtilities.newLine(sb);
      OutputUtilities.javaIndent(sb, indentLevel);
      sb.append(line);

      if ((line.endsWith("{") && !line.startsWith("switch")) // $NON-NLS-1$ //$NON-NLS-2$
          || line.endsWith(":")) { // $NON-NLS-1$
        indentLevel++;
      }

      if (line.startsWith("break")) { // $NON-NLS-1$
        // if the next line is '}', then don't outdent
        if (listIter.hasNext()) {
          String nextLine = listIter.next();
          if (nextLine.startsWith("}")) { // $NON-NLS-1$
            indentLevel++;
          }

          // set back to the previous element
          listIter.previous();
        }
        indentLevel--;
      }
    }

    indentLevel--;
    OutputUtilities.newLine(sb);
    OutputUtilities.javaIndent(sb, indentLevel);
    sb.append('}');

    return sb.toString();
  }
Beispiel #2
0
 /**
  * Adds the formatted annotations.
  *
  * @param sb the sb
  * @param indentLevel the indent level
  */
 public void addFormattedAnnotations(StringBuilder sb, int indentLevel) {
   for (String annotation : annotations) {
     OutputUtilities.javaIndent(sb, indentLevel);
     sb.append(annotation);
     OutputUtilities.newLine(sb);
   }
 }
Beispiel #3
0
 /**
  * Adds the formatted javadoc.
  *
  * @param sb the sb
  * @param indentLevel the indent level
  */
 public void addFormattedJavadoc(StringBuilder sb, int indentLevel) {
   for (String javaDocLine : javaDocLines) {
     OutputUtilities.javaIndent(sb, indentLevel);
     sb.append(javaDocLine);
     OutputUtilities.newLine(sb);
   }
 }
  /**
   * Gets the formatted content.
   *
   * @param indentLevel the indent level
   * @return the formatted content
   */
  public String getFormattedContent(int indentLevel) {
    StringBuilder sb = new StringBuilder();

    addFormattedJavadoc(sb, indentLevel);
    addFormattedAnnotations(sb, indentLevel);

    OutputUtilities.javaIndent(sb, indentLevel);
    sb.append(getVisibility().getValue());

    if (isAbstract()) {
      sb.append("abstract "); // $NON-NLS-1$
    }

    if (isStatic()) {
      sb.append("static "); // $NON-NLS-1$
    }

    if (isFinal()) {
      sb.append("final "); // $NON-NLS-1$
    }

    sb.append("class "); // $NON-NLS-1$
    sb.append(getType().getShortName());

    if (superClass != null) {
      sb.append(" extends "); // $NON-NLS-1$
      sb.append(superClass.getShortName());
    }

    if (superInterfaceTypes.size() > 0) {
      sb.append(" implements "); // $NON-NLS-1$

      boolean comma = false;
      for (FullyQualifiedJavaType fqjt : superInterfaceTypes) {
        if (comma) {
          sb.append(", "); // $NON-NLS-1$
        } else {
          comma = true;
        }

        sb.append(fqjt.getShortName());
      }
    }

    sb.append(" {"); // $NON-NLS-1$
    indentLevel++;

    Iterator<Field> fldIter = fields.iterator();
    while (fldIter.hasNext()) {
      OutputUtilities.newLine(sb);
      Field field = fldIter.next();
      sb.append(field.getFormattedContent(indentLevel));
      if (fldIter.hasNext()) {
        OutputUtilities.newLine(sb);
      }
    }

    if (initializationBlocks.size() > 0) {
      OutputUtilities.newLine(sb);
    }

    Iterator<InitializationBlock> blkIter = initializationBlocks.iterator();
    while (blkIter.hasNext()) {
      OutputUtilities.newLine(sb);
      InitializationBlock initializationBlock = blkIter.next();
      sb.append(initializationBlock.getFormattedContent(indentLevel));
      if (blkIter.hasNext()) {
        OutputUtilities.newLine(sb);
      }
    }

    if (methods.size() > 0) {
      OutputUtilities.newLine(sb);
    }

    Iterator<Method> mtdIter = methods.iterator();
    while (mtdIter.hasNext()) {
      OutputUtilities.newLine(sb);
      Method method = mtdIter.next();
      sb.append(method.getFormattedContent(indentLevel, false));
      if (mtdIter.hasNext()) {
        OutputUtilities.newLine(sb);
      }
    }

    if (innerClasses.size() > 0) {
      OutputUtilities.newLine(sb);
    }
    Iterator<InnerClass> icIter = innerClasses.iterator();
    while (icIter.hasNext()) {
      OutputUtilities.newLine(sb);
      InnerClass innerClass = icIter.next();
      sb.append(innerClass.getFormattedContent(indentLevel));
      if (icIter.hasNext()) {
        OutputUtilities.newLine(sb);
      }
    }

    if (innerEnums.size() > 0) {
      OutputUtilities.newLine(sb);
    }

    Iterator<InnerEnum> ieIter = innerEnums.iterator();
    while (ieIter.hasNext()) {
      OutputUtilities.newLine(sb);
      InnerEnum innerEnum = ieIter.next();
      sb.append(innerEnum.getFormattedContent(indentLevel));
      if (ieIter.hasNext()) {
        OutputUtilities.newLine(sb);
      }
    }

    indentLevel--;
    OutputUtilities.newLine(sb);
    OutputUtilities.javaIndent(sb, indentLevel);
    sb.append('}');

    return sb.toString();
  }