查找Java类中的所有依赖项 [英] Find all dependencies in a Java class

查看:483
本文介绍了查找Java类中的所有依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图获取Java类中的所有依赖项,包括用于泛型参数化和局部变量类型的类。我发现最好的框架是apache bcel 。使用它可以从字节码中轻松找到所有字段,方法参数和局部变量。基本上除泛型和局部变量类型之外的所有内容。例如,从行列表<点> point = new ArrayList< Point>(); 我只能从bcel找到一个依赖关系 - 使用 JavaClass.getConstantPool()方法的ArrayList。它不能检测到List接口和Point类。我也尝试过 tattletale 和CDA,不幸的是没有成功(同样的结果)。检查进口是不够的 - 我还需要同一个包的依赖关系,我不能接受通配符。我会感谢任何帮助。

I'm trying to get all dependencies in a Java class, including classes used for generics parametrization and local variables types. So far best framework I've found is apache bcel. Using it I can easily find all fields, method arguments and local variables from byte code. Basically everything except of generics and local variables types. For example, from line List<Point> points = new ArrayList<Point>(); I can only find one dependency - ArrayList using JavaClass.getConstantPool() method from bcel. It can't detect neither List interface nor Point class. I also tried tattletale and CDA, unfortunately without success (the same results). Examining imports is not enough - I need also dependencies from the same package and I can't accept wildcards. I would be grateful for any help.

推荐答案

我终于找到了解决方案。 ASM Bytecode Framework 是正确的工具。使用官方教程,右键示例很容易得到所有需要的依赖项。在该示例中,已经有一个访问者类 DependencyVisitor 这样做是我想要的。为了获得正确的格式,我不得不在 DependencyVistitor 示例代码中只更改一个方法,所以它只添加完整的类名,而不是包:

I've finally found solution. ASM Bytecode Framework is the right tool to use. Using official tutorial and right example it's quite easy to get all needed dependencies. In the example there is already a visitor class DependencyVisitor which does what I want. To get right formatting I had to change only one method in DependencyVistitor example code, so it adds full class names instead of packages only:

private String getGroupKey(String name)
{
        //Just comment that block so you can get full class names instead of package only
        /*
        int n = name.lastIndexOf('/');
        if (n > -1)
        {
            name = name.substring(0, n);
        }
       */
        // Replace resource char with package separator char        
        packages.add(name.replace("/", "."));
        //packages.add(name);
        return name;
}

查看 DependencyVisitor 代码,您可以轻松地了解它的作用,并根据您的需要进行修改。在我的示例类中运行它,它给我很好的,有用的输出:
[java.util.ArrayList,java.lang.Object,java.util.List,java.awt.Point,goobar .test.asmhello.TestClass,java.lang.String,java.lang.Integer,java.awt.Graphics,goobar.test.asmhello.TestClass2] 。它包含我使用的每个类和接口,用于泛型参数化的每种类型。

Looking at DependencyVisitor code you can easily understand what it does and modify it to your needs. Running it on my example class it gives me nice, useful output: [java.util.ArrayList, java.lang.Object, java.util.List, java.awt.Point, goobar.test.asmhello.TestClass, java.lang.String, java.lang.Integer, java.awt.Graphics, goobar.test.asmhello.TestClass2]. It contains every class and interface I've used and every type used for generics parameterization.

这篇关于查找Java类中的所有依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆