Exemplo n.º 1
0
  @Override
  public void execute() throws MojoExecutionException {
    try {
      JavaProjectBuilder builder = new JavaProjectBuilder();
      builder.addSourceTree(new File(srcDirectory, "src/main/java"));

      ObjectNode root = initializeRoot();
      ArrayNode tags = mapper.createArrayNode();
      ObjectNode paths = mapper.createObjectNode();

      root.set("tags", tags);
      root.set("paths", paths);

      builder.getClasses().forEach(jc -> processClass(jc, paths, tags));

      if (paths.size() > 0) {
        getLog().info("Generating ONOS REST API documentation...");
        genCatalog(root);

        if (!isNullOrEmpty(apiPackage)) {
          genRegistrator();
        }
      }

      project.addCompileSourceRoot(new File(dstDirectory, GEN_SRC).getPath());

    } catch (Exception e) {
      getLog().warn("Unable to generate ONOS REST API documentation", e);
      throw e;
    }
  }
  @Test
  public void generateDotClassDiagram() throws IOException {
    JavaProjectBuilder javaProjectBuilder = new JavaProjectBuilder();
    javaProjectBuilder.addSourceTree(new File("./src/main/java/me/loki2302/"));

    StringBuilder stringBuilder = new StringBuilder();

    stringBuilder.append("digraph g { overlap = false; node [shape=rectangle;];\n");

    int clusterIdx = 0;
    for (JavaPackage javaPackage : javaProjectBuilder.getPackages()) {
      stringBuilder.append(String.format("subgraph cluster_%d {\n", clusterIdx++));
      stringBuilder.append(String.format("label=\"%s\";\n", javaPackage.getName()));
      for (JavaClass javaClass : javaPackage.getClasses()) {
        stringBuilder.append(String.format("%s;\n", simpleName(javaClass.getName())));
      }
      stringBuilder.append("}\n");
    }

    for (JavaClass javaClass : javaProjectBuilder.getClasses()) {
      // inheritance
      JavaClass superClass = javaClass.getSuperJavaClass();
      if (superClass != null) {
        if (!shouldIgnore(superClass)) {
          stringBuilder.append(
              String.format(
                  "%s -> %s;\n",
                  simpleName(javaClass.getName()), simpleName(superClass.getName())));
        }
      }

      for (JavaClass i : javaClass.getInterfaces()) {
        if (shouldIgnore(i)) {
          continue;
        }

        stringBuilder.append(
            String.format("%s -> %s;\n", simpleName(javaClass.getName()), simpleName(i.getName())));
      }

      // delegation
      List<JavaField> fields = javaClass.getFields();
      for (JavaField field : fields) {
        if (shouldIgnore(field.getType())) {
          continue;
        }

        stringBuilder.append(
            String.format(
                "%s -> %s;\n",
                simpleName(javaClass.getName()), simpleName(field.getType().getName())));
      }
    }

    stringBuilder.append("}\n");

    documentation.text("classDiagram", stringBuilder.toString());
  }
  @Test
  public void generatePlantUmlClassDiagram() throws IOException {
    JavaProjectBuilder javaProjectBuilder = new JavaProjectBuilder();
    javaProjectBuilder.addSourceTree(new File("./src/main/java"));

    StringBuilder stringBuilder = new StringBuilder();

    for (JavaClass javaClass : javaProjectBuilder.getClasses()) {
      if (shouldIgnore(javaClass)) {
        continue;
      }

      String classType;
      if (javaClass.isInterface()) {
        classType = "interface";
      } else if (javaClass.isAbstract()) {
        classType = "abstract class";
      } else {
        classType = "class";
      }

      stringBuilder.append(String.format("%s %s", classType, javaClass.getFullyQualifiedName()));

      // extends
      JavaClass superClass = javaClass.getSuperJavaClass();
      if (superClass != null) {
        if (!shouldIgnore(superClass)) {
          stringBuilder.append(String.format(" extends %s", superClass.getFullyQualifiedName()));
        }
      }

      // implements
      List<JavaClass> interfaces = javaClass.getInterfaces();
      if (!interfaces.isEmpty()) {
        List<String> interfaceNames =
            interfaces
                .stream()
                .filter(i -> !shouldIgnore(i))
                .map(i -> i.getFullyQualifiedName())
                .collect(Collectors.toList());
        String implementsString = " implements " + String.join(", ", interfaceNames);
        stringBuilder.append(implementsString);
      }

      stringBuilder.append("\n");
    }

    for (JavaClass javaClass : javaProjectBuilder.getClasses()) {
      if (shouldIgnore(javaClass)) {
        continue;
      }

      // delegation
      List<JavaField> fields = javaClass.getFields();
      for (JavaField field : fields) {
        if (shouldIgnore(field.getType())) {
          continue;
        }

        stringBuilder.append(
            String.format(
                "%s -> %s: delegates\n",
                javaClass.getFullyQualifiedName(), field.getType().getFullyQualifiedName()));
      }
    }

    documentation.text("classDiagram", stringBuilder.toString());
  }