OGrandeDiEnne
OGrandeDiEnne

Reputation: 888

How can I (statically) analyze java code and extract actual parameter values in constructors or methods?

I would like to have a tool which finds all the occurrences of a (Java) constructor/method and reports the actual parameter values for each invocation.

I guess there's not something already prepared and also that I will need Static Analysis.

Which library/program do you suggest to use ?

(I have to analyse Java code but can use any language to write the analyser)

Upvotes: 2

Views: 549

Answers (2)

David Newcomb
David Newcomb

Reputation: 10943

You could use AspectJ. Define a Pointcut on constructors. Then write an aspect to execute on each invocation. Then apply the aspect to the entire project.

The code inside your aspect is given details to the call chain so you can interrogate the parameters yourself and log them anyway you want.

Upvotes: 1

Ira Baxter
Ira Baxter

Reputation: 95334

You need a tool that can parse Java, builds an AST capturing the code (so you can search the code for method calls), can resolve type and names (so you know the meaning of each symbol instance and can determine that a specific identifier names the method(s) of interest, not just a symbol with accidentally the same name), and can help you generate the method call.

The Java compiler offers APIs to support some of this. I don't know that it will help you regenerate the method call text.

Our DMS Software Reengineering Toolkit provides general program analysis and transformation capabilities, including parsing, AST building/visiting, symbol table construction, and prettyprinting (regeneration of text) for arbitrary subtrees. It has a full Java front end providing full name and type resolution. It would be pretty straightforward to code an accurate, reliable tool to do what you want with the support DMS provides.

Upvotes: 0

Related Questions