Skip to content

Commit

Permalink
--taskid=ID20231018
Browse files Browse the repository at this point in the history
抽取配置参数类,统一控制
  • Loading branch information
xinluke committed Oct 18, 2023
1 parent 0c357b5 commit c179877
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 27 deletions.
26 changes: 26 additions & 0 deletions src/main/java/com/wangym/lombok/conf/RuleConf.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,30 @@ public class RuleConf {
* 是否开启request path声明配置
*/
private boolean requestMappingPathEnable;

/**
* 是否开启javadoc注释配置
* 如果开启,则会在方法上面,将单行注释转化为javadoc注释
*/
private boolean singleComment2javadocEnable;
/**
* 是否要将类上面的url和方法拼接到一起
*/
private boolean enableMergeRequestUrl;
/**
* 有些开发者希望api返回的格式是固定的,所以直接在注解声明只支持json格式的数据响应
* 避免有些开发者不清楚自己使用了Http的Accept协商头,而我们的api假如是多种返回格式的话,可能会返回xml或者其他格式的数据,而重点在于开发者不太了解http的协商机制,从而认为是我们的问题。
* 现阶段使用json格式的数据应该是满足绝大部分的用户的需求
*/
private boolean onlySupportJson;
/**
* 是否需要在每一个方法上面添加@ApiOperation注解
*/
private boolean apiOperation;
private boolean synchronizedAnnotationSupport;
private boolean fullSearch;
/**
* 去除日志打印参数中的"JSON.toJSONString(list)"
*/
private boolean deleteJsonWraped;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Arrays;
Expand All @@ -31,12 +30,10 @@
public class ReplaceLoggerJob extends AbstractJavaJob {

private Metadata meta = new Metadata("Slf4j", "lombok.extern.slf4j.Slf4j");
@Value("${loggerSearchAll:false}")
private boolean fullSearch;

@Override
public void process(CompilationUnit compilationUnit) {
LoggerVisitor visitor = new LoggerVisitor(fullSearch);
LoggerVisitor visitor = new LoggerVisitor(ruleConf.isFullSearch());
compilationUnit.accept(visitor, null);
//compilationUnit.findAll(FieldDeclaration.class).forEach(it -> visitor.visit(it, null));
// rename变量
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.wangym.lombok.job.AbstractJavaJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

Expand All @@ -23,9 +22,6 @@
@Slf4j
public class ReplaceLoggerPlaceholderJob extends AbstractJavaJob {

@Value("${logger.deleteJsonWraped:false}")
private boolean deleteJsonWraped;

@Override
public void process(CompilationUnit compilationUnit) {
boolean hasAnn = compilationUnit.findAll(ClassOrInterfaceDeclaration.class).stream().filter(this::isTarget)
Expand Down Expand Up @@ -80,7 +76,7 @@ public Visitable visit(MethodCallExpr n, Void arg) {
doHandle1(n);
}
// 去除参数中的"JSON.toJSONString(list)"
if (deleteJsonWraped && args.size() > 1) {
if (ruleConf.isDeleteJsonWraped() && args.size() > 1) {
for (ListIterator<Expression> iterator = args.listIterator(); iterator.hasNext();) {
Expression itExpr = iterator.next();
if (itExpr instanceof MethodCallExpr) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.Arrays;
Expand All @@ -31,13 +30,7 @@
public class ReplaceRequestMappingJob extends AbstractJavaJob {
private List<String> annNames = Arrays.asList("RequestMapping", "GetMapping", "PostMapping", "PutMapping",
"DeleteMapping", "PatchMapping");
// 是否要将类上面的url和方法拼接到一起
@Value("${mergeRequestUrl:false}")
private boolean enableMergeRequestUrl;
@Value("${onlySupportJson:false}")
private boolean onlySupportJson;
@Value("${addApiOperation:false}")
private boolean apiOperation;

private static Map<String, Metadata> mapping = new HashMap<>();
static {
mapping.put("RequestMethod.GET",
Expand All @@ -58,7 +51,7 @@ public void process(CompilationUnit compilationUnit) {
RequestMappingConstructorVisitor visitor = new RequestMappingConstructorVisitor(compilationUnit);
compilationUnit.accept(visitor, null);
compilationUnit.accept(new RequiresPermissionsVisitor(), null);
if (apiOperation) {
if (ruleConf.isApiOperation()) {
compilationUnit.accept(new ApiOperationVisitor(), null);
}
// 如果存在变更,则操作
Expand Down Expand Up @@ -269,7 +262,7 @@ private void searchPath0(ClassOrInterfaceDeclaration n) {

void recordPath(String newPath) {
// 只更新一次
if (path == null && enableMergeRequestUrl && isInterface) {
if (path == null && ruleConf.isEnableMergeRequestUrl() && isInterface) {
path = newPath;
}
}
Expand Down Expand Up @@ -359,10 +352,7 @@ private void doHandle(NormalAnnotationExpr expr) {
producesExist = true;
}
}
if(onlySupportJson && !producesExist) {
// 有些开发者希望api返回的格式是固定的,所以直接在注解声明只支持json格式的数据响应
// 避免有些开发者不清楚自己使用了Http的Accept协商头,而我们的api假如是多种返回格式的话,可能会返回xml或者其他格式的数据,而重点在于开发者不太了解http的协商机制,从而认为是我们的问题。
// 现阶段使用json格式的数据应该是满足绝大部分的用户的需求
if(ruleConf.isOnlySupportJson() && !producesExist) {
FieldAccessExpr value = new FieldAccessExpr(new NameExpr("MediaType"), "APPLICATION_JSON_VALUE");
pairs.add(new MemberValuePair("produces", value));
addImports(compilationUnit, mediaTypeMetadata);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.wangym.lombok.job.Metadata;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.util.*;
Expand All @@ -39,8 +38,6 @@ public class SystemOutPrintJob extends AbstractJavaJob {
private Metadata meta3 = new Metadata("Synchronized", "lombok.Synchronized");
private Metadata metaHashMap = new Metadata("HashMap", "java.util.HashMap");
private Metadata metaArrayList = new Metadata("ArrayList", "java.util.ArrayList");
@Value("${synchronizedAnnotationSupport:false}")
private boolean synchronizedAnnotationSupport;

@Override
public void process(CompilationUnit compilationUnit) {
Expand Down Expand Up @@ -356,7 +353,7 @@ public SynchronizedMehtodVisitor(CompilationUnit compilationUnit) {
@Override
public Visitable visit(MethodDeclaration n, Void arg) {
//如果存在Synchronized的方法块,需要转成lombok的注解
if (synchronizedAnnotationSupport && n.isSynchronized()) {
if (ruleConf.isSynchronizedAnnotationSupport() && n.isSynchronized()) {
NodeList<Modifier> mo = n.getModifiers();
mo.remove(Modifier.synchronizedModifier());
n.addAnnotation(new MarkerAnnotationExpr(meta3.getAnnName()));
Expand Down

0 comments on commit c179877

Please sign in to comment.