已有项目中的注释格式不够规范,基本是行末注释,需要修改为以下两种注释:
1.文档注释
//修改为文档注释
public String convertToDocument(String origin){
StringBuilder sb = new StringBuilder();
String[] split = origin.split("//");
if (split.length == 2 ) {
sb.append("/**").append(separator).append("* ").append(split[1]).append(separator).append("*/").append(separator).append(split[0]);
}
return sb.toString();
}
2.修改为swagger属性注释
//修改为swaggerAPI注释方式
public static String convertToSwaggerProperty(String origin){
StringBuilder sb = new StringBuilder();
String[] split = origin.split("//");
if (split.length == 2 ) {
sb.append("@ApiModelProperty(value = \"").append(split[1]).append("\")").append(separator).append(split[0]).append(separator);
}else {
if (origin.contains("public class")){
sb.append("@Data").append(separator).append(origin).append(separator);
return sb.toString();
}
if (origin.startsWith("package")){
sb.append(origin).append(separator).append("import io.swagger.annotations.ApiModelProperty;").append(separator);
return sb.toString();
}
return origin;
}
return sb.toString();
}
3.修改文件
public static void main(String[] args) throws IOException {
String separator = System.getProperty("line.separator");
//TODO 指定文件所在路径
String filePath = "";
File file = new File(filePath);
File[] files = file.listFiles();
FileReader fileInputStream;
FileOutputStream outputStream;
BufferedReader bufferedReader;
for (File f:files){
StringBuilder sb = new StringBuilder();
//读取文件
fileInputStream = new FileReader(f);
bufferedReader = new BufferedReader(fileInputStream);
String lineTxt = null;
while((lineTxt = bufferedReader.readLine()) != null){
//按行读取后对已有字符串进行修改
sb.append(convertToSwaggerProperty(lineTxt)).append(separator);
}
//将修改后的文件内容重新写入文件
outputStream = new FileOutputStream(f);
outputStream.write(sb.toString().getBytes(StandardCharsets.UTF_8));
}
outputStream.close();
bufferedReader.close();
fileInputStream.close();
}