From 638a3728cba5f8af16092ce25d451400dbba6ff6 Mon Sep 17 00:00:00 2001 From: duanledexianxianxian Date: Mon, 13 Apr 2020 02:15:37 +0800 Subject: [PATCH] :recycle: Refactoring code. --- .../core/common/helper/AnnotationHelper.java | 6 +++--- .../common/helper/ClassDeclarationHelper.java | 19 ++++++++++++++----- .../core/common/helper/CommentHelper.java | 18 +++++++++--------- .../kim/apidoc/springmvc/SpringParser.java | 5 ++--- .../com/kim/apidoc/springmvc/SpringTest.java | 2 +- 5 files changed, 29 insertions(+), 21 deletions(-) diff --git a/apidoc-core/src/main/java/com/kim/apidoc/core/common/helper/AnnotationHelper.java b/apidoc-core/src/main/java/com/kim/apidoc/core/common/helper/AnnotationHelper.java index 3f1aecc..a539365 100644 --- a/apidoc-core/src/main/java/com/kim/apidoc/core/common/helper/AnnotationHelper.java +++ b/apidoc-core/src/main/java/com/kim/apidoc/core/common/helper/AnnotationHelper.java @@ -24,7 +24,7 @@ public class AnnotationHelper { } if (annotationExpr.isNormalAnnotationExpr()) { for (MemberValuePair pair : annotationExpr.asNormalAnnotationExpr().getPairs()) { - if (Objects.equals(key, pair.getNameAsString())){ + if (Objects.equals(key, pair.getNameAsString())) { return Optional.of(pair.getValue()); } } @@ -32,10 +32,10 @@ public class AnnotationHelper { return Optional.empty(); } - public static Optional getAnyAttribute(AnnotationExpr annotationExpr, String ... keys) { + public static Optional getAnyAttribute(AnnotationExpr annotationExpr, String... keys) { for (String key : keys) { Optional optional = getAttribute(annotationExpr, key); - if(optional.isPresent()){ + if (optional.isPresent()) { return optional; } } diff --git a/apidoc-core/src/main/java/com/kim/apidoc/core/common/helper/ClassDeclarationHelper.java b/apidoc-core/src/main/java/com/kim/apidoc/core/common/helper/ClassDeclarationHelper.java index 2734e86..3ea0d10 100644 --- a/apidoc-core/src/main/java/com/kim/apidoc/core/common/helper/ClassDeclarationHelper.java +++ b/apidoc-core/src/main/java/com/kim/apidoc/core/common/helper/ClassDeclarationHelper.java @@ -1,15 +1,12 @@ package com.kim.apidoc.core.common.helper; import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; -import com.github.javaparser.ast.body.FieldDeclaration; import com.github.javaparser.ast.type.ClassOrInterfaceType; import com.github.javaparser.resolution.types.ResolvedReferenceType; import com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration; import com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration; import lombok.extern.slf4j.Slf4j; -import java.util.ArrayList; -import java.util.List; import java.util.Optional; @Slf4j @@ -28,9 +25,9 @@ public class ClassDeclarationHelper { if (resolvedReferenceType.getTypeDeclaration() instanceof JavaParserClassDeclaration) { JavaParserClassDeclaration typeDeclaration = (JavaParserClassDeclaration) resolvedReferenceType.getTypeDeclaration(); return Optional.of(typeDeclaration.getWrappedNode()); - }else if(resolvedReferenceType.getTypeDeclaration() instanceof ReflectionClassDeclaration){ + } else if (resolvedReferenceType.getTypeDeclaration() instanceof ReflectionClassDeclaration) { ReflectionClassDeclaration typeDeclaration = (ReflectionClassDeclaration) resolvedReferenceType.getTypeDeclaration(); - System.out.println("type Declaration:"+typeDeclaration); + System.out.println("type Declaration:" + typeDeclaration); //TODO } } catch (Exception e) { @@ -39,4 +36,16 @@ public class ClassDeclarationHelper { return Optional.empty(); } + public static String getClassOrInterfacePackageName(ClassOrInterfaceDeclaration classOrInterfaceDeclaration) { + final String[] result = {null}; + classOrInterfaceDeclaration.findCompilationUnit().ifPresent(x -> { + x.getPackageDeclaration().ifPresent(y -> { + result[0] = y.getNameAsString() + "." + classOrInterfaceDeclaration.getName(); + }); + }); + return result[0]; + + } + } + diff --git a/apidoc-core/src/main/java/com/kim/apidoc/core/common/helper/CommentHelper.java b/apidoc-core/src/main/java/com/kim/apidoc/core/common/helper/CommentHelper.java index bdfeb27..86e8f7e 100644 --- a/apidoc-core/src/main/java/com/kim/apidoc/core/common/helper/CommentHelper.java +++ b/apidoc-core/src/main/java/com/kim/apidoc/core/common/helper/CommentHelper.java @@ -26,7 +26,7 @@ public class CommentHelper { * @param description the description * @return string */ - public static String getDescription(JavadocDescription description){ + public static String getDescription(JavadocDescription description) { return description.getElements() .stream() .filter(e -> !(e instanceof JavadocInlineTag)) @@ -39,8 +39,8 @@ public class CommentHelper { * @param comment the comment * @return the string */ - public static String getContent(Comment comment){ - if(!comment.isJavadocComment()){ + public static String getContent(Comment comment) { + if (!comment.isJavadocComment()) { return comment.getContent(); } return getDescription(comment.asJavadocComment().parse().getDescription()); @@ -53,11 +53,11 @@ public class CommentHelper { * @param it the it * @return the string */ - public static String getComment(MethodUsage it){ + public static String getComment(MethodUsage it) { if (it.getDeclaration() instanceof JavaParserMethodDeclaration) { MethodDeclaration wrappedNode = ((JavaParserMethodDeclaration) it.getDeclaration()).getWrappedNode(); Optional optional = wrappedNode.getComment(); - if(optional.isPresent()){ + if (optional.isPresent()) { return CommentHelper.getContent(optional.get()); } } @@ -70,14 +70,14 @@ public class CommentHelper { * @param it the it * @return the string */ - public static String getComment(ResolvedFieldDeclaration it){ - if(it instanceof JavaParserFieldDeclaration){ + public static String getComment(ResolvedFieldDeclaration it) { + if (it instanceof JavaParserFieldDeclaration) { FieldDeclaration wrappedNode = ((JavaParserFieldDeclaration) it).getWrappedNode(); Optional optional = wrappedNode.getComment(); - if(optional.isPresent()){ + if (optional.isPresent()) { return CommentHelper.getContent(optional.get()); } - }else if(it instanceof JavaParserClassDeclaration){ + } else if (it instanceof JavaParserClassDeclaration) { JavaParserClassDeclaration classDeclaration = (JavaParserClassDeclaration) it; } return null; 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 ea24985..68304f2 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 @@ -10,9 +10,7 @@ import com.kim.apidoc.core.ApiDoc; import com.kim.apidoc.core.common.URI; import com.kim.apidoc.core.common.description.ObjectTypeDescription; import com.kim.apidoc.core.common.description.TypeDescription; -import com.kim.apidoc.core.common.helper.AnnotationHelper; -import com.kim.apidoc.core.common.helper.ExpressionHelper; -import com.kim.apidoc.core.common.helper.StringHelper; +import com.kim.apidoc.core.common.helper.*; import com.kim.apidoc.core.parser.ParserStrategy; import com.kim.apidoc.core.schema.Chapter; import com.kim.apidoc.core.schema.Header; @@ -313,6 +311,7 @@ public class SpringParser implements ParserStrategy { ClassOrInterfaceType returnType = new ClassOrInterfaceType(); returnType.setName(resultDataClassOrInterfaceDeclaration.getName()); returnType.setTypeArguments(n.getType()); + n.findCompilationUnit().get().addImport(ClassDeclarationHelper.getClassOrInterfacePackageName(resultDataClassOrInterfaceDeclaration)); n.setType(returnType); } TypeDescription description = ApiDoc.getInstance().getTypeResolvers().resolve(n.getType()); 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