Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Analysing the content of a Java method

 
Andy Young
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Martijn Verburg
author
Bartender
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
checkstyle/pmd pluggins are available ,they can be used to check the style,cyclometric complexity
 
Andy Young
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3275
5
Eclipse IDE Java Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic