Find out which classes of a given API are used

2019-01-07 16:49发布

In a Java Project of mine, I would like to find out programmatically which classes from a given API are used. Is there a good way to do that? Through source code parsing or bytecode parsing maybe? Because Reflection won't be of any use, I'm afraid.

To make things simpler: there are no wildcard imports (import com.mycompany.api.*;) anywhere in my project, no fully qualified field or variable definitions (private com.mycompany.api.MyThingy thingy;) nor any Class.forName(...) constructs. Given these limitations, it boils down to parsing import statements, I guess. Is there a preferred approach to do this?

8条回答
beautiful°
2楼-- · 2019-01-07 17:20

Something like this perhaps:

import java.io.*;
import java.util.Scanner;
import java.util.regex.Pattern;

public class FileTraverser {

    public static void main(String[] args) {
        visitAllDirsAndFiles(new File("source_directory"));
    }

    public static void visitAllDirsAndFiles(File root) {
        if (root.isDirectory())
            for (String child : root.list())
                visitAllDirsAndFiles(new File(root, child));
        process(root);
    }

    private static void process(File f) {

        Pattern p = Pattern.compile("(?=\\p{javaWhitespace}*)import (.*);");
        if (f.isFile() && f.getName().endsWith(".java")) {
            try {
                Scanner s = new Scanner(f);
                String cls = "";
                while (null != (cls = s.findWithinHorizon(p, 0)))
                    System.out.println(cls);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
        }
    }
}

You may want to take comments into account, but it shouldn't be too hard. You could also make sure you only look for imports before the class declaration.

查看更多
闹够了就滚
3楼-- · 2019-01-07 17:21

I use DependencyFinder exactly for that purpose. It can analyse bytecode and extract all dependencies, then dump a report in txt or xml format (see the DependencyExtractor tool). You should be able to programatically analyse the report from your application's code.

I have integrated this in my build process in order to check that certain APIs are NOT used by an application.

查看更多
4楼-- · 2019-01-07 17:24

I think the following might help you out:

  1. Class Dependency Analyzer
  2. Dependency Finder
查看更多
来,给爷笑一个
5楼-- · 2019-01-07 17:26
  1. Compiler Tree API
  2. byte code analysis - the fully qualified names should be in the constant pool
查看更多
可以哭但决不认输i
6楼-- · 2019-01-07 17:27

You may want to use STAN for that.

The "Couplings View" visualizes the dependencies to your API in a nice graph.

查看更多
小情绪 Triste *
7楼-- · 2019-01-07 17:32

If you use Eclipse. Try using the profiling tools. It doesn't only tells which classes are being used, but tells much more about it. The results will be something like:

alt text

There is a very good quickstart at:

http://www.eclipse.org/tptp/home/documents/tutorials/profilingtool/profilingexample_32.html

查看更多
登录 后发表回答