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);
}
}
What your trying to do is called static code analysis - specifically data flow analysis, but with a twist...you didn't show you are looking at source code, but at compiled code...if you want to do it at runtime, where you're having to deal with compiled (bytecode) code instead of source. So, you're looking for a library capable of bytecode data-flow analysis. There are quite a few libraries out there to help (now that you know what to search for, you can find alternatives to my recommendation if you would like).
OK, not getting to an example...I like javassist - I find it to be as clear as a bytecode library can be with great examples and documentation online. javassit has some higher-level bytecode analysis API, so you might not even have to dig down too deep, depending on what you need to do.
To print output for your Foo/Bar example above, use the following code:
will print:
If you need more detail, you'll need to actually read the bytecode, and have the JVM specification handy:
I hope this helps get you started =)
There is a problem in method due to made in static type . Static Method will call first Execute at the starting time of class So all will Execute at the first stage and will not able to second time call due to static qualities of method . So main method will not able to call the above method.
OP Answer for reference:
The goal is to get this to work:
Which will print the objects own dependency. To do this you need:
In depth knowledge of the ASM Tree API:
http://asm.ow2.org/
Methods of opening and accessing Jar contents, including
A JNI Signature parser
http://journals.ecs.soton.ac.uk/java/tutorial/native1.1/implementing/method.html
And a graph framework such as
http://jgrapht.org/
What you need is construct a call graph, and then ask if two nodes (a caller and callee) are connected in the call graph. This isn't an easy task.
What you need to do:
[You might be able to avoid the parsing/name-type resolution part of the above using Wala, which is constructed essentially by doing most of the above].
With the call graph, if you want to know if A can call B, find the node for A in the call graph, and see if there is a path to B.
Another note here suggests this is a 6 month task for a compiler class. I think it is 6 months for an experienced compiler person, or more (and we haven't addressed nasty problems such as class loaders and reflective calls).
I think you are better off finding a solution for this, that somebody else has already built. Likely somebody has; not so likely it is easily found or she wants to part with it. You might find implementations done in Univerisities; there are all kinds of papers written by academics (and supported by a prototype) to compute object-graphs. The down side is all those systems are prototypes, and being build by small, unpaid teams of graduates, they usually don't handle all the edge cases let alone the latest version of Java (lambdas, anyone?)
You can use ASM api to find information on a class file, The sample code gives a fair idea on how to get the method details.
Analyzer class
Bar Class
Foo Class
I think you can get all information from stacktrace, if you call any method. When we get any exception we can see stack trace using printStackTrace(); method. This is not an answer but it can help you to find you a solution for your problem.