Welcome to part 2 in our series about the extensibility of NetWeaver Developer Studio (NWDS) through plug-ins.
In the Pimp Up My NWDS Part 1 I showed up a way to install and manage additional plug-ins in NWDS without modifying the standard file structure to much. This approach gives you more flexibility when doing an upgrade and also helps you maintaining the same set of plug-ins for two different NWDS installations on the same machine, e.g. for NetWeaver04 (6.40) and NetWeaver2004s (7.0).
In this part I want to show you a plug-in that will check your code using static code analysis to give hints to possible bugs and anti patterns and inefficient code. This way it will help you write better and often faster code.
PMD finds possible problems and shortcomings in your programs by analysing the Java source code. It does this by using a set of rules that you can run on your programs source code to find unused variables, unnecessary object creation, empty catch blocks, and so forth. Rules in PMD are defined in an extensible way that lets you write your own rules specific for your organization or project.
PMD is already covered in a Analysing your Java code with PMD so I wont go into many details but show you how to use it in NWDS. If you want to get deeper knowledge about PMD there is also a book PMD Applied written by one of its lead developers, Tom Copeland.
As most of todays plug-ins PMD is currently developed for Eclipse 3.x but there is one brave hero still maintaining an Eclipse 2.x version named pmd-eclipse-2.3 that is only one minor version behind the official release. You can get it at PMDs official download site. After downloading follow the instructions for installation covered in Pimp Up My NWDS Part 1.
Correctly installed PMD will give you two ways to check your code in NWDS. One way is to trigger the code checks manually against single files, folders or projects. Therefore you have to select a node or element in the Navigation view. Right click the element and the resulting context menu will show an entry PMD with several subentries. Select Check Code With PMD to start it.
The other way is to enable PMD in the project properties. Here you can also define a specific rule set for you project. This way code analysis will executed each time a file is added or saved.
The results of the code check will be displayed in the Tasks view or in the special PMD Violations view, which gives a more tailored display and more specific filtering options.
The PMD Violations view gives a more detailed view on the detected problems. It includes e.g. a severity level for each entry to help you prioritize your pending violations or filter the displayed entries. In case you dont remember the cause of the violation you can also get more detailed information for each of the violated rules by selecting Show Rule from the context menu for an entry.
Either if you use PMD for your own project or if you introduce coding standards into an organisation it is a good practice to tailor the number of rules to the specific needs. In a project this set can be defined by a joint decision and then imported into each single NWDS.
To configure a NWDS global rule set (project specific rule sets can be defined in the project properties) open the Preferences window and go to the PMD->Rules Configuration entry. Here you can see a list of all the available PMD rules. From this list, you can go through the rules, adjust priorities, modify any of the other rule-specific properties, or remove any rules you don’t need. If necessary export your defined set and import it to any other NWDS.
Thats basically all you need to know about PMD in an NWDS environment. Lets hope that the Eclipse 2.x version will be maintained another couple of month (or years?) until we get our hands dirty in a New York (a.k.a. NetWeaver 7.1) project.
Checkstyle and Findbugs
There are two other well knows tools that can help you in your development process in a similar way like PMD.
Checkstyle by Oliver Burn also analyses your code, like PMD, using the Java source. There is an Eclipse plug-in available named CheckClipse. Even though you still find a compatible 2.x version on the download page it wont be maintained anymore and is frankly two years old now. But maybe you like it and give it try anyway or use the stand alone version.
Code analyser FindBugs by Bill Pugh takes a different approach on this topic. It examines your class or JAR files looking for potential problems by matching your bytecodes against a list of bug patterns. Checking is also done using a set of rules but because it works on the bytecode the results of course very different from what you would get with PMD or Checkstyle. Sadly the Eclipse 2.x plug-in situation is the same as with Checkstyle so if you want to try it out in NWDS you have to deal with an old version from 2004 or use the stand alone version.
PMD and similar tools can help you significantly reduce the time and afford involved in code reviews. All code conventions and best practices, which can be described by rules, can automatically be verified in the development process. Seamlessly integrated into your IDE they popup if necessary and get out of the way otherwise. On the other hand any automated code analysis wont replace human code reviews, as there will always be errors that cant be detected by machine analysis.
Thats it for part 2 of Pimp Up My NWDS. The last thing thats missing is to point you to a nice weblog by Maksim Rashchynski The Story About How 2 Eclipse Plugins helped me to make a lot of points on SDN forum were he talks about JadEclipse and JAR Class Finder, two plug-ins which helped him getting points in the forms. Thanks Maksim!