Commit 638a3728 authored by duanledexianxianxian's avatar duanledexianxianxian 😁

Refactoring code.

parent ac2eb0d4
...@@ -24,7 +24,7 @@ public class AnnotationHelper { ...@@ -24,7 +24,7 @@ public class AnnotationHelper {
} }
if (annotationExpr.isNormalAnnotationExpr()) { if (annotationExpr.isNormalAnnotationExpr()) {
for (MemberValuePair pair : annotationExpr.asNormalAnnotationExpr().getPairs()) { for (MemberValuePair pair : annotationExpr.asNormalAnnotationExpr().getPairs()) {
if (Objects.equals(key, pair.getNameAsString())){ if (Objects.equals(key, pair.getNameAsString())) {
return Optional.of(pair.getValue()); return Optional.of(pair.getValue());
} }
} }
...@@ -32,10 +32,10 @@ public class AnnotationHelper { ...@@ -32,10 +32,10 @@ public class AnnotationHelper {
return Optional.empty(); return Optional.empty();
} }
public static Optional<Expression> getAnyAttribute(AnnotationExpr annotationExpr, String ... keys) { public static Optional<Expression> getAnyAttribute(AnnotationExpr annotationExpr, String... keys) {
for (String key : keys) { for (String key : keys) {
Optional<Expression> optional = getAttribute(annotationExpr, key); Optional<Expression> optional = getAttribute(annotationExpr, key);
if(optional.isPresent()){ if (optional.isPresent()) {
return optional; return optional;
} }
} }
......
package com.kim.apidoc.core.common.helper; package com.kim.apidoc.core.common.helper;
import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration; 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.ast.type.ClassOrInterfaceType;
import com.github.javaparser.resolution.types.ResolvedReferenceType; import com.github.javaparser.resolution.types.ResolvedReferenceType;
import com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration; import com.github.javaparser.symbolsolver.javaparsermodel.declarations.JavaParserClassDeclaration;
import com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration; import com.github.javaparser.symbolsolver.reflectionmodel.ReflectionClassDeclaration;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional; import java.util.Optional;
@Slf4j @Slf4j
...@@ -28,9 +25,9 @@ public class ClassDeclarationHelper { ...@@ -28,9 +25,9 @@ public class ClassDeclarationHelper {
if (resolvedReferenceType.getTypeDeclaration() instanceof JavaParserClassDeclaration) { if (resolvedReferenceType.getTypeDeclaration() instanceof JavaParserClassDeclaration) {
JavaParserClassDeclaration typeDeclaration = (JavaParserClassDeclaration) resolvedReferenceType.getTypeDeclaration(); JavaParserClassDeclaration typeDeclaration = (JavaParserClassDeclaration) resolvedReferenceType.getTypeDeclaration();
return Optional.of(typeDeclaration.getWrappedNode()); return Optional.of(typeDeclaration.getWrappedNode());
}else if(resolvedReferenceType.getTypeDeclaration() instanceof ReflectionClassDeclaration){ } else if (resolvedReferenceType.getTypeDeclaration() instanceof ReflectionClassDeclaration) {
ReflectionClassDeclaration typeDeclaration = (ReflectionClassDeclaration) resolvedReferenceType.getTypeDeclaration(); ReflectionClassDeclaration typeDeclaration = (ReflectionClassDeclaration) resolvedReferenceType.getTypeDeclaration();
System.out.println("type Declaration:"+typeDeclaration); System.out.println("type Declaration:" + typeDeclaration);
//TODO //TODO
} }
} catch (Exception e) { } catch (Exception e) {
...@@ -39,4 +36,16 @@ public class ClassDeclarationHelper { ...@@ -39,4 +36,16 @@ public class ClassDeclarationHelper {
return Optional.empty(); 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];
}
} }
...@@ -26,7 +26,7 @@ public class CommentHelper { ...@@ -26,7 +26,7 @@ public class CommentHelper {
* @param description the description * @param description the description
* @return string * @return string
*/ */
public static String getDescription(JavadocDescription description){ public static String getDescription(JavadocDescription description) {
return description.getElements() return description.getElements()
.stream() .stream()
.filter(e -> !(e instanceof JavadocInlineTag)) .filter(e -> !(e instanceof JavadocInlineTag))
...@@ -39,8 +39,8 @@ public class CommentHelper { ...@@ -39,8 +39,8 @@ public class CommentHelper {
* @param comment the comment * @param comment the comment
* @return the string * @return the string
*/ */
public static String getContent(Comment comment){ public static String getContent(Comment comment) {
if(!comment.isJavadocComment()){ if (!comment.isJavadocComment()) {
return comment.getContent(); return comment.getContent();
} }
return getDescription(comment.asJavadocComment().parse().getDescription()); return getDescription(comment.asJavadocComment().parse().getDescription());
...@@ -53,11 +53,11 @@ public class CommentHelper { ...@@ -53,11 +53,11 @@ public class CommentHelper {
* @param it the it * @param it the it
* @return the string * @return the string
*/ */
public static String getComment(MethodUsage it){ public static String getComment(MethodUsage it) {
if (it.getDeclaration() instanceof JavaParserMethodDeclaration) { if (it.getDeclaration() instanceof JavaParserMethodDeclaration) {
MethodDeclaration wrappedNode = ((JavaParserMethodDeclaration) it.getDeclaration()).getWrappedNode(); MethodDeclaration wrappedNode = ((JavaParserMethodDeclaration) it.getDeclaration()).getWrappedNode();
Optional<Comment> optional = wrappedNode.getComment(); Optional<Comment> optional = wrappedNode.getComment();
if(optional.isPresent()){ if (optional.isPresent()) {
return CommentHelper.getContent(optional.get()); return CommentHelper.getContent(optional.get());
} }
} }
...@@ -70,14 +70,14 @@ public class CommentHelper { ...@@ -70,14 +70,14 @@ public class CommentHelper {
* @param it the it * @param it the it
* @return the string * @return the string
*/ */
public static String getComment(ResolvedFieldDeclaration it){ public static String getComment(ResolvedFieldDeclaration it) {
if(it instanceof JavaParserFieldDeclaration){ if (it instanceof JavaParserFieldDeclaration) {
FieldDeclaration wrappedNode = ((JavaParserFieldDeclaration) it).getWrappedNode(); FieldDeclaration wrappedNode = ((JavaParserFieldDeclaration) it).getWrappedNode();
Optional<Comment> optional = wrappedNode.getComment(); Optional<Comment> optional = wrappedNode.getComment();
if(optional.isPresent()){ if (optional.isPresent()) {
return CommentHelper.getContent(optional.get()); return CommentHelper.getContent(optional.get());
} }
}else if(it instanceof JavaParserClassDeclaration){ } else if (it instanceof JavaParserClassDeclaration) {
JavaParserClassDeclaration classDeclaration = (JavaParserClassDeclaration) it; JavaParserClassDeclaration classDeclaration = (JavaParserClassDeclaration) it;
} }
return null; return null;
......
...@@ -10,9 +10,7 @@ import com.kim.apidoc.core.ApiDoc; ...@@ -10,9 +10,7 @@ import com.kim.apidoc.core.ApiDoc;
import com.kim.apidoc.core.common.URI; import com.kim.apidoc.core.common.URI;
import com.kim.apidoc.core.common.description.ObjectTypeDescription; import com.kim.apidoc.core.common.description.ObjectTypeDescription;
import com.kim.apidoc.core.common.description.TypeDescription; import com.kim.apidoc.core.common.description.TypeDescription;
import com.kim.apidoc.core.common.helper.AnnotationHelper; import com.kim.apidoc.core.common.helper.*;
import com.kim.apidoc.core.common.helper.ExpressionHelper;
import com.kim.apidoc.core.common.helper.StringHelper;
import com.kim.apidoc.core.parser.ParserStrategy; import com.kim.apidoc.core.parser.ParserStrategy;
import com.kim.apidoc.core.schema.Chapter; import com.kim.apidoc.core.schema.Chapter;
import com.kim.apidoc.core.schema.Header; import com.kim.apidoc.core.schema.Header;
...@@ -313,6 +311,7 @@ public class SpringParser implements ParserStrategy { ...@@ -313,6 +311,7 @@ public class SpringParser implements ParserStrategy {
ClassOrInterfaceType returnType = new ClassOrInterfaceType(); ClassOrInterfaceType returnType = new ClassOrInterfaceType();
returnType.setName(resultDataClassOrInterfaceDeclaration.getName()); returnType.setName(resultDataClassOrInterfaceDeclaration.getName());
returnType.setTypeArguments(n.getType()); returnType.setTypeArguments(n.getType());
n.findCompilationUnit().get().addImport(ClassDeclarationHelper.getClassOrInterfacePackageName(resultDataClassOrInterfaceDeclaration));
n.setType(returnType); n.setType(returnType);
} }
TypeDescription description = ApiDoc.getInstance().getTypeResolvers().resolve(n.getType()); TypeDescription description = ApiDoc.getInstance().getTypeResolvers().resolve(n.getType());
......
...@@ -17,7 +17,7 @@ public class SpringTest { ...@@ -17,7 +17,7 @@ public class SpringTest {
Context context = new Context(); Context context = new Context();
context.setId("test"); context.setId("test");
context.setName("测试项目"); 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"); // context.setCss("https://darshandsoni.com/asciidoctor-skins/css/monospace.css");
ApiDoc apigcc = new ApiDoc(context); ApiDoc apigcc = new ApiDoc(context);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment