10/10/15

Source Code Analysis with PMD



PMD


is a source code analysis tool. This comes in form of plugin. This plugin can be integrated with your IDE or build tool. PMD integration is available for
1. Maven PMD plugin
2. Eclipse Plugin
3. Netbeans plugin
4. JBuilder pugin
5. JDeveloper plugin
6. IntelliJ IDEA plugin

Language Support :-
It supports source code analysis for these languages
Java, JavaScript, PLSQL, Apache Velocity, XML, XSL.
Additionally it includes CPD, the copy-paste-detector. CPD finds duplicated code in Java, C, C++, C#, PHP, Ruby, Fortran, JavaScript, PLSQL, Apache Velocity, Ruby, Scala, Objective C, Matlab, Python, Go.

How it works :-


PMD have set of rules  defined in xml for languages given above. When you analyse your source code it checks for validation of defined rules. It given errors and warnings based on levels of priority. It has 2 levels of error, 2 levels of warning and information level. This will look like
1. Error (High priority)
2. Error (priority)
3. Warning (High priority)
4. Warning (priority)
5. Information

So you can correct your source code priority wise with PMD defined standards.

Let me show you how to use PMD with Eclipse

PMD Eclipse Plugin:-


Download PMD source code analyser from the link below
http://sourceforge.net/projects/pmd/files/pmd-eclipse/update-site/

Installation

To install the PMD for Eclipse plugin:-

  1. In Eclipse, click on Help -> Install New Software...
  2. Click on Add..
  3. Enter the following:
  4. and click OK.
  5. You should see PMD for Eclipse 4. Select the checkbox next to it and click Next >.
  6. You'll need to accept the license and confirm you want to install a plugin that is not digitally signed. Go ahead and install it anyway.
  7. Restart eclipse.

How to use in Eclipse

  1. Right click on Java Resources -> src  and then choose option under PMD
  2. open PMD perspective in Eclipse.
  3. Under PMD select on analyse source code option. 
  4. it will check your source code and fill your source code files with errors and warnings.
  5. Hovering on error or warning will show you what rule is not being followed.
It will not show you how to fix that so you need to google it. Most of the rules are pretty generic otherwise you will find most of the help on google.
Keep checking your source code while Java programming. It will help make you a better programmer.

Enjoy. Like. Comment.