Ejemplo n.º 1
0
  private void buildResponseErrors(Method method, MethodDocument document, JavaMethod javaMethod) {
    final DocletTag[] errorStatuses = javaMethod.getTagsByName("error.status");
    final DocletTag[] errorCauses = javaMethod.getTagsByName("error.cause");
    int totalErrors = errorCauses.length;
    if (errorStatuses.length != errorCauses.length) {
      LOG.warn(
          "For method {}, the number of tags @error.status is different than the"
              + " number of tags @error.cause; Because of this, the {} will only use the minimum"
              + " count from the two.",
          method.getName(),
          this.getClass().getName());
      totalErrors = Math.min(errorCauses.length, errorStatuses.length);
    }

    List<ResponseError> errors = new ArrayList<ResponseError>(totalErrors);
    for (int i = 0; i < totalErrors; i++) {
      try {
        errors.add(
            new ResponseError(
                Integer.parseInt(errorStatuses[i].getValue()), errorCauses[i].getValue()));
      } catch (NumberFormatException e) {
        LOG.error("Invalid number for @error.status: " + errorStatuses[i].getValue());
      }
    }

    document.setResponseErrors(ImmutableList.copyOf(errors));
  }
Ejemplo n.º 2
0
  private void buildQueryAndPathDocuments(
      Method method, JavaMethod javaMethod, MethodDocument document) {
    if (method.getParameterTypes().length != javaMethod.getParameters().length) {
      LOG.error(
          "Unable to generate argument documentation: number of method arguments is not "
              + "the same as number of @param found in javadoc.");
      return;
    }

    List<ParamDocument> queryParams = new ArrayList<ParamDocument>();
    List<ParamDocument> pathParams = new ArrayList<ParamDocument>();

    Annotation[][] annotations = method.getParameterAnnotations();
    for (int i = 0; i < annotations.length; i++) {
      JavaParameter javaParameter = javaMethod.getParameters()[i];
      DocletTag javaParameterTag = javaMethod.getTagsByName("param")[i];

      // Check annotations at parameter i
      for (Annotation a : annotations[i]) {
        if (a.annotationType() == QueryParam.class) {
          queryParams.add(
              new ParamDocument(
                  javaParameter.getName(),
                  extractParameterValue(javaParameterTag, javaParameter.getName())));
        } else if (a.annotationType() == PathParam.class) {
          pathParams.add(
              new ParamDocument(
                  javaParameter.getName(),
                  extractParameterValue(javaParameterTag, javaParameter.getName())));
        }
      }
    }

    LOG.info("Found {} query params for method {}", queryParams.size(), method.getName());
    LOG.info("Found {}  path params for method {}", pathParams.size(), method.getName());
    document.setQueryParams(ImmutableList.copyOf(queryParams));
    document.setPathParams(ImmutableList.copyOf(pathParams));
  }
Ejemplo n.º 3
0
  private void buildExamples(JavaMethod javaMethod, MethodDocument document) {
    final DocletTag[] exampleTitles = javaMethod.getTagsByName("example.title");
    final DocletTag[] exampleDescriptions = javaMethod.getTagsByName("example.description");
    final DocletTag[] exampleRequests = javaMethod.getTagsByName("example.request");
    final DocletTag[] exampleRequestsContentType =
        javaMethod.getTagsByName("example.requestContentType");
    final DocletTag[] exampleResponses = javaMethod.getTagsByName("example.response");
    final DocletTag[] exampleResponsesContentType =
        javaMethod.getTagsByName("example.responseContentType");

    if ((exampleTitles.length != exampleDescriptions.length)
        || (exampleTitles.length != exampleRequests.length)
        || (exampleTitles.length != exampleRequestsContentType.length)
        || (exampleTitles.length != exampleResponses.length)
        || (exampleTitles.length != exampleResponsesContentType.length)) {
      LOG.error(
          "Unable to build examples. The number of tags "
              + "example/title,description,request,requestContentType,response,responseContentType "
              + "must be equal.");
      return;
    }

    List<ApiExampleDocument> examples = new ArrayList<ApiExampleDocument>();
    for (int i = 0; i < exampleTitles.length; i++) {
      ApiExampleDocument example =
          new ApiExampleDocument(
              exampleTitles[i].getValue(),
              exampleDescriptions[i].getValue(),
              exampleRequests[i].getValue(),
              exampleRequestsContentType[i].getValue(),
              exampleResponses[i].getValue(),
              exampleResponsesContentType[i].getValue());
      LOG.info("Added new example for {}: {}", document.getPath(), example);
      examples.add(example);
    }

    document.setExampleDocuments(ImmutableList.copyOf(examples));
  }
Ejemplo n.º 4
0
 private void buildDescription(MethodDocument document, JavaMethod javaMethod) {
   document.setDescription(javaMethod.getComment());
 }