Object Commando languages, development and design

8Apr/090

April Lambda Lounge – Factor and Parrot

I attended the April Lambda Lounge meeting last week and as always, there were interesting language topics to discuss. Kyle Cordes presented Factor and Charles Sharp presented Parrot and the new Perl. I think languages like Factor make the Lambda Lounge fun. Something outside of what you see everyday and very different. I am sceptical about the embracing of the stack at least logically in Factor. Developers in OO and functional styles are taught to avoid side affects. This is for good reason. Experience shows that the more side affects methods have the more difficult they are to understand and support. In Factor, this seems like the exact opposite. Not only does it embrace side affects, but it requires it.

Filed under: Languages No Comments
3Apr/090

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.

JSourceObjectizer

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{
  @Override
  public void performAction(MethodDefinition methodDefinition) {
    //Do stuff with method definition
  }

  @Override
  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();

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

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.