11 May 2015

Java Code Analyzer

Analyzing java source code is an important aspest of good coding. It helps developer to findout the unused variables, dead code, deviation from the coding standard, possible bugs and many more. There are many open source Java code analyzers are available in the market.Few of them are PMD, SONAR, Find Bugs, QJ-Pro, JDiff, CheckStyle etc. PMD and SONAR are very popular in the industry. This tutorial gives brief on PMD setup.

PMD

PMD is a static rule set based Java source code analyzer that identifies the potential problems like:

  • Possible bugs - Empty try/catch/finally/switch blocks
  • Dead code - Unused variables
  • Duplicate code
  • Empty if/while statements

PMD report shows the indicative issues. It is upto the developer to decide whether the issue to be fixed. However, it is advisable that the developpers should fix priority 1 and 2 violations.

PMD plugin setup in Eclipse

As the Eclipse is the most popular IDE so I have done the PMD setup in eclipse. Steps to add PMD plugin in Eclipse is given below

  • Right click on some project and check if PMD is getting displayed on the popup. If displayed then the PMD is already available in your eclipse.


  • Otherwise follow the below steps
  • Start Eclipse and open a project
  • Select "Help"->"Software Updates"->"Find and Install"
  • Click "Next", then click "New remote site"
  • Enter "PMD" into the Name field and "http://pmd.sf.net/eclipse" into the URL field
  • Click through the rest of the dialog boxes to install the plugin
  • Restart eclipse
  • Right click on some project and check if PMD is getting displayed on the popup(as shown in the above image).

PMD Rule setup in Eclipse

As mentioned, PMD is a static rule set based Java source code analyzer. There will be a default set of rules that you can edit/delete. You can add new rules. Also you can import/export a rule set. Go to

  • Window->Preference
  • Expand PMD in the left hand panel
  • Select Rules configuration under PMD
  • All the rules will be visible in the right panel. Now spend some time with the rules to understand how to edit/modify, import/export rules.

Analyze code using PMD

Check Violations
  • Change the perspective to “PMD” in Eclipse. (Normally at the top right corner it will be visible) Righ click on the project->PMD->Check Code with PMD as shown below
  • Now a violation window will open. Check your code violations here.
    See the below screenshot. All important sections are highlighted. In this example, Violation priority 3 is complaining about the length of the variable is excessively long.
Check duplicate Code

Follow the below step to find out the duplicate code that can be refactored into a method. Right click on Project -> PMD -> Find suspect cut and paste

Please note that this tutorial talked about the basic stuffs on PMD. If you want more details on PMD then go to their Official website

Next topic will discuss about the SONAR tool which is more powerful and more widely used in the industry.



blog comments powered by Disqus
J2EE,SOAP,RESTful,SVN,PMD,SONAR,JaCoCo,HTTP,API,MAVEN,AngularJS,GitHub,LDAP,AOP,ORM,JMS,MVC,AWS,SQL,PHP,H2DB,JDBC