fix null point error

This commit is contained in:
oppofind 2020-10-24 13:45:53 +08:00
parent 1a9a64eb03
commit 50cfffc80e
1 changed files with 145 additions and 144 deletions

View File

@ -1,144 +1,145 @@
/* /*
* smart-doc https://github.com/shalousun/smart-doc * smart-doc https://github.com/shalousun/smart-doc
* *
* Copyright (C) 2018-2020 smart-doc * Copyright (C) 2018-2020 smart-doc
* *
* Licensed to the Apache Software Foundation (ASF) under one * Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file * or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information * distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file * regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the * to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance * "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at * with the License. You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, * Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an * software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the * KIND, either express or implied. See the License for the
* specific language governing permissions and limitations * specific language governing permissions and limitations
* under the License. * under the License.
*/ */
package com.power.doc.handler; package com.power.doc.handler;
import com.power.common.util.StringUtil; import com.power.common.util.StringUtil;
import com.power.doc.constants.*; import com.power.doc.constants.DocAnnotationConstants;
import com.power.doc.model.ApiReqHeader; import com.power.doc.constants.DocTags;
import com.power.doc.utils.DocClassUtil; import com.power.doc.constants.SpringMvcAnnotations;
import com.power.doc.utils.DocUtil; import com.power.doc.model.ApiReqHeader;
import com.thoughtworks.qdox.model.JavaAnnotation; import com.power.doc.utils.DocClassUtil;
import com.thoughtworks.qdox.model.JavaMethod; import com.power.doc.utils.DocUtil;
import com.thoughtworks.qdox.model.JavaParameter; import com.thoughtworks.qdox.model.JavaAnnotation;
import com.thoughtworks.qdox.model.expression.AnnotationValue; import com.thoughtworks.qdox.model.JavaMethod;
import com.thoughtworks.qdox.model.JavaParameter;
import java.util.*;
import java.util.stream.Collectors; import java.util.*;
import java.util.stream.Stream; import java.util.stream.Collectors;
import java.util.stream.Stream;
/**
* @author yu 2019/12/22. /**
*/ * @author yu 2019/12/22.
public class SpringMVCRequestHeaderHandler { */
public class SpringMVCRequestHeaderHandler {
/**
* handle Spring MVC Request Header /**
* * handle Spring MVC Request Header
* @param method JavaMethod *
* @return list of ApiReqHeader * @param method JavaMethod
*/ * @return list of ApiReqHeader
public List<ApiReqHeader> handle(JavaMethod method) { */
List<ApiReqHeader> mappingHeaders = new ArrayList<>(); public List<ApiReqHeader> handle(JavaMethod method) {
List<JavaAnnotation> annotations = method.getAnnotations(); List<ApiReqHeader> mappingHeaders = new ArrayList<>();
for (JavaAnnotation annotation : annotations) { List<JavaAnnotation> annotations = method.getAnnotations();
String annotationName = annotation.getType().getName(); for (JavaAnnotation annotation : annotations) {
if (!isMapping(annotationName)) { String annotationName = annotation.getType().getValue();
continue; Object headersObject = annotation.getNamedParameter("headers");
} if (!isMapping(annotationName) || Objects.isNull(headersObject)) {
Object headersObject = annotation.getNamedParameter("headers"); continue;
List<String> headers = (LinkedList) headersObject; }
for (String str : headers) { List<String> headers = (LinkedList) headersObject;
String header = StringUtil.removeQuotes(str); for (String str : headers) {
if (header.startsWith("!")) { String header = StringUtil.removeQuotes(str);
continue; if (header.startsWith("!")) {
} continue;
if (header.contains("!=")) { }
String headerName = header.substring(0, header.indexOf("!")); if (header.contains("!=")) {
ApiReqHeader apiReqHeader = ApiReqHeader.builder().setName(headerName) String headerName = header.substring(0, header.indexOf("!"));
.setRequired(true).setValue(null).setDesc(header).setType("string"); ApiReqHeader apiReqHeader = ApiReqHeader.builder().setName(headerName)
mappingHeaders.add(apiReqHeader); .setRequired(true).setValue(null).setDesc(header).setType("string");
} else { mappingHeaders.add(apiReqHeader);
String headerName; } else {
String headerValue = null; String headerName;
if (header.contains("=")) { String headerValue = null;
int index = header.indexOf("="); if (header.contains("=")) {
headerName = header.substring(0, header.indexOf("=")); int index = header.indexOf("=");
headerValue = header.substring(index + 1); headerName = header.substring(0, header.indexOf("="));
} else { headerValue = header.substring(index + 1);
headerName = header; } else {
} headerName = header;
ApiReqHeader apiReqHeader = ApiReqHeader.builder().setName(headerName) }
.setRequired(true).setValue(headerValue).setDesc(header).setType("string"); ApiReqHeader apiReqHeader = ApiReqHeader.builder().setName(headerName)
mappingHeaders.add(apiReqHeader); .setRequired(true).setValue(headerValue).setDesc(header).setType("string");
} mappingHeaders.add(apiReqHeader);
} }
} }
List<ApiReqHeader> reqHeaders = new ArrayList<>(); }
for (JavaParameter javaParameter : method.getParameters()) { List<ApiReqHeader> reqHeaders = new ArrayList<>();
List<JavaAnnotation> javaAnnotations = javaParameter.getAnnotations(); for (JavaParameter javaParameter : method.getParameters()) {
String className = method.getDeclaringClass().getCanonicalName(); List<JavaAnnotation> javaAnnotations = javaParameter.getAnnotations();
Map<String, String> paramMap = DocUtil.getParamsComments(method, DocTags.PARAM, className); String className = method.getDeclaringClass().getCanonicalName();
String paramName = javaParameter.getName(); Map<String, String> paramMap = DocUtil.getParamsComments(method, DocTags.PARAM, className);
ApiReqHeader apiReqHeader; String paramName = javaParameter.getName();
for (JavaAnnotation annotation : javaAnnotations) { ApiReqHeader apiReqHeader;
String annotationName = annotation.getType().getValue(); for (JavaAnnotation annotation : javaAnnotations) {
if (SpringMvcAnnotations.REQUEST_HERDER.equals(annotationName)) { String annotationName = annotation.getType().getValue();
apiReqHeader = new ApiReqHeader(); if (SpringMvcAnnotations.REQUEST_HERDER.equals(annotationName)) {
Map<String, Object> requestHeaderMap = annotation.getNamedParameterMap(); apiReqHeader = new ApiReqHeader();
if (requestHeaderMap.get(DocAnnotationConstants.VALUE_PROP) != null) { Map<String, Object> requestHeaderMap = annotation.getNamedParameterMap();
apiReqHeader.setName(StringUtil.removeQuotes((String) requestHeaderMap.get(DocAnnotationConstants.VALUE_PROP))); if (requestHeaderMap.get(DocAnnotationConstants.VALUE_PROP) != null) {
} else { apiReqHeader.setName(StringUtil.removeQuotes((String) requestHeaderMap.get(DocAnnotationConstants.VALUE_PROP)));
apiReqHeader.setName(paramName); } else {
} apiReqHeader.setName(paramName);
StringBuilder desc = new StringBuilder(); }
String comments = paramMap.get(paramName); StringBuilder desc = new StringBuilder();
desc.append(comments); 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))); if (requestHeaderMap.get(DocAnnotationConstants.DEFAULT_VALUE_PROP) != null) {
desc.append("(defaultValue: ") apiReqHeader.setValue(StringUtil.removeQuotes((String) requestHeaderMap.get(DocAnnotationConstants.DEFAULT_VALUE_PROP)));
.append(StringUtil.removeQuotes((String) requestHeaderMap.get(DocAnnotationConstants.DEFAULT_VALUE_PROP))) desc.append("(defaultValue: ")
.append(")"); .append(StringUtil.removeQuotes((String) requestHeaderMap.get(DocAnnotationConstants.DEFAULT_VALUE_PROP)))
} .append(")");
apiReqHeader.setDesc(desc.toString()); }
if (requestHeaderMap.get(DocAnnotationConstants.REQUIRED_PROP) != null) { apiReqHeader.setDesc(desc.toString());
apiReqHeader.setRequired(!Boolean.FALSE.toString().equals(requestHeaderMap.get(DocAnnotationConstants.REQUIRED_PROP))); if (requestHeaderMap.get(DocAnnotationConstants.REQUIRED_PROP) != null) {
} else { apiReqHeader.setRequired(!Boolean.FALSE.toString().equals(requestHeaderMap.get(DocAnnotationConstants.REQUIRED_PROP)));
apiReqHeader.setRequired(true); } else {
} apiReqHeader.setRequired(true);
String typeName = javaParameter.getType().getValue().toLowerCase(); }
apiReqHeader.setType(DocClassUtil.processTypeNameForParams(typeName)); String typeName = javaParameter.getType().getValue().toLowerCase();
reqHeaders.add(apiReqHeader); apiReqHeader.setType(DocClassUtil.processTypeNameForParams(typeName));
break; reqHeaders.add(apiReqHeader);
} break;
} }
} }
List<ApiReqHeader> allApiReqHeaders = Stream.of(mappingHeaders, reqHeaders) }
.flatMap(Collection::stream).distinct().collect(Collectors.toList()); List<ApiReqHeader> allApiReqHeaders = Stream.of(mappingHeaders, reqHeaders)
return allApiReqHeaders; .flatMap(Collection::stream).distinct().collect(Collectors.toList());
} return allApiReqHeaders;
}
private boolean isMapping(String annotationName) {
switch (annotationName) { private boolean isMapping(String annotationName) {
case "GetMapping": switch (annotationName) {
case "RequestMapping": case "GetMapping":
case "PostMapping": case "RequestMapping":
case "PutMapping": case "PostMapping":
case "PatchMapping": case "PutMapping":
case "DeleteMapping": case "PatchMapping":
return true; case "DeleteMapping":
default: return true;
return false; default:
} return false;
} }
} }
}