Object Commando languages, development and design


JSourceObjectizer – Custom Static Code Analysis

I stumbled onto a pretty nice Java library for custom static code analysis last week called JSourceObjectizer. I call this custom static code analysis because it really isn't like PMD or FindBugs or other tools that have built in rules for what to look for. Actually, I'm not even using the library for static code analysis (more on what I'm using it for later). Basically, the library is a facade over the Abstract Syntax Tree produced by a Java parser. The parser is written using ANTLR, as well as the tree walker and can be downloaded separately here. I started with the parser, not realizing that the JSourceObjectizer was there and then quickly switched when I found it.


The simplest API is a fairly large, single interface based, event driven API. How I used it was the TraverseActionAdapter which has a default implementation of all of the methods in TraverseAction interface. The method calls look like:

public class Traverser extends TraverseActionAdapter{
  public void performAction(MethodDefinition methodDefinition) {
    //Do stuff with method definition

  public void actionPerformed(MethodDefinition methodDefinition) {
    //Do stuff with method definition

Basically each language construct has two methods, performAction and actionPerformed. PerformAction is called at the beginning, in this case when the method is declared and action performed is called at the end of the declaration. This same pattern is repeated for each construct, so at the beginning and end of an if statement, try/catch statement for/while loop etc. Once the traverse actions are defined, it just needs to be hooked into to the library with code like:

public void parseFile(){
JavaSource javaSource = null;
  TraverserExample traverser = new TraverserExample();

    javaSource = new JSourceUnmarshaller().unmarshal(sourceFile, null);
  }catch (JSourceUnmarshallerException ex) {
    //Properly handle error here of course...

The events will be sent to the TraverseAction implementation and action can be taken based on what the code looks like. It also gives line numbers with the callbacks to make good error messages easily. It's important to create a new instance of the TraverseAction implementation for each file parsed. I tried to reuse instances and received some pretty odd results. Creating a new instance each time solved the problem.

Putting JSourceObjectizer to Good Use

My main purpose for using this software is to parse and classify statements in a Java program for the purposes of data mining. In my case I was using a fairly large code base (JBoss) and condensing methods into smaller patterns for sequential pattern mining. This library saved me quite a bit of time. I thought I would probably find an open source parser to use and then I would have to walk the AST to find out what I wanted. This library did almost everything I needed. One thing that it didn't do, was to have a callback around the finally block, that would be nice. It also didn't quite parse all of the files. I think I ran into 5 or 10 that it ran into difficulties with. I still need to debug where the problem is, but it looks to be pretty deep in JSourceObjectizer.

Comments (0) Trackbacks (0)

No comments yet.

Leave a comment


No trackbacks yet.