com.lq.doc.present.LoadController Maven / Gradle / Ivy
package com.lq.doc.present;
import com.lq.comment.GlobArgs;
import com.lq.comment.util.ClassUtil;
import com.lq.comment.util.FileUtil;
import com.lq.comment.util.StringUtil;
import com.lq.doc.entity.ApiRequestEntity;
import com.lq.doc.entity.ParamEntity;
import com.lq.doc.entity.ParamRequestMethod;
import com.lq.doc.entity.RequestParamEntity;
import com.lq.doc.load.JavaFileLoader;
import org.hibernate.validator.constraints.Length;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.io.File;
import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.List;
public class LoadController {
private GlobArgs globArgs;
private static LoadController instance = null;
public static LoadController getInstance(GlobArgs globArgs) {
if (instance == null) {
synchronized (LoadController.class) {
if (instance == null) {
instance = new LoadController(globArgs);
}
}
}
return instance;
}
private LoadController(GlobArgs globArgs) {
this.globArgs = globArgs;
}
public List parseController(File file) throws Exception {
String className = file.getName();
StringBuilder fileContent = getControllerFileContent(file);
if (fileContent != null) {
Class> aClass = JavaFileLoader.loadClass(className, fileContent);
Annotation[] classAnnotations = aClass.getDeclaredAnnotations();
if (classAnnotations.length > 0) {
for (Annotation annotation : classAnnotations) {
if (annotation instanceof Controller || annotation instanceof RestController) {
return realParseController(aClass);
}
}
}
}
return null;
}
private StringBuilder getControllerFileContent(File controllerFile) throws IOException {
String className = controllerFile.getName().replace(".java", "");
StringBuilder stringBuilder = FileUtil.readFileContent(controllerFile);
String allClassName = stringBuilder.substring(stringBuilder.indexOf(className), stringBuilder.indexOf("{"));
if (allClassName.contains("extends")) {
String extendClassName = allClassName.split("extends")[1].trim();
String prefixContent = stringBuilder.substring(0, stringBuilder.indexOf("public class"));
String extendClassFilePath;
if (prefixContent.contains(extendClassName)) {
String substring = prefixContent.substring(0, prefixContent.indexOf(extendClassName + ";"));
String[] split = substring.substring(substring.lastIndexOf("import") + 6).trim().split("\\.");
String path = "";
for (String s : split) {
path += s + File.separator;
}
extendClassFilePath = globArgs.projectPath + File.separator + "src" + File.separator + "main" + File.separator + "java" + File.separator + path + extendClassName + ".java";
} else {
extendClassFilePath = controllerFile.getAbsolutePath().replace(className, extendClassName).trim();
}
stringBuilder.delete(prefixContent.length(), stringBuilder.indexOf("{"))
.insert(stringBuilder.indexOf("{"), "public class " + className);
File extendClassFile = new File(extendClassFilePath);
StringBuilder extendClassSb = FileUtil.readFileContent(extendClassFile);
String extendClassPrefixContent = extendClassSb.substring(0, extendClassSb.indexOf("public class"));
String extendClassPrefix = extendClassPrefixContent.substring(extendClassPrefixContent.indexOf(";") + 1, extendClassPrefixContent.lastIndexOf(";") + 1);
String extendClassContent = extendClassSb.substring(extendClassSb.indexOf("{") + 1, extendClassSb.lastIndexOf("}"));
stringBuilder.insert(prefixContent.lastIndexOf(";") + 1, extendClassPrefix);
stringBuilder.insert(stringBuilder.indexOf("{") + 1, extendClassContent);
return stringBuilder;
} else {
return FileUtil.readFileContent(controllerFile);
}
}
private List loadJavaBean(String className) throws Exception {
Class> aClass1 = Class.forName(className);
List declaredFieldList = new ArrayList<>();
ClassUtil.getDeclaredFields(aClass1, declaredFieldList);
Field[] declaredFields = aClass1.getDeclaredFields();
if (declaredFields.length > 0) {
List paramEntities = new ArrayList<>();
for (Field field : declaredFields) {
ParamEntity paramEntity = new ParamEntity();
paramEntity.setRequired(false);
paramEntity.setLengthLimit("无限制");
Annotation[] declaredAnnotations = field.getDeclaredAnnotations();
if (declaredAnnotations.length > 0) {
for (Annotation annotation : declaredAnnotations) {
if (annotation instanceof NotBlank || annotation instanceof NotEmpty) {
paramEntity.setRequired(true);
}
if (annotation instanceof Length) {
String message = ((Length) annotation).message();
paramEntity.setLengthLimit(message);
}
}
}
paramEntity.setType(field.getType().getName());
paramEntity.setName(field.getName());
paramEntities.add(paramEntity);
}
return paramEntities;
}
return null;
}
public List realParseController(Class> aClass) throws Exception {
String controllerUrlPath = "";
RequestMapping requestMapping = aClass.getDeclaredAnnotation(RequestMapping.class);
if (requestMapping != null) {
controllerUrlPath = requestMapping.value()[0] + "/";
}
List apiRequestEntities = null;
Method[] methods = aClass.getDeclaredMethods();
if (methods.length > 0) {
apiRequestEntities = new ArrayList<>();
for (Method method : methods) {
if (!method.getName().startsWith("lambda$")) {
ApiRequestEntity apiRequestEntity = new ApiRequestEntity();
Annotation[] methodAnnotations = method.getDeclaredAnnotations();
if (methodAnnotations.length > 0) {
for (Annotation methodAnnotation : methodAnnotations) {
if (methodAnnotation instanceof PostMapping || methodAnnotation instanceof GetMapping ||
methodAnnotation instanceof DeleteMapping || methodAnnotation instanceof PutMapping ||
methodAnnotation instanceof RequestMapping) {
if (methodAnnotation instanceof PostMapping) {
PostMapping postMapping = (PostMapping) methodAnnotation;
apiRequestEntity.setRequestMethod(RequestMethod.POST);
apiRequestEntity.setValue(controllerUrlPath + postMapping.value()[0]);
apiRequestEntity.setConsumes(postMapping.consumes());
apiRequestEntity.setProduces(postMapping.produces());
apiRequestEntity.setHeaders(postMapping.headers());
} else if (methodAnnotation instanceof GetMapping) {
GetMapping getMapping = (GetMapping) methodAnnotation;
apiRequestEntity.setRequestMethod(RequestMethod.GET);
apiRequestEntity.setValue(controllerUrlPath + getMapping.value()[0]);
apiRequestEntity.setConsumes(getMapping.consumes());
apiRequestEntity.setProduces(getMapping.produces());
apiRequestEntity.setHeaders(getMapping.headers());
} else if (methodAnnotation instanceof PutMapping) {
PutMapping putMapping = (PutMapping) methodAnnotation;
apiRequestEntity.setRequestMethod(RequestMethod.PUT);
apiRequestEntity.setValue(controllerUrlPath + putMapping.value()[0]);
apiRequestEntity.setConsumes(putMapping.consumes());
apiRequestEntity.setProduces(putMapping.produces());
apiRequestEntity.setHeaders(putMapping.headers());
} else if (methodAnnotation instanceof DeleteMapping) {
DeleteMapping deleteMapping = (DeleteMapping) methodAnnotation;
apiRequestEntity.setRequestMethod(RequestMethod.DELETE);
apiRequestEntity.setValue(controllerUrlPath + deleteMapping.value()[0]);
apiRequestEntity.setConsumes(deleteMapping.consumes());
apiRequestEntity.setProduces(deleteMapping.produces());
apiRequestEntity.setHeaders(deleteMapping.headers());
} else {
RequestMapping methodRequestMapping = (RequestMapping) methodAnnotation;
RequestMethod[] requestMethods = methodRequestMapping.method();
apiRequestEntity.setRequestMethod(requestMethods.length == 0 ? RequestMethod.GET : requestMethods[0]);
apiRequestEntity.setValue(controllerUrlPath + methodRequestMapping.value()[0]);
apiRequestEntity.setConsumes(methodRequestMapping.consumes());
apiRequestEntity.setProduces(methodRequestMapping.produces());
apiRequestEntity.setHeaders(methodRequestMapping.headers());
}
}
}
}
List requestParamEntities = null;
Parameter[] methodParameters = method.getParameters();
if (methodParameters.length > 0) {
requestParamEntities = new ArrayList<>();
for (Parameter parameter : methodParameters) {
String parameterType = parameter.getParameterizedType().getTypeName();
if (!parameterType.equals(Constants.BINDING_RESULT) && !parameterType.startsWith(Constants.HTTP_SERVLET)) {
RequestParamEntity requestParamEntity = new RequestParamEntity();
Annotation[] parameterAnnotations = parameter.getAnnotations();
requestParamEntity.setLengthLimit("none");
requestParamEntity.setType(parameterType);
if (parameterAnnotations.length > 0) {
Annotation parameterAnnotation = parameterAnnotations[0];
if (parameterAnnotation instanceof RequestBody) {
RequestBody requestBody = (RequestBody) parameterAnnotation;
requestParamEntity.setRequired(requestBody.required());
if (parameterType.startsWith(globArgs.packageName)) {
String entityName = parameterType.substring(parameterType.lastIndexOf(".") + 1);
requestParamEntity.setName(StringUtil.firstToLowerCase(entityName));
List paramEntities = loadJavaBean(parameterType);
requestParamEntity.setParamEntities(paramEntities);
} else {
requestParamEntity.setName(parameter.getName());
}
requestParamEntity.setParamRequestMethod(ParamRequestMethod.BODY);
} else if (parameterAnnotation instanceof RequestParam) {
RequestParam requestParam = (RequestParam) parameterAnnotation;
requestParamEntity.setRequired(requestParam.required());
requestParamEntity.setName(requestParam.value());
if (parameterType.equals(Constants.MULTIPART_FILE)) {
requestParamEntity.setParamRequestMethod(ParamRequestMethod.FILE);
} else {
requestParamEntity.setParamRequestMethod(ParamRequestMethod.PARAM);
}
} else if (parameterAnnotation instanceof RequestHeader) {
RequestHeader requestHeader = (RequestHeader) parameterAnnotation;
requestParamEntity.setRequired(requestHeader.required());
requestParamEntity.setName(requestHeader.value());
requestParamEntity.setParamRequestMethod(ParamRequestMethod.HEADER);
} else if (parameterAnnotation instanceof RequestAttribute) {
RequestAttribute requestAttribute = (RequestAttribute) parameterAnnotation;
requestParamEntity.setRequired(requestAttribute.required());
requestParamEntity.setName(requestAttribute.value());
requestParamEntity.setParamRequestMethod(ParamRequestMethod.ATTRIBUTE);
} else if (parameterAnnotation instanceof RequestPart) {
RequestPart requestPart = (RequestPart) parameterAnnotation;
requestParamEntity.setRequired(requestPart.required());
requestParamEntity.setName(requestPart.value());
requestParamEntity.setParamRequestMethod(ParamRequestMethod.PART);
} else {
if (parameterType.startsWith(globArgs.packageName)) {
String entityName = parameterType.substring(parameterType.lastIndexOf(".") + 1);
requestParamEntity.setName(StringUtil.firstToLowerCase(entityName));
List paramEntities = loadJavaBean(parameterType);
// paramEntities.forEach(paramEntity -> System.out.println(paramEntity.toString()));
requestParamEntity.setParamEntities(paramEntities);
} else {
requestParamEntity.setName(parameter.getName());
}
requestParamEntity.setParamRequestMethod(ParamRequestMethod.PARAM);
}
} else {
requestParamEntity.setRequired(true);
if (parameterType.startsWith(globArgs.packageName)) {
String entityName = parameterType.substring(parameterType.lastIndexOf(".") + 1);
requestParamEntity.setName(StringUtil.firstToLowerCase(entityName));
List paramEntities = loadJavaBean(parameterType);
requestParamEntity.setParamEntities(paramEntities);
requestParamEntity.setName(entityName);
requestParamEntity.setParamRequestMethod(ParamRequestMethod.PARAM);
} else {
requestParamEntity.setName(parameter.getName());
requestParamEntity.setParamRequestMethod(ParamRequestMethod.NONE);
}
}
requestParamEntities.add(requestParamEntity);
}
}
}
apiRequestEntity.setMethodName(method.getName());
apiRequestEntity.setRequestParamEntities(requestParamEntities);
apiRequestEntities.add(apiRequestEntity);
}
}
}
return apiRequestEntities;
}
}