コード例 #1
0
 /**
  * Get ITestNGMethod author(s) string, or class author(s) if no method author is present. Default
  * return value is "unknown".
  *
  * @param className
  * @param method
  * @return
  * @author hzjingcheng
  */
 private String getAuthors(String className, ITestNGMethod method) {
   JavaClass cls = builder.getClassByName(className);
   DocletTag[] authors = cls.getTagsByName("author");
   // get class authors as default author name
   String allAuthors = "";
   if (authors.length == 0) {
     allAuthors = "unknown";
   } else {
     for (DocletTag author : authors) {
       allAuthors += author.getValue() + " ";
     }
   }
   // get method author name
   JavaMethod[] mtds = cls.getMethods();
   for (JavaMethod mtd : mtds) {
     if (mtd.getName().equals(method.getMethodName())) {
       authors = mtd.getTagsByName("author");
       if (authors.length != 0) {
         allAuthors = "";
         for (DocletTag author : authors) {
           allAuthors += author.getValue() + " ";
         }
       }
       break;
     }
   }
   return allAuthors.trim();
 }
コード例 #2
0
ファイル: DocletDocumenter.java プロジェクト: djalexd/TGIRest
 private String extractParameterValue(DocletTag javaParameterTag, String name) {
   if (!javaParameterTag.getValue().startsWith(name + " ")) {
     return javaParameterTag.getValue();
   } else {
     return javaParameterTag.getValue().substring(name.length() + 1);
   }
 }
コード例 #3
0
ファイル: OnosSwaggerMojo.java プロジェクト: rvhub/onos
  // Processes parameters of javaMethod and enters the proper key-values into the methodNode
  private void processParameters(JavaMethod javaMethod, ObjectNode methodNode) {
    ArrayNode parameters = mapper.createArrayNode();
    methodNode.set("parameters", parameters);
    boolean required = true;

    for (JavaParameter javaParameter : javaMethod.getParameters()) {
      ObjectNode individualParameterNode = mapper.createObjectNode();
      Optional<JavaAnnotation> optional =
          javaParameter
              .getAnnotations()
              .stream()
              .filter(
                  annotation ->
                      annotation.getType().getName().equals(PATH_PARAM)
                          || annotation.getType().getName().equals(QUERY_PARAM))
              .findAny();
      JavaAnnotation pathType = optional.isPresent() ? optional.get() : null;

      String annotationName = javaParameter.getName();

      if (pathType != null) { // the parameter is a path or query parameter
        individualParameterNode.put(
            "name", pathType.getNamedParameter("value").toString().replace("\"", ""));
        if (pathType.getType().getName().equals(PATH_PARAM)) {
          individualParameterNode.put("in", "path");
        } else if (pathType.getType().getName().equals(QUERY_PARAM)) {
          individualParameterNode.put("in", "query");
        }
        individualParameterNode.put("type", getType(javaParameter.getType()));
      } else { // the parameter is a body parameter
        individualParameterNode.put("name", annotationName);
        individualParameterNode.put("in", "body");

        // TODO add actual hardcoded schemas and a type
        // body parameters must have a schema associated with them
        ArrayNode schema = mapper.createArrayNode();
        individualParameterNode.set("schema", schema);
      }
      for (DocletTag p : javaMethod.getTagsByName("param")) {
        if (p.getValue().contains(annotationName)) {
          try {
            String description = p.getValue().split(" ", 2)[1].trim();
            if (description.contains("optional")) {
              required = false;
            }
            individualParameterNode.put("description", description);
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
      }
      individualParameterNode.put("required", required);
      parameters.add(individualParameterNode);
    }
  }
コード例 #4
0
  private void _addReturnElement(Element methodElement, JavaMethod javaMethod) throws Exception {

    Type returns = javaMethod.getReturns();

    if (returns == null) {
      return;
    }

    String returnsValue = returns.getValue();

    if (returnsValue.equals("void")) {
      return;
    }

    Element returnElement = methodElement.addElement("return");

    DocletTag[] returnDocletTags = javaMethod.getTagsByName("return");

    String comment = StringPool.BLANK;

    if (returnDocletTags.length > 0) {
      DocletTag returnDocletTag = returnDocletTags[0];

      comment = GetterUtil.getString(returnDocletTag.getValue());

      DocUtil.add(returnElement, "required", true);
    }

    comment = _trimMultilineText(comment);

    Element commentElement = returnElement.addElement("comment");

    commentElement.addCDATA(comment);
  }
コード例 #5
0
  private void _addDocletElements(
      Element parentElement, AbstractJavaEntity abstractJavaEntity, String name) {

    DocletTag[] docletTags = abstractJavaEntity.getTagsByName(name);

    for (DocletTag docletTag : docletTags) {
      String value = docletTag.getValue();

      if (name.equals("author")
          || name.equals("see")
          || name.equals("since")
          || name.equals("version")) {

        /*if (value.startsWith("Raymond Aug")) {
        	value = "Raymond Aug\u00c3\u00a9";
        }*/

        DocUtil.add(parentElement, name, value);
      } else {
        Element element = parentElement.addElement(name);

        element.addCDATA(value);
      }
    }
  }
コード例 #6
0
  private void _addThrowsElement(
      Element methodElement, Type exception, DocletTag[] throwsDocletTags) {

    String name = exception.getJavaClass().getName();
    String value = null;

    for (DocletTag throwsDocletTag : throwsDocletTags) {
      String curValue = throwsDocletTag.getValue();

      if (!curValue.startsWith(name)) {
        continue;
      } else {
        curValue = value;

        break;
      }
    }

    Element throwsElement = methodElement.addElement("throws");

    DocUtil.add(throwsElement, "name", name);
    DocUtil.add(throwsElement, "type", exception.getValue());

    if (value != null) {
      value = value.substring(name.length());
    }

    Element commentElement = throwsElement.addElement("comment");

    commentElement.addCDATA(_getCDATA(value));
  }
コード例 #7
0
  private void _addParamElement(
      Element methodElement, JavaParameter javaParameter, DocletTag[] paramDocletTags) {

    String name = javaParameter.getName();
    String type = javaParameter.getType().getValue();
    String value = null;

    for (DocletTag paramDocletTag : paramDocletTags) {
      String curValue = paramDocletTag.getValue();

      if (!curValue.startsWith(name)) {
        continue;
      } else {
        curValue = value;

        break;
      }
    }

    Element paramElement = methodElement.addElement("param");

    DocUtil.add(paramElement, "name", name);
    DocUtil.add(paramElement, "type", type);

    if (value != null) {
      value = value.substring(name.length());
    }

    Element commentElement = paramElement.addElement("comment");

    commentElement.addCDATA(_getCDATA(value));
  }
コード例 #8
0
  private void _addDocletElements(
      Element parentElement, AbstractJavaEntity abstractJavaEntity, String name) throws Exception {

    DocletTag[] docletTags = abstractJavaEntity.getTagsByName(name);

    for (DocletTag docletTag : docletTags) {
      String value = docletTag.getValue();

      value = _trimMultilineText(value);

      value = StringUtil.replace(value, " </", "</");

      Element element = parentElement.addElement(name);

      element.addCDATA(value);
    }

    if ((docletTags.length == 0) && name.equals("author")) {
      Element element = parentElement.addElement(name);

      element.addCDATA(ServiceBuilder.AUTHOR);
    }
  }
コード例 #9
0
ファイル: OnosSwaggerMojo.java プロジェクト: wangqilei/onos
  // Processes parameters of javaMethod and enters the proper key-values into the methodNode
  private void processParameters(
      JavaMethod javaMethod, ObjectNode methodNode, String method, DocletTag tag) {
    ArrayNode parameters = mapper.createArrayNode();
    methodNode.set("parameters", parameters);
    boolean required = true;

    for (JavaParameter javaParameter : javaMethod.getParameters()) {
      ObjectNode individualParameterNode = mapper.createObjectNode();
      Optional<JavaAnnotation> optional =
          javaParameter
              .getAnnotations()
              .stream()
              .filter(
                  annotation ->
                      annotation.getType().getName().equals(PATH_PARAM)
                          || annotation.getType().getName().equals(QUERY_PARAM))
              .findAny();
      JavaAnnotation pathType = optional.isPresent() ? optional.get() : null;

      String annotationName = javaParameter.getName();

      if (pathType != null) { // the parameter is a path or query parameter
        individualParameterNode.put(
            "name", pathType.getNamedParameter("value").toString().replace("\"", ""));
        if (pathType.getType().getName().equals(PATH_PARAM)) {
          individualParameterNode.put("in", "path");
        } else if (pathType.getType().getName().equals(QUERY_PARAM)) {
          individualParameterNode.put("in", "query");
        }
        individualParameterNode.put("type", getType(javaParameter.getType()));
      } else { // the parameter is a body parameter
        individualParameterNode.put("name", annotationName);
        individualParameterNode.put("in", "body");

        // Adds the reference to the Json model for the input
        // that goes in the post or put operation
        if (tag != null
            && (method.toLowerCase().equals("post") || method.toLowerCase().equals("put"))) {
          ObjectNode schema = mapper.createObjectNode();
          tag.getParameters()
              .stream()
              .forEach(
                  param -> {
                    schema.put("$ref", "#/definitions/" + param);
                  });
          individualParameterNode.set("schema", schema);
        }
      }
      for (DocletTag p : javaMethod.getTagsByName("param")) {
        if (p.getValue().contains(annotationName)) {
          try {
            String description = p.getValue().split(" ", 2)[1].trim();
            if (description.contains("optional")) {
              required = false;
            }
            individualParameterNode.put("description", description);
          } catch (Exception e) {
            e.printStackTrace();
          }
        }
      }
      individualParameterNode.put("required", required);
      parameters.add(individualParameterNode);
    }
  }