Code generation made easy using patterns

Since starting developing some code generation plugins (toString() generator and Java Util Logger Generator) using/for the NetBeans 6.1Beta I have learnt a lot and the more I worked on it I kept asking myself how could I make it easier. Once both the plugins took rudimentary form I learnt and discovered that patterns could make this task whole lot easier for me. So I started re-implementing the Logger generator (if you are interested in getting the resources please check the logger blog) plugin.

Now I wanted to implement that the plugin will search for System.out.print(ln) and out.print(ln) if static import exists. In doing so I had to walk the PARSED tree of a Java Source file. Just to give me a flavour that I am implementing something cool I called it JavaSourceTreeParser. Basically what it does is breaks down each every Java Statement to a form which can not be further decomposed. In the initial version the conversion was done in the parser it self (Revision 21). Then it felt that I could easily use Observer pattern for it and after implementing it (Revision 31) I saw that I am right and I achieved something which every code generator can use. (As I use GIT SCM I usually work offline and make bunch of commits together so dont worry how I make multiple commits at a go :)).

Once implementing it I found that for performance improvement and communication between various listeners I felt the need of session for state information sharing and then I decided to use Composite pattern for the purpose. and it also worked like a charm and as a result improving the overall performance of the plugins. The following diagram might give an idea how to use it using the API I designed.



What I implemented in the listener simply that the listener will get notified whenever the parser come across a particular type of tree, for System.out.println it would be MethodInvocationTree (Kind.METHOD_INVOCATION), and the listener according to its implementation will handle the Tree; in my case I replaced the method invocation with a logger invocation. In case where I inserted a Log method at the beginning of every method body I listened to Kind.METHOD. Now I am implementing Log insertion for Throw, Return and End of Method block. In process I will also add parsing life cycle listener to the API so that modification to the class is done only once when the parsing is completed. I hope this API helps users interested in code generation. I am very interested to learn what users think about it.

NetBeans: Generating Java Util Logger

Java Util Logger has enabled us to ship application without having dependency on any external JARs or APIs for logging purpose. After starting to use it I felt the necessity of a Logger creation tool which will create and initialize a logger for me instead of me either writing it or Copy-Pasting it for every file or worse every class. Also using a tool will enable me to maintain a coherence of naming and initialization of loggers across a project.

With the release of Java Source API with NetBeans 6.1Beta I finally got the chance to write such a tool for myself :). In sequence with my toString() generator module I started this module as well. You can download the NBM file and install it as it is free and open source. How-to of this tool is as follows.

Basically what I needed for it can be stated in 3 steps -
  1. Parse/read the Java File
  2. Find declared classes
  3. Check if Logger exists if not create it and add utility methods.
With the new Java Source API and Java Compiler Tree API it is nothing more than simple tree traversing for the purpose. You can have a peek at the implementation in the addLogger method in the source file (after opening the file 'Find' might be useful). One will find these NetBeans Wiki pages being helpful.

NetBeans - Generating toString for Java classes

My friend and colleague Shams developed a maven plugin to generate toString and equals method using Eclipse source code manipulation API and since then, being a NetBeans fan, I wanted to develop a similar component using NetBeans API. Though the Java Source API of NetBeans was available in the developer version, but it is finally going to be released with NetBeans 6.1 and one can have a look at it in 6.1Beta release.

So I started doing what I have been wanting to do for a long time :) - develop a NetBeans module to generate toString() for my Java classes. In this endeavour I came up with this open source project. In the generation of toString() I added iteration over array and collection over what Shams did. Users can simply download the NBM file from here and install it into your NetBeans 6.1 installation. To confirm that the installation is successful check the "Source" menu and you should see "Generate toString()" at the top of the list. To see the module in action simply open a Java Source file and choose the menu action and you will see that it will add/replace toString() methods of the classes in the file.

As future works to this plugin I have plans to add it to the context menu in project explorer for Java projects, provide a UI for user to choose what properties should be in toString and whether to replace toString or not. Additionally generate the current toString content in a separate method and invoke it from toString().

I also have plans for further plugins ; lets hope I have enough time for it :). Please create issues in the project to report issues or enhancements. Participation in development is most welcomed.

Search