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

Hammurapi

Java Code Review Tool


Agenda

 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
Introduction

 Hammurapi is an open source Java code review tool


 Components of Hammurapi Toolset
Mesapotamia, Hammurapi Rules, Hammurapi, Java Inspectors
 Other components with bundle
HSQL DB, JBoss
 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
programs.
– *** 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
results

Company Confidential
How does it work- continued…
HSQL DB

2. Stores parsed information in to DB

(Scan Objects)
1. Parses the source
SRC
Mesapotamia

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

7. Store violations, warnings, Metrics


Jboss
in to the DB

5. Each Object is passed to Inspector

6. Post violations, warnings, Metrics

Inspector

inspect(XYZ xyz) (Running Hammurapi web application)

Company Confidential
Download

 Download Hammurapi bundle from :-


http://www.hammurapi.biz/dropbox/hammurapi-bundle-5.3
(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:-
##-------runManager.bat---------##
start javaw -Xmx800m -classpath ..\lib\hsqldb.jar
org.hsqldb.util.DatabaseManagerSwing --driver org.hsqldb.jdbcDriver --url
jdbc:hsqldb:hsql://localhost --user mesopotamia

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

##-------server.properties----##
server.database.0=file:data/Mesopotamia
server.silent=true

Company Confidential
Initialize database

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


c:\hammurapi\db and then type following command
runServer.bat
 Intialize the databse:- Go to c:\hammurapi and type following
command
java -cp
lib\hgcommons.jar;lib\hsqldb.jar;lib\mesopotamia.jar;lib\hgee.jar
org.mesopotamia.util.InitDatabase

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


java -cp
lib\hgcommons.jar;lib\hsqldb.jar;lib\mesopotamia.jar;lib\mesopotamia-
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


build.xml
 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
c:\myproject\src.
 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


bundle
 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
run

10

Company Confidential
Review the code

 Go to c:\hammurapi
 Type following command
ant

 To see the reports type following in browser:-


http://localhost:8080/hammurapi

11

Company Confidential
Where is more information

 Quick Start Guide is here:-

http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/system/analysis
 User Guide can be downloaded from here:-
http://www.hammurapi.biz/hammurapi-biz/system/fileactions/get/76

12

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
Advance

13

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
inspectors.
 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
16

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));
}

17

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">
<collectionType>biz.hammurapi.rules.KnowledgeMaximizingSet</collectionType>
</collection-manager>

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

bundled Inspector

18

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

20

Company Confidential
Setting JAVA_HOME

 Click on New button for user variables


 You will get following dialog box to add a
variable

Go back

21

Company Confidential
Getting report offline

 Download wget for windows (


http://users.ugent.be/~bpuype/wget/#download) You need
wget.exe
 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.

22

Company Confidential