This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Analysing the content of a Java method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Analysing the content of a Java method" Watch "Analysing the content of a Java method" New topic
Author

Analysing the content of a Java method

Andy Young
Greenhorn

Joined: Nov 03, 2008
Posts: 9
Hi guys,

I am trying to look at the content of a Java method in order to analyse how "complex" it is. E.g. how many If, while, switch etc. statements there are. In addition to this, I would also like to know how many times a method is called from within a package (much like the Netbeans "Find Usages" functionality).

My initial idea is that I need to create a parser for the Java language (probably using ANTLR) though I'm not entirely sure this will do what I want. Can anyone advise me on this? Or know of any other way of analysing the content of a method?

Thanks,

Andy
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Hi Andy and welcome to Javaranch!

I'm actually going to shift this to another forum as there are plenty of third party tools that do this sort of thing for you


Cheers, Martijn - Blog,
Twitter, PCGen, Ikasan, My The Well-Grounded Java Developer book!,
My start-up.
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Now it may seem a little strange that I've posted it to this forum, but the problem you describe is typically solved using various test and code coverage plugins/libraries that you can execute from Ant or Maven (and often also just the command line).

I'd recommend looking for:

* Emma (code coverage)
* Clover (code coverage)
* PMD (code complexity)

Also have a search for java cyclomatic complexity on Google, hope that all helps!

Now that I've listed all of this without mentioning Ant or Maven I realise that I've moved it incorrectly, I'll send it back now.
Appi Sharma
Greenhorn

Joined: Nov 03, 2008
Posts: 1
checkstyle/pmd pluggins are available ,they can be used to check the style,cyclometric complexity
Andy Young
Greenhorn

Joined: Nov 03, 2008
Posts: 9
Hi and thanks for your replies.

After googleing "java cyclomatic complexity" it appears that Cyvis will work nicely for me to look at how complex a method's content is.

As for the code coverage tools...something like Emma is not sufficient for what I am looking to do. Perhaps i should explain for thoroughly...

I want to write a Java program to analyse some given Java source code (from a different program) and work out which methods in that source code are called the most. Emma and Clover just state which parts of a running program were called (and i dont intend to run the source code i want to analyse). I would like to know how many places within the given source code call each of the methods in that source code (like the NetBeans "Find Usages" functionality when right-clicking a method). Any ideas?

Thanks,

Andy
Martijn Verburg
author
Bartender

Joined: Jun 24, 2003
Posts: 3274
    
    5

Try Here for further 3rd party solutions. I use Cobertura as part of a Maven build and it definitely produces a number of times each line is called metric.
[ November 03, 2008: Message edited by: Martijn Verburg ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Analysing the content of a Java method
 
Similar Threads
Uploading a file Error...?!
HFSJ Mock Score
How use OOPs in Servlet
help on Garbage Collection analysis
best source of reference for a particular method in java?