From bf2a7907f4f985fa34e923f4a773ea3f683a6077 Mon Sep 17 00:00:00 2001 From: duanledexianxianxian Date: Thu, 9 Apr 2020 08:34:20 +0800 Subject: [PATCH] :recycle: Refactoring code. --- .../description/ObjectTypeDescription.java | 24 ++++++++++++ .../kim/apidoc/core/parser/VisitorParser.java | 37 +++++++++---------- .../com/kim/apidoc/core/schema/Project.java | 6 ++- .../kim/apidoc/springmvc/SpringParser.java | 18 ++++++--- .../com/kim/apidoc/springmvc/SpringTest.java | 2 +- 5 files changed, 60 insertions(+), 27 deletions(-) diff --git a/apidoc-core/src/main/java/com/kim/apidoc/core/common/description/ObjectTypeDescription.java b/apidoc-core/src/main/java/com/kim/apidoc/core/common/description/ObjectTypeDescription.java index 746ab04..8aba8aa 100644 --- a/apidoc-core/src/main/java/com/kim/apidoc/core/common/description/ObjectTypeDescription.java +++ b/apidoc-core/src/main/java/com/kim/apidoc/core/common/description/ObjectTypeDescription.java @@ -12,19 +12,38 @@ import java.util.List; /** * 对象类型描述 + * + * @author fengyuchenglun + * @version 1.0.0 */ @Setter @Getter public class ObjectTypeDescription extends TypeDescription { + /** + * The Members. + */ protected List members = Lists.newArrayList(); + /** + * The Value. + */ private ObjectNode value = ObjectMappers.instance.createObjectNode(); + /** + * Merge. + * + * @param other the other + */ public void merge(ObjectTypeDescription other) { value.setAll(other.getValue()); members.addAll(other.members); } + /** + * Add. + * + * @param component the component + */ public void add(TypeDescription component) { members.add(component); if (component.isPrimitive()) { @@ -38,6 +57,11 @@ public class ObjectTypeDescription extends TypeDescription { } } + /** + * Primitive. + * + * @param typeDescription the type description + */ public void primitive(PrimitiveTypeDescription typeDescription) { switch (typeDescription.getType()) { case "byte": diff --git a/apidoc-core/src/main/java/com/kim/apidoc/core/parser/VisitorParser.java b/apidoc-core/src/main/java/com/kim/apidoc/core/parser/VisitorParser.java index fcde316..f3a00bb 100644 --- a/apidoc-core/src/main/java/com/kim/apidoc/core/parser/VisitorParser.java +++ b/apidoc-core/src/main/java/com/kim/apidoc/core/parser/VisitorParser.java @@ -1,22 +1,14 @@ package com.kim.apidoc.core.parser; import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; -import com.github.javaparser.ast.body.EnumConstantDeclaration; import com.github.javaparser.ast.body.EnumDeclaration; import com.github.javaparser.ast.body.MethodDeclaration; -import com.github.javaparser.ast.comments.Comment; import com.github.javaparser.ast.comments.JavadocComment; import com.github.javaparser.ast.visitor.VoidVisitorAdapter; -import com.github.javaparser.javadoc.Javadoc; -import com.github.javaparser.javadoc.JavadocBlockTag; -import com.github.javaparser.symbolsolver.core.resolution.Context; import com.kim.apidoc.core.ApiDoc; import com.kim.apidoc.core.common.helper.OptionalHelper; import com.kim.apidoc.core.schema.*; -import java.util.Objects; -import java.util.Optional; - /** * The type Visitor parser. @@ -89,22 +81,29 @@ public class VisitorParser extends VoidVisitorAdapter { Chapter chapter = (Chapter) arg; OptionalHelper.any(chapter.getTag("code")) .ifPresent(tag -> { - if (javadocComment.getCommentedNode().isPresent()){ - com.github.javaparser.ast.Node commentedNode=javadocComment.getCommentedNode().get(); - // 常量类||枚举类 - if (commentedNode instanceof ClassOrInterfaceDeclaration - || commentedNode instanceof EnumDeclaration){ - Appendix appendix = Appendix.parse(javadocComment); - if (appendix != null) { - ApiDoc.getInstance().getProject().getAppendices().add(appendix); - } - } + if (javadocComment.getCommentedNode().isPresent()) { + com.github.javaparser.ast.Node commentedNode = javadocComment.getCommentedNode().get(); + // 常量类||枚举类 + if (commentedNode instanceof ClassOrInterfaceDeclaration + || commentedNode instanceof EnumDeclaration) { + Appendix appendix = Appendix.parse(javadocComment); + if (appendix != null) { + ApiDoc.getInstance().getProject().getAppendices().add(appendix); + } + } } }); OptionalHelper.any(chapter.getTag("resultData")) .ifPresent(tag -> { - System.out.println(javadocComment); + if (javadocComment.getCommentedNode().isPresent()) { + com.github.javaparser.ast.Node commentedNode = javadocComment.getCommentedNode().get(); + // 常量类||枚举类 + if (commentedNode instanceof ClassOrInterfaceDeclaration) { + ClassOrInterfaceDeclaration classOrInterfaceDeclaration = (ClassOrInterfaceDeclaration) commentedNode; + ApiDoc.getInstance().getProject().setResultDataClassOrInterfaceDeclaration(classOrInterfaceDeclaration); + } + } }); } super.visit(javadocComment, arg); diff --git a/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Project.java b/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Project.java index e64c103..013c93c 100644 --- a/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Project.java +++ b/apidoc-core/src/main/java/com/kim/apidoc/core/schema/Project.java @@ -1,5 +1,6 @@ package com.kim.apidoc.core.schema; +import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; import lombok.Getter; import lombok.Setter; @@ -30,7 +31,10 @@ public class Project extends Node { * 附录 */ List appendices = new LinkedList<>(); - + /** + * resultData + */ + ClassOrInterfaceDeclaration resultDataClassOrInterfaceDeclaration; /** * Add chapter. diff --git a/apidoc-springmvc/src/main/java/com/kim/apidoc/springmvc/SpringParser.java b/apidoc-springmvc/src/main/java/com/kim/apidoc/springmvc/SpringParser.java index d73f364..11a385e 100644 --- a/apidoc-springmvc/src/main/java/com/kim/apidoc/springmvc/SpringParser.java +++ b/apidoc-springmvc/src/main/java/com/kim/apidoc/springmvc/SpringParser.java @@ -1,6 +1,11 @@ package com.kim.apidoc.springmvc; -import com.kim.apidoc.springmvc.resovler.SpringComponentTypeResolver; +import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; +import com.github.javaparser.ast.body.MethodDeclaration; +import com.github.javaparser.ast.body.Parameter; +import com.github.javaparser.ast.expr.AnnotationExpr; +import com.github.javaparser.ast.expr.Expression; +import com.github.javaparser.ast.type.ClassOrInterfaceType; import com.kim.apidoc.core.ApiDoc; import com.kim.apidoc.core.common.URI; import com.kim.apidoc.core.common.description.ObjectTypeDescription; @@ -13,11 +18,7 @@ import com.kim.apidoc.core.schema.Chapter; import com.kim.apidoc.core.schema.Header; import com.kim.apidoc.core.schema.Row; import com.kim.apidoc.core.schema.Section; -import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; -import com.github.javaparser.ast.body.MethodDeclaration; -import com.github.javaparser.ast.body.Parameter; -import com.github.javaparser.ast.expr.AnnotationExpr; -import com.github.javaparser.ast.expr.Expression; +import com.kim.apidoc.springmvc.resovler.SpringComponentTypeResolver; import java.util.List; import java.util.Optional; @@ -307,6 +308,11 @@ public class SpringParser implements ParserStrategy { * @param section the section */ private void visitReturn(MethodDeclaration n, Chapter chapter, Section section) { + ClassOrInterfaceType returnType = new ClassOrInterfaceType(); + returnType.setName(ApiDoc.getInstance().getProject().getResultDataClassOrInterfaceDeclaration().getName()); + returnType.setTypeArguments(n.getType()); + n.setType(returnType); + TypeDescription description = ApiDoc.getInstance().getTypeResolvers().resolve(n.getType()); if (description.isAvailable()) { if (description.isPrimitive()) { diff --git a/apidoc-springmvc/src/test/java/com/kim/apidoc/springmvc/SpringTest.java b/apidoc-springmvc/src/test/java/com/kim/apidoc/springmvc/SpringTest.java index 824168d..23b6e0d 100644 --- a/apidoc-springmvc/src/test/java/com/kim/apidoc/springmvc/SpringTest.java +++ b/apidoc-springmvc/src/test/java/com/kim/apidoc/springmvc/SpringTest.java @@ -17,7 +17,7 @@ public class SpringTest { Context context = new Context(); context.setId("test"); context.setName("测试项目"); - context.addSource(Paths.get("F:\\@project@\\@dianli@\\tool\\apidoc\\apidoc-springmvc\\src\\test\\java")); + context.addSource(Paths.get("K:\\@project-dianli@\\tool\\apidoc\\apidoc-springmvc\\src\\test\\java")); // context.setCss("https://darshandsoni.com/asciidoctor-skins/css/monospace.css"); ApiDoc apigcc = new ApiDoc(context); -- GitLab