Esempio n. 1
0
  private String _removeJavadocFromJava(JavaClass javaClass, String content) {
    Set<Integer> lineNumbers = new HashSet<Integer>();

    lineNumbers.add(_getJavaClassLineNumber(javaClass));

    JavaMethod[] javaMethods = javaClass.getMethods();

    for (JavaMethod javaMethod : javaMethods) {
      lineNumbers.add(javaMethod.getLineNumber());
    }

    JavaField[] javaFields = javaClass.getFields();

    for (JavaField javaField : javaFields) {
      lineNumbers.add(javaField.getLineNumber());
    }

    String[] lines = StringUtil.splitLines(content);

    for (int lineNumber : lineNumbers) {
      if (lineNumber == 0) {
        continue;
      }

      int pos = lineNumber - 2;

      String line = lines[pos];

      if (line == null) {
        continue;
      }

      line = line.trim();

      if (line.endsWith("*/")) {
        while (true) {
          lines[pos] = null;

          if (line.startsWith("/**") || line.startsWith("/*")) {
            break;
          }

          line = lines[--pos].trim();
        }
      }
    }

    StringBundler sb = new StringBundler(content.length());

    for (String line : lines) {
      if (line != null) {
        sb.append(line);
        sb.append("\n");
      }
    }

    return sb.toString().trim();
  }
  private String _getJavaMethodComment(
      String[] lines, Map<String, Element> methodElementsMap, JavaMethod javaMethod) {

    String methodKey = _getMethodKey(javaMethod);

    Element methodElement = methodElementsMap.get(methodKey);

    if (methodElement == null) {
      return null;
    }

    String line = lines[javaMethod.getLineNumber() - 1];

    String indent = StringPool.BLANK;

    for (char c : line.toCharArray()) {
      if (Character.isWhitespace(c)) {
        indent += c;
      } else {
        break;
      }
    }

    StringBuilder sb = new StringBuilder();

    sb.append(indent);
    sb.append("/**\n");
    sb.append(indent);
    sb.append(" * ");
    sb.append(methodElement.elementText("comment"));
    sb.append("\n");
    sb.append(indent);
    sb.append(" *\n");

    _addDocletTags(methodElement, "deprecated", indent, sb);
    _addDocletTags(methodElement, "param", indent, sb);
    _addDocletTags(methodElement, "return", indent, sb);
    _addDocletTags(methodElement, "see", indent, sb);
    _addDocletTags(methodElement, "since", indent, sb);
    _addDocletTags(methodElement, "throws", indent, sb);
    _addDocletTags(methodElement, "version", indent, sb);

    sb.append(indent);
    sb.append(" */\n");

    return sb.toString();
  }
Esempio n. 3
0
  private void _updateJavaFromDocument(
      String fileName, String originalContent, String javadocLessContent, Document document)
      throws Exception {

    String[] lines = StringUtil.splitLines(javadocLessContent);

    JavaClass javaClass = _getJavaClass(fileName, new UnsyncStringReader(javadocLessContent));

    _updateLanguageProperties(document, javaClass.getName());

    List<Tuple> ancestorJavaClassTuples = new ArrayList<Tuple>();

    ancestorJavaClassTuples = _addAncestorJavaClassTuples(javaClass, ancestorJavaClassTuples);

    Element rootElement = document.getRootElement();

    Map<Integer, String> commentsMap = new TreeMap<Integer, String>();

    commentsMap.put(
        _getJavaClassLineNumber(javaClass), _getJavaClassComment(rootElement, javaClass));

    Map<String, Element> methodElementsMap = new HashMap<String, Element>();

    List<Element> methodElements = rootElement.elements("method");

    for (Element methodElement : methodElements) {
      String methodKey = _getMethodKey(methodElement);

      methodElementsMap.put(methodKey, methodElement);
    }

    JavaMethod[] javaMethods = javaClass.getMethods();

    for (JavaMethod javaMethod : javaMethods) {
      if (commentsMap.containsKey(javaMethod.getLineNumber())) {
        continue;
      }

      String javaMethodComment = _getJavaMethodComment(lines, methodElementsMap, javaMethod);

      // Handle override tag insertion

      if (!_hasAnnotation(javaMethod, "Override")) {
        if (_isOverrideMethod(javaClass, javaMethod, ancestorJavaClassTuples)) {

          String overrideLine = _getIndent(lines, javaMethod) + "@Override\n";

          if (Validator.isNotNull(javaMethodComment)) {
            javaMethodComment = javaMethodComment + overrideLine;
          } else {
            javaMethodComment = overrideLine;
          }
        }
      }

      commentsMap.put(javaMethod.getLineNumber(), javaMethodComment);
    }

    Map<String, Element> fieldElementsMap = new HashMap<String, Element>();

    List<Element> fieldElements = rootElement.elements("field");

    for (Element fieldElement : fieldElements) {
      String fieldKey = _getFieldKey(fieldElement);

      fieldElementsMap.put(fieldKey, fieldElement);
    }

    JavaField[] javaFields = javaClass.getFields();

    for (JavaField javaField : javaFields) {
      if (commentsMap.containsKey(javaField.getLineNumber())) {
        continue;
      }

      commentsMap.put(
          javaField.getLineNumber(), _getJavaFieldComment(lines, fieldElementsMap, javaField));
    }

    StringBundler sb = new StringBundler(javadocLessContent.length());

    for (int lineNumber = 1; lineNumber <= lines.length; lineNumber++) {
      String line = lines[lineNumber - 1];

      String comments = commentsMap.get(lineNumber);

      if (comments != null) {
        sb.append(comments);
      }

      sb.append(line);
      sb.append("\n");
    }

    String formattedContent = sb.toString().trim();

    if (!originalContent.equals(formattedContent)) {
      File file = new File(_inputDir + fileName);

      _fileUtil.write(file, formattedContent.getBytes(StringPool.UTF8));

      System.out.println("Writing " + file);
    }
  }
  private void _updateJavaFromJavadoc(String fileName, String oldContent) throws Exception {

    File javadocFile = new File(_basedir + fileName + "doc");

    if (!javadocFile.exists()) {
      return;
    }

    File file = new File(_basedir + fileName);

    if (oldContent == null) {
      oldContent = _fileUtil.read(file);
    }

    String[] lines = StringUtil.splitLines(oldContent);

    JavaClass javaClass = _getJavaClass(fileName, new UnsyncStringReader(oldContent));

    Document document = _saxReaderUtil.read(javadocFile);

    Element rootElement = document.getRootElement();

    Map<Integer, String> commentsMap = new TreeMap<Integer, String>();

    commentsMap.put(javaClass.getLineNumber(), _getJavaClassComment(rootElement, javaClass));

    Map<String, Element> methodElementsMap = new HashMap<String, Element>();

    List<Element> methodElements = rootElement.elements("method");

    for (Element methodElement : methodElements) {
      String methodKey = _getMethodKey(methodElement);

      methodElementsMap.put(methodKey, methodElement);
    }

    JavaMethod[] javaMethods = javaClass.getMethods();

    for (JavaMethod javaMethod : javaMethods) {
      if (commentsMap.containsKey(javaMethod.getLineNumber())) {
        continue;
      }

      commentsMap.put(
          javaMethod.getLineNumber(), _getJavaMethodComment(lines, methodElementsMap, javaMethod));
    }

    Map<String, Element> fieldElementsMap = new HashMap<String, Element>();

    List<Element> fieldElements = rootElement.elements("field");

    for (Element fieldElement : fieldElements) {
      String fieldKey = _getFieldKey(fieldElement);

      fieldElementsMap.put(fieldKey, fieldElement);
    }

    JavaField[] javaFields = javaClass.getFields();

    for (JavaField javaField : javaFields) {
      if (commentsMap.containsKey(javaField.getLineNumber())) {
        continue;
      }

      commentsMap.put(
          javaField.getLineNumber(), _getJavaFieldComment(lines, fieldElementsMap, javaField));
    }

    StringBuilder sb = new StringBuilder(oldContent.length());

    for (int lineNumber = 1; lineNumber <= lines.length; lineNumber++) {
      String line = lines[lineNumber - 1];

      String comments = commentsMap.get(lineNumber);

      if (comments != null) {
        sb.append(comments);
      }

      sb.append(line);
      sb.append("\n");
    }

    String newContent = sb.toString().trim();

    if ((oldContent == null) || !oldContent.equals(newContent)) {
      _fileUtil.write(file, newContent);

      System.out.println("Writing " + file);
    }
  }
  private String _removeJavadocFromJava(String fileName, boolean log) throws Exception {

    File file = new File(_basedir + fileName);

    String oldContent = _fileUtil.read(file);

    String[] lines = StringUtil.splitLines(oldContent);

    JavaClass javaClass = _getJavaClass(fileName, new UnsyncStringReader(oldContent));

    Set<Integer> lineNumbers = new HashSet<Integer>();

    lineNumbers.add(javaClass.getLineNumber());

    JavaMethod[] javaMethods = javaClass.getMethods();

    for (JavaMethod javaMethod : javaMethods) {
      lineNumbers.add(javaMethod.getLineNumber());
    }

    JavaField[] javaFields = javaClass.getFields();

    for (JavaField javaField : javaFields) {
      lineNumbers.add(javaField.getLineNumber());
    }

    for (int lineNumber : lineNumbers) {
      int pos = lineNumber - 2;

      String line = lines[pos].trim();

      if (line.endsWith("*/")) {
        while (true) {
          lines[pos] = null;

          if (line.startsWith("/**")) {
            break;
          }

          line = lines[--pos].trim();
        }
      }
    }

    StringBuilder sb = new StringBuilder(oldContent.length());

    for (String line : lines) {
      if (line != null) {
        sb.append(line);
        sb.append("\n");
      }
    }

    String newContent = sb.toString().trim();

    if ((oldContent == null) || !oldContent.equals(newContent)) {
      _fileUtil.write(file, newContent);

      if (log) {
        System.out.println("Writing " + file);
      }
    }

    return newContent;
  }