-2

I have to find usage of methods in java.

The requirement is to write metacode which can look in to code and find out methods which are used and where. In eclipse we can find it : Find methods calls in Eclipse project

I have tried manually parsing the code and the java parser api : http://code.google.com/p/javaparser/.

I found both the appoach difficult and involves lot of scenario to be handled.

Any APIs which can be used or i have to manually parse the code to find the usages?

Community
  • 1
  • 1
gauchy
  • 65
  • 8
  • I dont know why it got downvoted , question is perfectly valid(just that i did not add i tried X and Y) , Anyways adding that i tried X an Y :) – gauchy Jul 16 '14 at 11:43
  • 1
    I would imagine the downvotes are because the question appears to be off-topic ("*Any APIs which can be used or i have to manually parse the code to find the usages?*" looks to me like a request for us to recommend a library, which is off-topic on StackOverflow). – JonK Jul 16 '14 at 12:47

2 Answers2

1

here is the list of open source java code analyzers: http://java-source.net/open-source/code-analyzers

check source code of them, for instance PMD

1

If you can compile the code before analyzing it, then you can examine the bytecode in order to find method invocations.

For example, you could use the ObjectWeb ASM library. Using this library, you would need to write code that reads a class file. Then, you could examine each method in the class file in order to find INVOKESTATIC, INVOKEVIRTUAL, INVOKEINTERFACE, and INVOKESPECIAL bytecode instructions. Those are the bytecode instructions that are used to invoke methods. Each instruction has the name of the invoked method, the name of declaring class/interface, and its descriptor associated with the instruction.

This may sound rather complex, but it really is not.

Mackenzie
  • 1,897
  • 1
  • 19
  • 25