美文网首页
使用JDT 提取Java方法体以及对应注释

使用JDT 提取Java方法体以及对应注释

作者: 你好宝宝 | 来源:发表于2020-03-05 20:34 被阅读0次
    package app;
    
    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.util.List;
    import java.util.Map;
    import java.util.HashMap;
    
    import org.eclipse.jdt.core.dom.AST;
    import org.eclipse.jdt.core.dom.ASTParser;
    import org.eclipse.jdt.core.dom.CompilationUnit;
    import org.eclipse.jdt.core.dom.TypeDeclaration;
    import org.eclipse.jdt.core.dom.MethodDeclaration;
    import org.eclipse.jdt.core.dom.Javadoc;
    
    import net.sf.json.JSONObject;
    
    
    public class ParseJavaFile {
        public static void main(String[] args) throws IOException {
            String[] paths={"a.java"};
            String write_to="a.json";
            extractMethodAndComment(paths,write_to);
        }
    
        /**
         * 将方法体以及对应的注释以json的方式存储
         * @param pathFrom 原始数据的文件列表
         * @param pathTo    用于存储的json文件位置
         */
        public static void extractMethodAndComment(String[] pathFrom, String pathTo) throws IOException {
            for (String path : pathFrom) {
                ASTParser astParser = ASTParser.newParser(AST.JLS3);
                astParser.setKind(ASTParser.K_COMPILATION_UNIT);
                astParser.setSource(readFileToString(path).toCharArray());
                CompilationUnit unit = (CompilationUnit) astParser.createAST(null);
                TypeDeclaration type = (TypeDeclaration) unit.types().get(0);
                MethodDeclaration[] methods = type.getMethods();
               
                FileWriter fw=new FileWriter(new File(pathTo),true); //追加的方式创建写指针
                BufferedWriter  bw=new BufferedWriter(fw);
    
                for (int i = 0; i < methods.length; i++) {
                    MethodDeclaration method = methods[i];
                    if (method.isConstructor()){ // 过滤掉构造函数
                         continue;
                    }
                    Javadoc doc = method.getJavadoc();
                    String comment = null;
                    if (doc != null) {
                        comment = doc.tags().get(0).toString().replace("*", "");
                    } else {
                        continue;
                    }
                    String methodBody = method.toString().replace(doc.toString(), "");
    
    
                    //转化成json对象写出
                    Map<String,String> params=new HashMap<>();
                    params.put("comment", comment);
                    params.put("method", methodBody);
                    JSONObject jsonObject=JSONObject.fromObject(params);
                    String jsonStr=jsonObject.toString();
                    bw.write(jsonStr +"\t\n");
    
                }
                bw.close();
                fw.close();
            }
        }
    
        //将文件转化为字符串
        public static String readFileToString(String filePath) throws IOException {
            StringBuilder fileData = new StringBuilder(1000);
            BufferedReader reader = new BufferedReader(new FileReader(filePath));
    
            char[] buf = new char[10];
            int numRead = 0;
            while ((numRead = reader.read(buf)) != -1) {
                String readData = String.valueOf(buf, 0, numRead);
                fileData.append(readData);
                buf = new char[1024];
            }
    
            reader.close();
    
            return fileData.toString();
        }
    
    }
    

    相关文章

      网友评论

          本文标题:使用JDT 提取Java方法体以及对应注释

          本文链接:https://www.haomeiwen.com/subject/eldhrhtx.html