项目接口信息汇总
import com.sun.javadoc.AnnotationDesc;
import com.sun.javadoc.ClassDoc;
import com.sun.javadoc.MethodDoc;
import com.sun.javadoc.RootDoc;
import com.sun.tools.javadoc.Main;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.util.StringUtils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@Slf4j
public class GetInterfaceInfoToExcel {
private static final String SAVE_PATH = "D:/项目接口信息汇总.xlsx";
private static final List<String> SYSTEM_PATH = new ArrayList<>();
public static List<String> resultPath = new ArrayList<>();
static RootDoc rootDoc;
public static class Doclet {
public static boolean start(RootDoc rootDoc) {
GetInterfaceInfoToExcel.rootDoc = rootDoc;
return true;
}
}
public static void main(String[] args) {
init();
SYSTEM_PATH.forEach(sp -> {
resultPath.clear();
getPath(new File(sp));
String systemName = sp.substring(sp.lastIndexOf("\\") + 1);
resultPath.forEach(path -> saveInterfaceInfoToExcel(systemName, path));
});
}
private static void init() {
SYSTEM_PATH.add("D:\\codes\\aaa");
SYSTEM_PATH.add("D:\\codes\\bbb");
SYSTEM_PATH.add("D:\\codes\\ccc");
SYSTEM_PATH.add("D:\\codes\\ddd");
}
private static void getPath(File file) {
File[] files = file.listFiles();
if (file.isDirectory() && files != null) {
Arrays.stream(files).forEach(f -> {
if (f.isDirectory()) {
File[] fs = f.listFiles();
if ("controller".equals(f.getName()) && fs != null) {
Arrays.stream(fs)
.map(File::getPath)
.filter(p -> !p.contains("\\target\\") && !p.contains("\\test\\"))
.forEach(path -> resultPath.add(path));
} else {
getPath(f);
}
}
});
}
}
private static void saveInterfaceInfoToExcel(String systemName, String javaPath) {
String temp = javaPath.substring(javaPath.indexOf(systemName) + systemName.length() + 1);
String modelName = temp.substring(0, temp.indexOf("\\"));
String[] strs = new String[9];
strs[0] = "-locale";
strs[1] = "zh_CN";
strs[2] = "-doclet";
strs[3] = Doclet.class.getName();
strs[4] = "-docletpath";
strs[5] = Doclet.class.getResource("/").getPath();
strs[6] = "-encoding";
strs[7] = "UTF-8";
strs[8] = javaPath;
Main.execute(strs);
ClassDoc[] classes = rootDoc.classes();
for (ClassDoc classDoc : classes) {
String rawCommentText = classDoc.getRawCommentText();
if (rawCommentText.contains("@author")) {
rawCommentText = rawCommentText.substring(rawCommentText.indexOf("@author") + 7);
rawCommentText = authorFormat(rawCommentText);
} else if (rawCommentText.contains("@Author")) {
rawCommentText = rawCommentText.substring(rawCommentText.indexOf("@Author") + 7);
rawCommentText = authorFormat(rawCommentText);
} else {
rawCommentText = "";
}
String author = rawCommentText;
AnnotationDesc[] annotations = classDoc.annotations();
String classReqUrl = "";
for (AnnotationDesc annotationDesc : annotations) {
if ("RequestMapping".equals(annotationDesc.annotationType().name())
&& annotationDesc.elementValues().length > 0) {
classReqUrl = annotationDesc.elementValues()[0].value().toString();
}
}
MethodDoc[] methodDocs = classDoc.methods();
for (MethodDoc methodDoc : methodDocs) {
AnnotationDesc[] descs = methodDoc.annotations();
String commentText = methodDoc.commentText();
String reqUrl = "";
for (AnnotationDesc desc : descs) {
if ("RequestMapping".equals(desc.annotationType().name()) ||
"GetMapping".equals(desc.annotationType().name()) ||
"PostMapping".equals(desc.annotationType().name()) ||
"PutMapping".equals(desc.annotationType().name()) ||
"DeleteMapping".equals(desc.annotationType().name())) {
String value = desc.elementValues()[0].value().toString();
if (!value.contains(".")) {
reqUrl = value;
}
if (StringUtils.hasText(reqUrl) && !reqUrl.startsWith("/")) {
reqUrl = "/" + reqUrl;
}
reqUrl = classReqUrl + reqUrl;
reqUrl = reqUrl.replace("\"", "");
while (reqUrl.contains("//")) {
reqUrl = reqUrl.replace("//", "/");
}
}
if ("ApiOperation".equals(desc.annotationType().name()) &&
!desc.elementValues()[0].value().toString().contains(".")) {
commentText = !StringUtils.hasText(commentText) ?
desc.elementValues()[0].value().value().toString() :
commentText + ";" + desc.elementValues()[0].value().value().toString();
}
}
if (StringUtils.hasText(reqUrl)) {
List<String> sl = new ArrayList<>();
sl.add(modelName);
sl.add(reqUrl);
sl.add(commentText);
sl.add(author);
log.info(sl.toString());
log.info("类" + classDoc.name() + ",请求路径:" + reqUrl + ", 方法名:" + methodDoc.name() + ",开发人:"
+ author + ",方法注释:" + commentText);
writeExcel(systemName, sl);
}
}
}
}
private static String authorFormat(String rawCommentText) {
if (rawCommentText.contains("\n")) {
rawCommentText = rawCommentText.substring(0, rawCommentText.indexOf("\n"))
.replace(":", "").trim();
} else {
rawCommentText = rawCommentText.substring(0, rawCommentText.indexOf(" "))
.replace(":", "").trim();
}
if (rawCommentText.contains("by ")) {
rawCommentText = rawCommentText.substring(rawCommentText.indexOf("by ") + 3).trim();
rawCommentText = rawCommentText.substring(0, rawCommentText.indexOf(" "));
}
return rawCommentText;
}
private static void writeExcel(String tableName, List<String> list) {
File file = new File(SAVE_PATH);
XSSFWorkbook workbook = null;
FileOutputStream stream = null;
try {
if (!file.exists()) {
if (!file.createNewFile()) {
throw new IOException("创建excel文件失败!");
}
workbook = new XSSFWorkbook();
} else {
workbook = new XSSFWorkbook(new FileInputStream(file));
}
Sheet sheet = workbook.getSheet(tableName);
if (sheet == null) {
sheet = workbook.createSheet(tableName);
Row firstRow = sheet.createRow(0);
firstRow.createCell(0).setCellValue("模块");
firstRow.createCell(1).setCellValue("接口URL");
firstRow.createCell(2).setCellValue("功能描述");
firstRow.createCell(3).setCellValue("开发人");
}
int lastRowNum = sheet.getLastRowNum();
Row row = sheet.createRow(lastRowNum + 1);
for (int i = 0; i < list.size(); i++) {
row.createCell(i).setCellValue(list.get(i));
}
stream = new FileOutputStream(file);
workbook.write(stream);
log.info("写入成功:" + list);
} catch (IOException e) {
log.error(e.getMessage());
} finally {
try {
if (stream != null) {
stream.close();
}
if (workbook != null) {
workbook.close();
}
} catch (IOException e) {
log.error(e.getMessage());
}
}
}
}