com.thoughtworks.qdox.model.JavaParameter Java Examples

The following examples show how to use com.thoughtworks.qdox.model.JavaParameter. 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: MethodLocation.java    From jaxb2-maven-plugin with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new MethodLocation with the supplied package, class and member names.
 *
 * @param packageName The name of the package for a class potentially holding JavaDoc. Cannot be {@code null}.
 * @param className   The (simple) name of a class. Cannot be null or empty.
 * @param classXmlName  The name given as the {@link XmlType#name()} value of an annotation placed on the Class,
 *                      or {@code  null} if none is provided.
 * @param memberName  The name of a (method or) field. Cannot be null or empty.
 * @param memberXmlName The name given as the {@link XmlElement#name()} or {@link XmlAttribute#name()} value of
 *                      an annotation placed on this Field, or {@code null} if none is provided.
 * @param parameters  The names of the types which are parameters to this method.
 */
public MethodLocation(final String packageName,
        final String className,
        final String classXmlName,
        final String memberName,
        final String memberXmlName,
        final List<JavaParameter> parameters) {

    super(packageName, className, classXmlName, memberName, memberXmlName);

    // Check sanity
    Validate.notNull(parameters, "parameters");

    // Stringify the parameter types
    if (parameters.size() > 0) {
        final StringBuilder builder = new StringBuilder();

        for (JavaParameter current : parameters) {
            builder.append(current.getType().getFullyQualifiedName()).append(PARAMETER_SEPARATOR);
        }
        this.parameters = "(" + builder.substring(0, builder.lastIndexOf(PARAMETER_SEPARATOR)) + ")";
    }
}
 
Example #2
Source File: SpringMVCRequestHeaderHandler.java    From smart-doc with Apache License 2.0 4 votes vote down vote up
/**
 * handle Spring MVC Request Header
 *
 * @param method JavaMethod
 * @return list of ApiReqHeader
 */
public List<ApiReqHeader> handle(JavaMethod method) {
    List<ApiReqHeader> apiReqHeaders = new ArrayList<>();
    for (JavaParameter javaParameter : method.getParameters()) {
        List<JavaAnnotation> javaAnnotations = javaParameter.getAnnotations();
        String className = method.getDeclaringClass().getCanonicalName();
        Map<String, String> paramMap = DocUtil.getParamsComments(method, DocTags.PARAM, className);
        String paramName = javaParameter.getName();
        ApiReqHeader apiReqHeader;
        for (JavaAnnotation annotation : javaAnnotations) {
            String annotationName = annotation.getType().getValue();
            if (SpringMvcAnnotations.REQUEST_HERDER.equals(annotationName)) {
                apiReqHeader = new ApiReqHeader();
                Map<String, Object> requestHeaderMap = annotation.getNamedParameterMap();
                if (requestHeaderMap.get(DocAnnotationConstants.VALUE_PROP) != null) {
                    apiReqHeader.setName(StringUtil.removeQuotes((String) requestHeaderMap.get(DocAnnotationConstants.VALUE_PROP)));
                } else {
                    apiReqHeader.setName(paramName);
                }
                StringBuilder desc = new StringBuilder();
                String comments = paramMap.get(paramName);
                desc.append(comments);

                if (requestHeaderMap.get(DocAnnotationConstants.DEFAULT_VALUE_PROP) != null) {
                    apiReqHeader.setValue(StringUtil.removeQuotes((String) requestHeaderMap.get(DocAnnotationConstants.DEFAULT_VALUE_PROP)));
                    desc.append("(defaultValue: ")
                            .append(StringUtil.removeQuotes((String) requestHeaderMap.get(DocAnnotationConstants.DEFAULT_VALUE_PROP)))
                            .append(")");
                }
                apiReqHeader.setDesc(desc.toString());
                if (requestHeaderMap.get(DocAnnotationConstants.REQUIRED_PROP) != null) {
                    apiReqHeader.setRequired(!Boolean.FALSE.toString().equals(requestHeaderMap.get(DocAnnotationConstants.REQUIRED_PROP)));
                } else {
                    apiReqHeader.setRequired(true);
                }
                String typeName = javaParameter.getType().getValue().toLowerCase();
                apiReqHeader.setType(DocClassUtil.processTypeNameForParams(typeName));
                apiReqHeaders.add(apiReqHeader);
                break;
            }
        }
    }
    return apiReqHeaders;
}
 
Example #3
Source File: MockClassInfo.java    From auto-generate-test-maven-plugin with Apache License 2.0 4 votes vote down vote up
/**
 * 获取方法的信息,参数,方法返回值,参数类型
 * @param javaMethod 方法
 * @param fieldName 调用该方法的变量名
 * @param classType 该方法的类的类型
 * @param superClass 父类
 * @return 方法信息
 */
private static JavaMethodModel getJavaMethodModel(JavaMethod javaMethod, String fieldName,String classType, JavaClass superClass) {
    JavaMethodModel javaMethodModel = new JavaMethodModel();

    javaMethodModel.setFieldName(fieldName);
    javaMethodModel.setClassType(classType);
    javaMethodModel.setName(javaMethod.getName());

    javaMethodModel.setName(javaMethod.getName());
    //方法参数
    List<JavaParameter> javaParameterList = javaMethod.getParameters();
    List<JavaParameteModel> javaParameteModelList = new ArrayList<>();
    for (JavaParameter javaParameter : javaParameterList) {
        JavaParameteModel javaParameteModel = new JavaParameteModel();

        String typeS = javaParameter.getType().getFullyQualifiedName();
        String pType = InitConstant.getAbbreviation(typeS);
        javaParameteModel.setName(javaParameter.getName());
        javaParameteModel.setUpName(javaParameter.getName().substring(0, 1).toUpperCase() + javaParameter.getName().substring(1));
        javaParameteModel.setType(pType);
        javaParameteModel.setFullyType(typeS);
        javaParameteModel.setKeyName("");
        //TODO 设置值
        javaParameteModel.setCustomType(true);
        javaParameteModel.setValue("");

        javaParameteModelList.add(javaParameteModel);
    }

    javaMethodModel.setJavaParameteModelList(javaParameteModelList);

    String rTypeStr = javaMethod.getReturnType().getFullyQualifiedName();
    javaMethodModel.setReturnFullyType(rTypeStr);

    String rType = InitConstant.getAbbreviation(rTypeStr);
    javaMethodModel.setReturnType(rType);

    if (superClass != null) {
        //设置父类类型
        javaMethodModel.setParentClassFullyType(superClass.getFullyQualifiedName());
    }
    return javaMethodModel;
}
 
Example #4
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 #5
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);
    }
}