• JavaParse入门


    需求

    工作时有需求获取java文件成员变量的注释,故而研究了一系列开源产品,之所以选择JavaParse,是因为博客上文件比较多。。。

    多归多,但也版本杂乱,走了不少歪路。

    版本2.x和版本3.x还是有较大不同的,有需要的人可以参考。

    版本2.x

    
    <dependency>
    	<groupId>com.github.javaparsergroupId>
    	<artifactId>javaparser-coreartifactId>
    	<version>2.2.1version>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    package cn.com.infosec.netseal.webserver.util;
    
    import cn.com.infosec.netseal.common.util.StringUtils;
    import com.github.javaparser.JavaParser;
    import com.github.javaparser.ast.CompilationUnit;
    import com.github.javaparser.ast.Node;
    import com.github.javaparser.ast.body.TypeDeclaration;
    import com.github.javaparser.ast.comments.Comment;
    
    import java.io.File;
    import java.util.List;
    
    public class JavaParseTest {
        public static void main(String[] args) throws Exception {
            String classPath = "E:\\Idea\\NetSeal\\v7\\src\\netseal\\netseal-common\\src\\main\\java\\cn\\com\\infosec\\netseal\\common\\resource\\errCode\\ErrSubType.java";
            String mapName = "subTypeMap";
    
            CompilationUnit parse = JavaParser.parse(new File(classPath));
    
            // 获取指定文件中的所有类
            List<TypeDeclaration> types = parse.getTypes();
            for (TypeDeclaration type : types) {
                // 获取类名/类注释
                String className = type.getName();
                type.getComment();
    
                // 获取类里的变量、方法等子节点
                List<Node> list = type.getChildrenNodes();
                System.out.println("总个数:" + list.size());
                for (Node node : list) {
                    if (node.toString().indexOf("(") != -1)
                        continue;
    
                    // 获取变量的类型、变量名称和变量值;
                    List<Node> varTypeAndKV = node.getChildrenNodes();
                    Node varKV = varTypeAndKV.get(1);
                    String varK = varKV.toString().split("=")[0].trim();
    
                    // 获取变量注释
                    String content = "";
                    Comment comment = node.getComment();
                    if (comment != null && comment.getContent() != "") {
                        content = comment.getContent().replace("*", "").trim();
                    }
    
                    System.out.println(StringUtils.format("{}.put({}.{}, \"{}\");", mapName, className, varK, content));
    
                }
            }
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53

    版本3.x

    <dependency>
    	<groupId>com.github.javaparsergroupId>
    	<artifactId>javaparser-coreartifactId>
    	<version>3.6.16version>
    	<scope>compilescope>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    package cn.com.infosec.netseal.appserver.util;
    
    import cn.com.infosec.netseal.common.util.StringUtils;
    import com.alibaba.fastjson.JSON;
    import com.github.javaparser.JavaParser;
    import com.github.javaparser.ast.CompilationUnit;
    import com.github.javaparser.ast.Node;
    import com.github.javaparser.ast.NodeList;
    import com.github.javaparser.ast.body.ClassOrInterfaceDeclaration;
    import com.github.javaparser.ast.body.FieldDeclaration;
    import com.github.javaparser.ast.body.TypeDeclaration;
    import com.github.javaparser.ast.body.VariableDeclarator;
    import com.github.javaparser.ast.comments.Comment;
    import com.github.javaparser.ast.expr.SimpleName;
    
    import java.io.File;
    import java.util.List;
    import java.util.Optional;
    
    public class JavaParseTest {
        public static void main(String[] args) throws Exception {
            String classPath = "E:\\Idea\\NetSeal\\v7\\src\\netseal\\netseal-common\\src\\main\\java\\cn\\com\\infosec\\netseal\\common\\resource\\errCode\\ErrSubType.java";
            String mapName = "subTypeMap";
    
            CompilationUnit parse = JavaParser.parse(new File(classPath));
    
            Optional<ClassOrInterfaceDeclaration> errSubType = parse.getClassByName("ErrSubType");
    
            errSubType.ifPresent((c) -> {
                // 获取类名/类注释
                String className = c.getName().toString();
                c.getComment();
    
                // 获取类里的变量、方法等子节点
                List<Node> list = c.getChildNodes();
                System.out.println("总个数:" + list.size());
                for (Node nodeTmp : list) {
                    if (!(nodeTmp instanceof FieldDeclaration))
                        continue;
    
                    FieldDeclaration node = (FieldDeclaration) nodeTmp;
    
                    // 获取变量的类型、变量名称和变量值;
                    VariableDeclarator variable = node.getVariable(0);
                    String  varName = variable.getNameAsString();
                    String  varType = variable.getTypeAsString();
    
                    // 获取变量注释
                    String content = "";
                    Optional<Comment> comment = node.getComment();
                    if (comment != null && comment.get().getContent() != "") {
                        content = comment.get().getContent().replace("*", "").trim();
                    }
    
                    System.out.println(StringUtils.format("{}.put({}.{}, \"{}\");", mapName, className, varName, content));
    
                }
            });
    
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
  • 相关阅读:
    什么是运维自动化巡检?
    .net在使用存储过程中IN参数的拼接方案,使用Join()方法
    EasyCVR视频调阅页面如何正确关闭正在播放的视频?
    MNN编译
    blender怎么设置中文界面
    Linux 线程互斥
    【云原生 • Kubernetes】kubernetes 核心技术 - 持久化存储
    软件测试工程师-月入20k+,你要知道的
    使用Intellij IDEA远程debug服务器Java代码
    E: Unable to locate package libboost-all-dev
  • 原文地址:https://blog.csdn.net/qq_42873640/article/details/126230801