Search in sources :

Example 1 with Javadoc

use of act.apidoc.javadoc.Javadoc in project actframework by actframework.

the class ApiManager method exploreDeclaration.

private void exploreDeclaration(ClassOrInterfaceDeclaration classDeclaration, Map<String, Javadoc> methodJavaDocs, String prefix) {
    String className = classDeclaration.getName();
    String newPrefix = S.blank(prefix) ? className : S.concat(prefix, ".", className);
    for (Node node : classDeclaration.getChildrenNodes()) {
        if (node instanceof ClassOrInterfaceDeclaration) {
            exploreDeclaration((ClassOrInterfaceDeclaration) node, methodJavaDocs, newPrefix);
        } else if (node instanceof MethodDeclaration) {
            MethodDeclaration methodDeclaration = (MethodDeclaration) node;
            List<AnnotationExpr> annoList = methodDeclaration.getAnnotations();
            boolean needJavadoc = false;
            if (null != annoList && !annoList.isEmpty()) {
                for (AnnotationExpr anno : annoList) {
                    String annoName = anno.getName().getName();
                    if (actionAnnotations.contains(annoName)) {
                        needJavadoc = true;
                        break;
                    }
                }
            }
            if (!needJavadoc) {
                continue;
            }
            Comment comment = methodDeclaration.getComment();
            if (!(comment instanceof JavadocComment)) {
                continue;
            }
            JavadocComment javadocComment = (JavadocComment) comment;
            Javadoc javadoc = JavadocParser.parse(javadocComment);
            methodJavaDocs.put(S.concat(newPrefix, ".", methodDeclaration.getName()), javadoc);
        }
    }
}
Also used : JavadocComment(com.github.javaparser.ast.comments.JavadocComment) Comment(com.github.javaparser.ast.comments.Comment) JavadocComment(com.github.javaparser.ast.comments.JavadocComment) AnnotationExpr(com.github.javaparser.ast.expr.AnnotationExpr) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) MethodDeclaration(com.github.javaparser.ast.body.MethodDeclaration) Node(com.github.javaparser.ast.Node) Javadoc(act.apidoc.javadoc.Javadoc)

Example 2 with Javadoc

use of act.apidoc.javadoc.Javadoc in project actframework by actframework.

the class ApiManager method exploreDescriptions.

private void exploreDescriptions(Set<Class> controllerClasses) {
    DevModeClassLoader cl = $.cast(Act.app().classLoader());
    Map<String, Javadoc> methodJavaDocs = new HashMap<>();
    for (Class controllerClass : controllerClasses) {
        Source src = cl.source(controllerClass);
        if (null == src) {
            continue;
        }
        try {
            CompilationUnit compilationUnit = JavaParser.parse(IO.reader(src.code()), true);
            List<TypeDeclaration> types = compilationUnit.getTypes();
            for (TypeDeclaration type : types) {
                if (type instanceof ClassOrInterfaceDeclaration) {
                    exploreDeclaration((ClassOrInterfaceDeclaration) type, methodJavaDocs, "");
                }
            }
        } catch (Exception e) {
            LOGGER.warn(e, "error parsing source for " + controllerClass);
        }
    }
    for (Endpoint endpoint : endpoints) {
        Javadoc javadoc = methodJavaDocs.get(endpoint.getId());
        if (null != javadoc) {
            String desc = javadoc.getDescription().toText();
            if (S.notBlank(desc)) {
                endpoint.setDescription(desc);
            }
            List<ParamInfo> params = endpoint.getParams();
            if (params.isEmpty()) {
                continue;
            }
            Map<String, ParamInfo> paramLookup = new HashMap<>();
            for (ParamInfo param : params) {
                paramLookup.put(param.getName(), param);
            }
            List<JavadocBlockTag> blockTags = javadoc.getBlockTags();
            for (JavadocBlockTag tag : blockTags) {
                if ("param".equals(tag.getTagName())) {
                    String paramName = tag.getName().get();
                    ParamInfo paramInfo = paramLookup.get(paramName);
                    if (null != paramInfo) {
                        paramInfo.setDescription(tag.getContent().toText());
                    }
                }
            }
        }
    }
}
Also used : CompilationUnit(com.github.javaparser.ast.CompilationUnit) ClassOrInterfaceDeclaration(com.github.javaparser.ast.body.ClassOrInterfaceDeclaration) Javadoc(act.apidoc.javadoc.Javadoc) JavadocBlockTag(act.apidoc.javadoc.JavadocBlockTag) TypeDeclaration(com.github.javaparser.ast.body.TypeDeclaration) ParamInfo(act.apidoc.Endpoint.ParamInfo)

Aggregations

Javadoc (act.apidoc.javadoc.Javadoc)2 ClassOrInterfaceDeclaration (com.github.javaparser.ast.body.ClassOrInterfaceDeclaration)2 ParamInfo (act.apidoc.Endpoint.ParamInfo)1 JavadocBlockTag (act.apidoc.javadoc.JavadocBlockTag)1 CompilationUnit (com.github.javaparser.ast.CompilationUnit)1 Node (com.github.javaparser.ast.Node)1 MethodDeclaration (com.github.javaparser.ast.body.MethodDeclaration)1 TypeDeclaration (com.github.javaparser.ast.body.TypeDeclaration)1 Comment (com.github.javaparser.ast.comments.Comment)1 JavadocComment (com.github.javaparser.ast.comments.JavadocComment)1 AnnotationExpr (com.github.javaparser.ast.expr.AnnotationExpr)1