3

您好,互联网上最有帮助的社区!

我正在开发一个可以读取 .java 文件并吐出数据的项目。我提取数据的程序之一并没有完全按照我的预期工作(非常陈词滥调)。它解析文件树中的所有 .java 文件,并使用链接哈希图在 .txt 文件中列出调用方 pkg.class.method 和调用 pkg.class.method 等数据。它做的一切都是正确的,除了它只给我一个方法调用的最后一个方法,而不是每个方法调用的所有方法。我认为这是因为我在linkedhashmap 中存储数据的方式,即一个键(调用方方法)和多个值(调用方方法)。

我希望文本文件列出从第一个遇到的每个方法调用的每个方法。有没有解决的办法?

这是代码:

import japa.parser.JavaParser;
import japa.parser.ast.CompilationUnit;
import japa.parser.ast.body.MethodDeclaration;
import japa.parser.ast.expr.MethodCallExpr;
import japa.parser.ast.visitor.VoidVisitorAdapter;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.LinkedHashMap;

/*--------------------------------------------------------------------------------------------------        
 *      TASK 3: Information Extraction, Method Caller and Method Callee
 *              This java code will extract data in 2 columns
 *              (Format:pkg.class.method). First column denotes the method
 *              from which another method is being called and the second
 *              column denotes the name of the called method.
 *--------------------------------------------------------------------------------------------------
 */

public class Task3 
{

    private static HashMap<String,String> methods = new LinkedHashMap<String,String>();

    static class ClassVisitor extends VoidVisitorAdapter<Object> 
    {
        private static String className = "";

        public void visit(japa.parser.ast.body.ClassOrInterfaceDeclaration n, Object arg) 
        {
            className = arg + "," + n.getName();
            new MethodVisitor().visit(n, className);
        }

    }

    static class MethodVisitor extends VoidVisitorAdapter<Object> {
        private static String methodName = "";

        @Override
        public void visit(MethodDeclaration n, Object arg) 
        {
            methodName = arg + "," + n.getName();
            new MethodCallVisitor().visit(n,methodName);
        }


    }

    static class MethodCallVisitor extends VoidVisitorAdapter<Object>{

        @Override
        public void visit(MethodCallExpr n, Object arg) 
        {
              String className=arg.toString();    
              methods.put(arg.toString(),className.substring(0,className.lastIndexOf(','))+","+n.getName());
        }


    }

    public static void main(String[] args) 
    {
        try 
        {
            ListFiles files = new ListFiles();
            String projPath = "C:\\JavaBook\\Final\\JMencode_v0.64_src\\";
            Path file = Paths.get(projPath);
            Files.walkFileTree(file, files);
            CompilationUnit compilationUnit = null;
            FileInputStream fileInputStream = null;
            String pkg = "";

            ClassVisitor codeVisitor = null;

            for (Path path : files.javaFiles) 
            {
                fileInputStream = new FileInputStream(path.toFile());

                try 
                {
                    compilationUnit = JavaParser.parse(fileInputStream);
                } 

                finally 
                {
                    fileInputStream.close();
                }

                pkg = compilationUnit.getPackage().getName().toString();
                codeVisitor = new ClassVisitor();
                codeVisitor.visit(compilationUnit, pkg);

            }

            File ouputFile = new File("C:\\JavaBook\\Final\\src\\pkg\\Task_1_2_3\\JMencode_v0.64_src_task3_" + methods.size() + ".txt");
            FileWriter fW = new FileWriter(ouputFile);

            fW.write("Caller Method" + " \t\t\t " + "Callee Method\n");

            for (String callerMeth : methods.keySet()) 
            {
                fW.write(callerMeth + " \t\t\t "+ methods.get(callerMeth)+"\n");
                System.out.println(callerMeth + " \t\t\t " + methods.get(callerMeth)+"\n");
            }

            fW.close();

        } 

        catch (Exception ex) 
        {
            System.out.println("Exception in ProjectInfo " + ex.getMessage());
        }

    }

}

谢谢大家!

4

1 回答 1

1

在这种情况下,有多个具有相同键的值,因此您应该像这样声明方法映射:

private static HashMap<String, Set<String>> methods = new LinkedHashMap<String, Set<String>>();

并将值存储在一组中:

Set<String> set = methods.get(arg.toString());

if (set == null) {
  set = new HashSet<String>();
  methods.put(arg.toString(), set);
}

set.add(className.substring(0,className.lastIndexOf(','))+","+n.getName());
于 2015-12-03T17:29:19.697 回答