Вы находитесь на странице: 1из 22


Java Code Review Tool


 Hammurapi configuration and setup

– Setting up environment variables
– Setting up database
– Running database server and Jboss
 Running code review through tool
 Writing custom rules

Company Confidential

 Hammurapi is an open source Java code review tool

 Components of Hammurapi Toolset
Mesapotamia, Hammurapi Rules, Hammurapi, Java Inspectors
 Other components with bundle
 It comes with LGPL (lesser general public license)
– http://www.gnu.org/licenses/why-not-lgpl.html
– The choice of license makes a big difference: using the Lesser
GPL permits use of the library in proprietary programs; using
the ordinary GPL for a library makes it available only for free
– *** Please check whether this licensing suites you or not***

Company Confidential
How does it work

 Mesopotamia parses source files and stores parsed

information in the database. Parsed files are
represented by Scan object
 Hammurapi retrieves Scan from the database and
iterates over its Source units and Language elements.
 Each object is passed to inspectors' inspect() method
with compatible parameters
 Inspectors inspect language elements and source units
and can post Violations, Warnings or Metrics
 Violations, Warnings and Metrics posted by inspectors
are collected by Hammurapi and stored to the database
 Hammurapi Web application is used to render review

Company Confidential
How does it work- continued…

2. Stores parsed information in to DB

(Scan Objects)
1. Parses the source

4. Iterates over source (Scan Objects, Violations, Warnings & Metrics are stored here)
units and language Hammurapi 3. Get scan objects from DB
8. Render review results

7. Store violations, warnings, Metrics

in to the DB

5. Each Object is passed to Inspector

6. Post violations, warnings, Metrics


inspect(XYZ xyz) (Running Hammurapi web application)

Company Confidential

 Download Hammurapi bundle from :-

(For latest stable release you can go to
http://www.hammurapi.biz and play around )
 Extract downloaded zip file in a directory (for example-
c:\hammurapi )
 Set environment variable MESOPOTAMIA_HOME to
c:\hammurapi (HOW? Click here)
 If you don’t have Ant already installed, also download
Ant from :-http://ant.apache.org/bindownload.cgi
 Add its bin folder path (example c:\ant\bin) to path
environment variable
 Set environment variable JAVA_HOME to JDK folder

Company Confidential
Configure databse

 Hammurapi bundle comes with hsql databse to keep its review results
 Create a folder named db in c:\hammurapi
 Inside c:\hammurapi\db folder create following three files:-
start javaw -Xmx800m -classpath ..\lib\hsqldb.jar
org.hsqldb.util.DatabaseManagerSwing --driver org.hsqldb.jdbcDriver --url
jdbc:hsqldb:hsql://localhost --user mesopotamia

start java -Xmx512m -classpath ..\lib\hsqldb.jar org.hsqldb.Server %1 %2
%3 %4 %5 %6 %7 %8 %9


Company Confidential
Initialize database

 Start hsql databse:- Open commandline and go to directory

c:\hammurapi\db and then type following command
 Intialize the databse:- Go to c:\hammurapi and type following
java -cp

 To load Java Module:- Type following command:-

java -cp
java.jar org.mesopotamia.lang.java.util.InitDatabase

 ( To browse the database structure start runManager.bat in db folder )

 ( Whenever you want to shutdown the server:- Execute SHUTDOWN

command to politely bring the server down)

Company Confidential
Set path of source code to be reviewed

 Go to directory c:\hammurapi and open file

 Change the value of review.src to full path of
you source folder to be reviewed. (by default it
is set to test)
 For example you can set value as
 Now second line of build.xml will looklike :-
<property name="review.src" value="c:/myproject/src"/>

Company Confidential
Start the Jboss

 JBoss also comes bundled with hammurapi

 Make sure you are not running any other service
on port 8080 because Jboss will use it. (You can
change it if you want and know how to do it )
 Go to c:\hammurapi\jboss\bin
 Type following command to start the Jboss


Company Confidential
Review the code

 Go to c:\hammurapi
 Type following command

 To see the reports type following in browser:-



Company Confidential
Where is more information

 Quick Start Guide is here:-

 User Guide can be downloaded from here:-


Company Confidential
Some theory

 This tool uses rule engine to review the code

 It comes bundled with commonly used rules
 New rules can be implemented and configured
 Adding your new rules requires Java coding only
 You can validate whether code is adhering to design
guidelines or framework standards
 Only tool that can be used through any Java IDE and
also without IDE
 Every time you run review a new version of review
result is published so you can compare from previous
review how many violations are closed


Company Confidential
Thank You
Advance Topic
Write your own rules

 Don’t write stringObj.equals(“abc”)

 Use “abc”.equals(stringObj)
 All the method calls are sent to respective
 We will write an Inspector who will be notified
on method calls. This inspector will check
whether method called is equals()
 Then it will check whether its argument is
StringContstant i.e “abc”
 If yes then post a volilation. That’s all

Company Confidential
Your own custom Inspector class

package com.birlasoft.customrules.blrules;

import biz.hammurapi.review.Inspector;
import org.mesopotamia.SourceUnit;
import org.mesopotamia.lang.java.MethodCall;
import org.mesopotamia.lang.java.StringConstant;
import biz.hammurapi.review.Violation;

public class StringComparisionInspector extends Inspector {

public void inspect(MethodCall methodCall) {
String methodName=methodCall.getName();
if (methodName.equals("equals")
&& methodCall.getArguments().size()==1
&& methodCall.getArguments().get(0) instanceof StringConstant) {

post(new Violation(methodCall));


Company Confidential
Inspector configuration File

<ruleset type="biz.hammurapi.config.ElementNameDomConfigurableContainer">

<name>Birlasoft custom inspectors</name>

<description>Hammurapi inspectors for Birlasoft Framework</description>

<handle-manager type="biz.hammurapi.rules.KnowledgeMaximizingHandleManager"/>

<collection-manager type="biz.hammurapi.rules.PojoCollectionManager">

<rules type="biz.hammurapi.review.ReviewRulesContainer">
<rule type="com.birlasoft.customrules.blrules.StringComparisionInspector">
<description>Inspector under development</description>

bundled Inspector


Company Confidential
Use your custom rule

 Put custom rule class after compilation in class path or

inside c:\hammurapi\lib as a jar file
 Put your inspector.xml in the directory where build.xml
is there (c:\hammurapi)
 Run the review as mentioned previously

19 customrule.jar Inspectors.xml
Company Confidential
How to create environment variable

 From start menu go to settings>>control panel

 Click “system”>>Click “Advance” tab>>click
“Environment Variables” button

Go back


Company Confidential

 Click on New button for user variables

 You will get following dialog box to add a

Go back


Company Confidential
Getting report offline

 Download wget for windows (

http://users.ugent.be/~bpuype/wget/#download) You need
 Put wget.exe in a folder and navigate to that folder
from a command line (go to c:\wget for example)
 Now type following command:-
wget -mrkE http://localhost:8080/hammurapi/report.jsp?ID=XX

Here XX is your report ID in hammurapi report for

example 15
 You will have all the files in a folder inside wget folder
you created.


Company Confidential