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 3f1aecc91e68ebd831829131d97333199ddf4199..a5393650a378e9ff884507fb52d36d8f4d4ef278 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 2734e86d1326e0ee71208382301ae7247d643737..3ea0d10bc02960da5803104b0f806dc2baecdd9e 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 bdfeb278097a51543dcc64c9c65795fe2d8d0f44..86e8f7ea5441c0a7f08f1bc3c97adb3930e679f5 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 ea249855a4b79c58051d230cbdbc66947b2d1c3c..68304f2c3688ba913f1f04808eff032fd680c972 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 824168dc587a2d69e3c03bd4eed31e6601f1ec2d..23b6e0d983d6a4de9e38c0b0e3ec12366c68f807 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);