How to infer the types of all the parameters of a function using java-parser and java-symbol-solver?

1.3k views Asked by At

Suppose I have the following Class A in pkg1 and a Class B in pkg2 and function calculate() is defined in pkg2 class B as follows:

B.calculate(A a1, A a2);

I want to identify the the type of parameters a1 and a2. For instance, here it will be pkg1.A and pkg1.A as class A belongs to pkg1.

My main concern is, I do not want to build the project, I want to do static analysis(I have all the source files).

I know a way to infer the the parameter types using Eclipse JDT but how do I do it using javaparser(https://github.com/javaparser/javaparser) and javasymbolsolver(https://github.com/javaparser/javasymbolsolver).

In Eclipse JDT, i have just set the source files' path in the parser environment and it worked. But in javasymbolsolver, even after I add the source files in the typesolver, it is not working.

CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver();
combinedTypeSolver.add(new JavaParserTypeSolver("path to the sourcefiles"));
JavaParserFacade j=JavaParserFacade.get(combinedTypeSolver);
SymbolReference<MethodDeclaration> ref = j.solve((MethodCallExpr) n);
1

There are 1 answers

4
Federico Tomassetti On

You need to specify also the path to the dependencies. The CombinedTypeSolver should know where to find not only the source files but also all the jars.

Something like this should work:

combinedTypeSolver.add(new JarTypeSolver(new File("libs/mylib.jar")));

Source: I am the mantainer of JavaSymbolSolver