Find out if one method could call another

2019-03-29 01:19发布

I am trying to figure out how to take a Java pojo, and analyze its method for all other methods and function it could call. For example, here is a hardcoded example of the output. How can I make this general? I need to analyze Java objects programmatically to determine what methods they could call if executed. Example:

package com.example.analyze;

public class Main
{

    private static class Foo {

        public void foo(int value, Bar bar) {
            if(value > 5)
                bar.gaz();
        }
    }

    private static class Bar {

        public void gaz() {
            System.out.println("gaz");
        }
    }

    private static class Analyzer {

        public void analyze(Object object){
            System.out.println("Object method foo could call Bar method gaz");
        }

    }

    public static void main(String[] args)
    {
        Foo foo = new Foo();
        Analyzer analyzer = new Analyzer();
        analyzer.analyze(foo);
    }
}

7条回答
Anthone
2楼-- · 2019-03-29 02:09

This is quite tough - You will need to use Java Reflect API and do some heavy parsing and a lot of work a compiler would do. Instead you could just use one of the many Java Dependency tools/plugins already available (like JDepend from https://stackoverflow.com/a/2366872/986160)

查看更多
登录 后发表回答