Pure Danger Tech


Printing your Java source stats

17 Nov 2007

Eric Burke posed a challenge for next week to post the longest human-coded method.

I mentioned in his comments that you can use JavaNCSS for this. Just in case that wasn’t quite enough detail, here’s a brief sketch of how to do so. You’ll need to make some tweaks for your environment but I think you’ll get the idea.

Install JavaNCSS

  1. Go to the link above, download JavaNCSS and expand it
  2. export JAVA_HOME=…
  3. export JAVANCSS_HOME=…
  4. export PATH=$PATH:$JAVANCSS_HOME/bin

Install Xalan

This is just to run some XSLT stylesheets on the output XML file.

  1. Download Xalan from Apache and expand it somewhere
  2. export XALAN_HOME=…
  3. export CLASSPATH=$XALAN_HOME/xalan.jar:\

Run JavaNCSS on your source tree

  1. cd …root of all source files…
  2. javancss -all -xml -out $JAVANCSS_HOME/ncss.xml -recursive *

Transform the output

  2. java org.apache.xalan.xslt.Process -in ncss.xml -xsl xslt/wfncss.xsl -out ncss.html

You will now have an ncss.html file generated from your output that you can use to look for huge methods (and other interesting bad stuff).


If you want to be more precise about including many source trees, more work is involved. I wrote an Ant task to make this easier (the referenced one didn’t work for me on modern Ant versions) but I don’t have the code anymore. Ant does such a nice job making it easy to collect source flexibly! I’ll leave this as an exercise for the reader….

There are a bunch of XSLT reports in $JAVANCSS_HOME/xslt. You might find others more interesting. The one I chose just outputs violations of the default warning levels. It doesn’t sort the output as nicely as you’d wish, but should cut it down enough to scan it by eye. Or, you can modify it to sort as desired. Another exercise for the reader…

Hope that helped. You might learn some other interesting stuff here as well if you’ve never looked at your source stats!

PS. 707 was the longest human-coded method I found in our source.