com.thoughtworks.qdox.model.DocletTag Java Examples

The following examples show how to use com.thoughtworks.qdox.model.DocletTag. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DocUtil.java    From smart-doc with Apache License 2.0 6 votes vote down vote up
/**
 * obtain params comments
 *
 * @param javaMethod JavaMethod
 * @param tagName    java comments tag
 * @param className  class name
 * @return Map
 */
public static Map<String, String> getParamsComments(final JavaMethod javaMethod, final String tagName, final String className) {
    List<DocletTag> paramTags = javaMethod.getTagsByName(tagName);
    Map<String, String> paramTagMap = new HashMap<>();
    for (DocletTag docletTag : paramTags) {
        String value = docletTag.getValue();
        if (StringUtil.isEmpty(value) && StringUtil.isNotEmpty(className)) {
            throw new RuntimeException("ERROR: #" + javaMethod.getName()
                    + "() - bad @" + tagName + " javadoc from " + javaMethod.getDeclaringClass().getCanonicalName() + ", must be add comment if you use it.");
        }
        String pName;
        String pValue;
        int idx = value.indexOf("\n");
        //existed \n
        if (idx > -1) {
            pName = value.substring(0, idx);
            pValue = value.substring(idx + 1);
        } else {
            pName = (value.contains(" ")) ? value.substring(0, value.indexOf(" ")) : value;
            pValue = value.contains(" ") ? value.substring(value.indexOf(' ') + 1) : DocGlobalConstants.NO_COMMENTS_FOUND;
        }
        paramTagMap.put(pName, pValue);
    }
    return paramTagMap;
}
 
Example #2
Source File: OnosSwaggerMojo.java    From onos with Apache License 2.0 6 votes vote down vote up
private void addJsonSchemaDefinition(ObjectNode definitions, DocletTag tag) {
    File definitionsDirectory = new File(srcDirectory + "/src/main/resources/definitions");
    if (tag != null) {
        tag.getParameters().forEach(param -> {
            try {
                File config = new File(definitionsDirectory.getAbsolutePath() + "/"
                                               + param + ".json");
                definitions.putPOJO(param, jsonParser.parse(new FileReader(config)));
            } catch (IOException e) {
                getLog().error(String.format("Could not process %s in %s@%s: %s",
                              tag.getName(), tag.getContext(), tag.getLineNumber(),
                              e.getMessage()));
                throw Throwables.propagate(e);
            }
        });

    }
}
 
Example #3
Source File: OnosSwaggerMojo.java    From onos with Apache License 2.0 6 votes vote down vote up
private void addResponses(ObjectNode methodNode, DocletTag tag, boolean responseJson) {
    ObjectNode responses = mapper.createObjectNode();
    methodNode.set("responses", responses);

    ObjectNode success = mapper.createObjectNode();
    success.put("description", "successful operation");
    responses.set("200", success);
    if (tag != null && responseJson) {
        ObjectNode schema = mapper.createObjectNode();
        tag.getParameters().forEach(
                param -> schema.put("$ref", "#/definitions/" + param));
        success.set("schema", schema);
    }

    ObjectNode defaultObj = mapper.createObjectNode();
    defaultObj.put("description", "Unexpected error");
    responses.set("default", defaultObj);
}
 
Example #4
Source File: DocUtil.java    From smart-doc with Apache License 2.0 5 votes vote down vote up
/**
 * Get field tags
 *
 * @param field        JavaField
 * @param docJavaField DocJavaField
 * @return map
 */
public static Map<String, String> getFieldTagsValue(final JavaField field, DocJavaField docJavaField) {
    List<DocletTag> paramTags = field.getTags();
    if (CollectionUtil.isEmpty(paramTags) && Objects.nonNull(docJavaField)) {
        paramTags = docJavaField.getDocletTags();
    }
    return paramTags.stream().collect(Collectors.toMap(DocletTag::getName, DocletTag::getValue,
            (key1, key2) -> key1 + "," + key2));
}
 
Example #5
Source File: JavaDocData.java    From jaxb2-maven-plugin with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a JavaDocData for a particular entry with the supplied JavaDoc comment and List of DocletTags.
 *
 * @param comment The actual comment in the JavaDoc. Null values are replaced with the value {@code NO_COMMENT},
 *                to ensure that the {@code getComment() } method does not return null values.
 * @param tags    The DocletTags of the JavaDoc entry. Can be null or empty.
 */
public JavaDocData(final String comment, final List<DocletTag> tags) {

    // Assign internal state
    this.comment = comment == null ? NO_COMMENT : comment;
    this.tag2ValueMap = new TreeMap<String, String>();

    // Parse, and assign internal state
    for (DocletTag current : tags) {

        final String tagName = current.getName();
        String tagValue = current.getValue();

        // Handle the case of multi-valued tags, such as
        //
        // @author SomeAuthor
        // @author AnotherAuthor
        //
        // which becomes [author] --> [SomeAuthor, AnotherAuthor]
        String currentValue = tag2ValueMap.get(tagName);
        if (currentValue != null) {
            tagValue = currentValue + ", " + current.getValue();
        }

        tag2ValueMap.put(tagName, tagValue);
    }
}
 
Example #6
Source File: SwaggerGenerator.java    From onos with Apache License 2.0 5 votes vote down vote up
private void addResponses(JavaMethod javaMethod, ObjectNode methodNode, DocletTag tag, boolean responseJson) {
    ObjectNode responses = mapper.createObjectNode();
    methodNode.set("responses", responses);

    Optional<JavaAnnotation> responsesAnnotation = getResponsesAnnotation(javaMethod);

    if (responsesAnnotation.isPresent()) {
        Object annotationsObj = responsesAnnotation.get().getNamedParameter("value");
        if (annotationsObj != null && annotationsObj instanceof List) {
            List<JavaAnnotation> responseAnnotation = (List<JavaAnnotation>) annotationsObj;
            responseAnnotation.forEach(
                    javaAnnotation -> {
                        ObjectNode response = mapper.createObjectNode();
                        response.put("description",
                                String.valueOf(javaAnnotation.getNamedParameter("message"))
                                        .replaceAll("^\"|\"$", ""));
                        responses.set(String.valueOf(javaAnnotation.getNamedParameter("code")), response);
                    }
            );
        }
    } else {
        ObjectNode success = mapper.createObjectNode();
        success.put("description", "successful operation");
        responses.set("200", success);

        ObjectNode defaultObj = mapper.createObjectNode();
        defaultObj.put("description", "Unexpected error");
        responses.set("default", defaultObj);

        if (tag != null && responseJson) {
            ObjectNode schema = mapper.createObjectNode();
            tag.getParameters().stream().forEach(
                    param -> schema.put("$ref", "#/definitions/" + param));
            success.set("schema", schema);
        }
    }
}
 
Example #7
Source File: DocJavaField.java    From smart-doc with Apache License 2.0 4 votes vote down vote up
public List<DocletTag> getDocletTags() {
    if (docletTags == null) {
        return new ArrayList<>();
    }
    return docletTags;
}
 
Example #8
Source File: DocJavaField.java    From smart-doc with Apache License 2.0 4 votes vote down vote up
public DocJavaField setDocletTags(List<DocletTag> docletTags) {
    this.docletTags = docletTags;
    return this;
}
 
Example #9
Source File: SwaggerGenerator.java    From onos with Apache License 2.0 4 votes vote down vote up
private void processRestMethod(JavaMethod javaMethod, String method,
                               Map<String, ObjectNode> pathMap,
                               String resourcePath, ArrayNode tagArray,
                               ObjectNode definitions, File srcDirectory) {
    String fullPath = resourcePath, consumes = "", produces = "",
            comment = javaMethod.getComment();
    DocletTag tag = javaMethod.getTagByName("onos.rsModel");
    for (JavaAnnotation annotation : javaMethod.getAnnotations()) {
        String name = annotation.getType().getName();
        if (name.equals(PATH)) {
            fullPath = resourcePath + "/" + getPath(annotation);
            fullPath = fullPath.replaceFirst("^//", "/");
        }
        if (name.equals(CONSUMES)) {
            consumes = getIOType(annotation);
        }
        if (name.equals(PRODUCES)) {
            produces = getIOType(annotation);
        }
    }
    ObjectNode methodNode = mapper.createObjectNode();
    methodNode.set("tags", tagArray);

    addSummaryDescriptions(methodNode, comment);
    addJsonSchemaDefinition(srcDirectory, definitions, tag);

    processParameters(javaMethod, methodNode, method, tag);

    processConsumesProduces(methodNode, "consumes", consumes);
    processConsumesProduces(methodNode, "produces", produces);
    if (tag == null || !(tag.getParameters().size() > 1)) {
        addResponses(javaMethod, methodNode, tag, false);
    } else {
        addResponses(javaMethod, methodNode, tag, true);
    }

    ObjectNode operations = pathMap.get(fullPath);
    if (operations == null) {
        operations = mapper.createObjectNode();
        operations.set(method, methodNode);
        pathMap.put(fullPath, operations);
    } else {
        operations.set(method, methodNode);
    }
}
 
Example #10
Source File: SwaggerGenerator.java    From onos with Apache License 2.0 4 votes vote down vote up
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.orElse(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)) {
                String description = "";
                if (p.getValue().split(" ", 2).length >= 2) {
                    description = p.getValue().split(" ", 2)[1].trim();
                    if (description.contains("optional")) {
                        required = false;
                    }
                } else {
                    throw new RuntimeException(String.format("No description for parameter \"%s\" in " +
                                                                     "method \"%s\" in %s (line %d)",
                                                             p.getValue(), javaMethod.getName(),
                                                             javaMethod.getDeclaringClass().getName(),
                                                             javaMethod.getLineNumber()));
                }
                individualParameterNode.put("description", description);
            }
        }
        individualParameterNode.put("required", required);
        parameters.add(individualParameterNode);
    }
}
 
Example #11
Source File: OnosSwaggerMojo.java    From onos with Apache License 2.0 4 votes vote down vote up
private void processRestMethod(JavaMethod javaMethod, String method,
                               Map<String, ObjectNode> pathMap,
                               String resourcePath, ArrayNode tagArray, ObjectNode definitions) {
    String fullPath = resourcePath, consumes = "", produces = "",
            comment = javaMethod.getComment();
    DocletTag tag = javaMethod.getTagByName("onos.rsModel");
    for (JavaAnnotation annotation : javaMethod.getAnnotations()) {
        String name = annotation.getType().getName();
        if (name.equals(PATH)) {
            fullPath = resourcePath + "/" + getPath(annotation);
            fullPath = fullPath.replaceFirst("^//", "/");
        }
        if (name.equals(CONSUMES)) {
            consumes = getIOType(annotation);
        }
        if (name.equals(PRODUCES)) {
            produces = getIOType(annotation);
        }
    }
    ObjectNode methodNode = mapper.createObjectNode();
    methodNode.set("tags", tagArray);

    addSummaryDescriptions(methodNode, comment);
    addJsonSchemaDefinition(definitions, tag);

    processParameters(javaMethod, methodNode, method, tag);

    processConsumesProduces(methodNode, "consumes", consumes);
    processConsumesProduces(methodNode, "produces", produces);
    if (tag == null || ((method.toLowerCase().equals("post") || method.toLowerCase().equals("put"))
            && !(tag.getParameters().size() > 1))) {
        addResponses(methodNode, tag, false);
    } else {
        addResponses(methodNode, tag, true);
    }

    ObjectNode operations = pathMap.get(fullPath);
    if (operations == null) {
        operations = mapper.createObjectNode();
        operations.set(method, methodNode);
        pathMap.put(fullPath, operations);
    } else {
        operations.set(method, methodNode);
    }
}
 
Example #12
Source File: OnosSwaggerMojo.java    From onos with Apache License 2.0 4 votes vote down vote up
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.orElse(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().forEach(param -> {
                    schema.put("$ref", "#/definitions/" + param);
                });
                individualParameterNode.set("schema", schema);
            }
        }
        for (DocletTag p : javaMethod.getTagsByName("param")) {
            if (p.getValue().contains(annotationName)) {
                String description = "";
                if (p.getValue().split(" ", 2).length >= 2) {
                    description = p.getValue().split(" ", 2)[1].trim();
                    if (description.contains("optional")) {
                        required = false;
                    }
                } else {
                    getLog().warn(String.format(
                                "No description for parameter \"%s\" in " +
                                "method \"%s\" in %s (line %d)",
                                  p.getValue(), javaMethod.getName(),
                                  javaMethod.getDeclaringClass().getName(),
                                  javaMethod.getLineNumber()));
                }
                individualParameterNode.put("description", description);
            }
        }
        individualParameterNode.put("required", required);
        parameters.add(individualParameterNode);
    }
}