java - 获取方法的类文件的绝对路径

标签 java eclipse methods path

我有一个代码示例,它浏览我的工作区并选择每个类中的每个方法。 我的目的是打印出该方法的绝对路径,但我不知道如何处理它。

类似 method.getAbsolutePath() 的东西不存在。

我的代码:

IPackageFragment[] packages = javaProject.getPackageFragments();
for (IPackageFragment mypackage : packages) {
    for (ICompilationUnit unit : mypackage.getCompilationUnits()) {
        IType[] allTypes = unit.getAllTypes();
        for (IType type : allTypes) {
            IMethod[] methods = type.getMethods();
            for (IMethod method : methods) {
                //System.out.println(MyMethodPath);
                //here is want to print the path
            }
        }
    } 
}

有人可以帮忙吗?

最佳答案

您可以通过 IMethod.getClassFile 获取类(class),然后可以使用 IClassFile.getPath 来获取绝对路径,或更重要的是:

System.out.println(method.getClassFile().getPath().toString());

关于java - 获取方法的类文件的绝对路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17305607/

相关文章:

使用 netbeans 运行 maven jgroups 项目构建时出现 java.lang.NoClassDefFoundError

java:将 HTML 转换为 PDF 无法处理 <strong> 和 <em> whth itext

visual-studio - Visual Studio "Go to implementation "

java - 实现与 SimpleDateFormat 相等的 DateTimeFormatter Y10K 输出?

java - 编写自定义 Android ViewAction 以截取屏幕截图

java - Eclipse 不更新 JSP scriptlet 中的自定义方法

java - Eclipse : Failed to connect to remote VM. 连接被拒绝。

eclipse - 使用 Maven war 插件过滤 Web 资源在带有 m2e 的 Eclipse 中不起作用

java - 如何将这些变量从不同的方法传递到其他方法

C++编译问题;类方法