Skip to content

Commit

Permalink
--taskid=ID20231010
Browse files Browse the repository at this point in the history
将方法上的单行注释换成javadoc注释,ide可以自动关联,方便查阅注释说明
  • Loading branch information
xinluke committed Oct 10, 2023
1 parent c0b6d0d commit c70a66f
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 16 deletions.
45 changes: 38 additions & 7 deletions src/main/java/com/wangym/lombok/job/impl/SystemOutPrintJob.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
import com.github.javaparser.ast.body.FieldDeclaration;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.comments.Comment;
import com.github.javaparser.ast.comments.LineComment;
import com.github.javaparser.ast.expr.*;
import com.github.javaparser.ast.stmt.BlockStmt;
import com.github.javaparser.ast.type.ClassOrInterfaceType;
Expand Down Expand Up @@ -37,6 +39,7 @@ public class SystemOutPrintJob extends AbstractJavaJob {
private Metadata metaArrayList = new Metadata("ArrayList", "java.util.ArrayList");
@Value("${synchronizedAnnotationSupport:false}")
private boolean synchronizedAnnotationSupport;

@Override
public void process(CompilationUnit compilationUnit) {
compilationUnit.accept(new SynchronizedMehtodVisitor(compilationUnit), null);
Expand All @@ -45,10 +48,11 @@ public void process(CompilationUnit compilationUnit) {
compilationUnit.accept(new GuavaVisitor(compilationUnit), null);
compilationUnit.accept(visitor, null);
compilationUnit.accept(new ValueVisitor(), null);
compilationUnit.accept(new CommentVistor(), null);
compilationUnit.accept(new FeignClientVisitor(), null);
AutowiredVisitor visit = new AutowiredVisitor();
compilationUnit.accept(visit, null);
if(visit.isFlag()) {
if (visit.isFlag()) {
addImports(compilationUnit, meta2);
}
}
Expand Down Expand Up @@ -81,15 +85,38 @@ private boolean hasAnnotation(ClassOrInterfaceDeclaration parent, String annotat
return exist;
}
}, null);
};
}

;

class CommentVistor extends ModifierVisitor<Void> {
@Override
public Visitable visit(Modifier n, Void arg) {
return super.visit(n, arg);
}

@Override
public Visitable visit(MethodDeclaration n, Void arg) {
//将非javadoc注释换成javadoc注释
if (n.getComment().isPresent()) {
Comment comment = n.getComment().get();
//只处理单行注释
if (comment instanceof LineComment) {
String content = comment.getContent();
n.setJavadocComment(content);
}
}
return super.visit(n, arg);
}
}

class ValueVisitor extends ModifierVisitor<Void> {
@Override
public Visitable visit(FieldDeclaration n, Void arg) {
Expression targetValExpr = null;
NodeList<AnnotationExpr> anns = n.getAnnotations();
// 将字段上面的@Value(value="${black.list}")去掉value=,统一格式
for (Iterator<AnnotationExpr> iterator = anns.iterator(); iterator.hasNext();) {
for (Iterator<AnnotationExpr> iterator = anns.iterator(); iterator.hasNext(); ) {
AnnotationExpr annotationExpr = iterator.next();
if ("Value".equals(annotationExpr.getNameAsString())) {
if (annotationExpr instanceof NormalAnnotationExpr) {
Expand Down Expand Up @@ -125,7 +152,7 @@ public Visitable visit(FieldDeclaration n, Void arg) {
boolean record = false;
NodeList<AnnotationExpr> anns = n.getAnnotations();
// 将字段上面的@Resource换成@Autowired,统一管理
for (Iterator<AnnotationExpr> iterator = anns.iterator(); iterator.hasNext();) {
for (Iterator<AnnotationExpr> iterator = anns.iterator(); iterator.hasNext(); ) {
AnnotationExpr annotationExpr = iterator.next();
if ("Resource".equals(annotationExpr.getNameAsString())) {
iterator.remove();
Expand All @@ -140,6 +167,7 @@ record = true;
return super.visit(n, arg);
}
}

class FeignClientVisitor extends ModifierVisitor<Void> {

@Override
Expand All @@ -156,9 +184,11 @@ public Visitable visit(SingleMemberAnnotationExpr n, Void arg) {


}

class FeignClientCheckVisitor extends ModifierVisitor<Void> {

private String path;

public FeignClientCheckVisitor(String path) {
super();
this.path = path;
Expand Down Expand Up @@ -248,7 +278,7 @@ public ExprWrapper(ObjectCreationExpr expr, Metadata meta) {
class SystemOutPrintVisitor extends ModifierVisitor<Void> {

private CompilationUnit compilationUnit;

public SystemOutPrintVisitor(CompilationUnit compilationUnit) {
super();
this.compilationUnit = compilationUnit;
Expand Down Expand Up @@ -279,7 +309,7 @@ private MethodCallExpr process(MethodCallExpr expr) {
if (size == 1) {
Expression arg = args.get(0);
// 如果判断参数类型不是String的话就应该包装print
if (!(arg instanceof StringLiteralExpr|| arg instanceof BinaryExpr)) {
if (!(arg instanceof StringLiteralExpr || arg instanceof BinaryExpr)) {
args.add(0, new StringLiteralExpr("print:{}"));
}
}
Expand All @@ -292,7 +322,7 @@ private MethodCallExpr process(MethodCallExpr expr) {

class SynchronizedMehtodVisitor extends ModifierVisitor<Void> {
private CompilationUnit compilationUnit;

public SynchronizedMehtodVisitor(CompilationUnit compilationUnit) {
super();
this.compilationUnit = compilationUnit;
Expand All @@ -310,6 +340,7 @@ public Visitable visit(MethodDeclaration n, Void arg) {
return super.visit(n, arg);
}
}

class MainMehtodVisitor extends ModifierVisitor<Void> {
@Override
public Visitable visit(MethodDeclaration n, Void arg) {
Expand Down
16 changes: 7 additions & 9 deletions src/test/resources/JmlinkPortalClient.java
Original file line number Diff line number Diff line change
@@ -1,34 +1,32 @@
package com.wangym.portal.client;

import com.wangym.jmlink.base.data.enums.UniversalLinkType;
import com.wangym.jmlink.base.data.model.env.BaseAppEnvParam;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import feign.RequestTemplate;
import feign.Retryer;
import feign.codec.EncodeException;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

import java.lang.reflect.Type;

@FeignClient(name = "jmlink-portal",
configuration = {
JmlinkPortalClient.FeignRetryingConfig.class
})
public interface JmlinkPortalClient {

//这是一个单行注释
@PostMapping(value = "/jmlink-portal/v1/jmlink/app/{appKey}/env/android/integrate")
HttpEntity addAndroid(@PathVariable("appKey") String appKey,
@RequestBody AndroidParam param);
/*
这是一个多行注释
*/
@PostMapping(value = "/jmlink-portal/v1/jmlink/app/{appKey}/env/android/integrate2")
HttpEntity addAndroid2(@PathVariable("appKey") String appKey,
@RequestBody AndroidParam param);

@EqualsAndHashCode(callSuper = true)
@Data
Expand Down

0 comments on commit c70a66f

Please sign in to comment.