gpt4 book ai didi

java - .java 报告生成、方法调用者和被调用者

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:56:27 25 4
gpt4 key购买 nike

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

我正在开发一个可以读取 .java 文件并输出数据的项目。我的一个提取数据的程序并没有完全按照我的预期工作(非常陈词滥调)。它解析文件树中的所有 .java 文件,并使用 linkedhashmap 在 .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());
}

}

}

提前谢谢大家!

最佳答案

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

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());

关于java - .java 报告生成、方法调用者和被调用者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34071560/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com