22
Hammurapi Java Code Review Tool

Hammurapi Java Code Review Tool

Embed Size (px)

Citation preview

Page 1: Hammurapi Java Code Review Tool

Hammurapi Java Code Review Tool

Page 2: Hammurapi Java Code Review Tool

Company Confidential

2

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

Page 3: Hammurapi Java Code Review Tool

Company Confidential

3

Introduction

Hammurapi is an open source Java code review tool Components of Hammurapi Toolset

Mesapotamia, Hammurapi Rules, Hammurapi, Java Inspectors

Other components with bundleHSQL 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***

Page 4: Hammurapi Java Code Review Tool

Company Confidential

4

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

Page 5: Hammurapi Java Code Review Tool

Company Confidential

5

How does it work- continued…

HSQL DB

(Scan Objects, Violations, Warnings & Metrics are stored here)

Jboss

(Running Hammurapi web application)

Mesapotamia

Hammurapi

Inspector

inspect(XYZ xyz)

SRC

3. Get scan objects from DB

1. Parses the source

5. Each Object is passed to Inspector

6. Post violations, warnings, Metrics

2. Stores parsed information in to DB

(Scan Objects)

4. Iterates over source

units and language

elements

7. Store violations, warnings, Metrics

in to the DB

8. Render review results

Page 6: Hammurapi Java Code Review Tool

Company Confidential

6

Download

Download Hammurapi bundle from :- http://www.hammurapi.biz/dropbox/hammurapi-bundle-5.3.1.zip(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

Page 7: Hammurapi Java Code Review Tool

Company Confidential

7

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/Mesopotamiaserver.silent=true

Page 8: Hammurapi Java Code Review Tool

Company Confidential

8

Initialize database

Start hsql databse:- Open commandline and go to directory c:\hammurapi\db and then type following commandrunServer.bat

Intialize the databse:- Go to c:\hammurapi and type following commandjava -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)

Page 9: Hammurapi Java Code Review Tool

Company Confidential

9

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"/>

Page 10: Hammurapi Java Code Review Tool

Company Confidential

10

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

Page 11: Hammurapi Java Code Review Tool

Company Confidential

11

Review the code

Go to c:\hammurapi Type following command

ant

To see the reports type following in browser:-http://localhost:8080/hammurapi

Page 12: Hammurapi Java Code Review Tool

Company Confidential

12

Where is more information

Quick Start Guide is here:-http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/system/analysis.analysisactions/viewanalysisdocumentro/152/hammurapi+5+quick+start+guide.html

User Guide can be downloaded from here:-http://www.hammurapi.biz/hammurapi-biz/system/fileactions/get/76/hammurapirulesuserguide.pdf

Page 13: Hammurapi Java Code Review Tool

Company Confidential

13

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

Page 14: Hammurapi Java Code Review Tool

Thank You

Page 15: Hammurapi Java Code Review Tool

Advance Topic

Page 16: Hammurapi Java Code Review Tool

Company Confidential

16

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

Page 17: Hammurapi Java Code Review Tool

Company Confidential

17

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

}

}

Page 18: Hammurapi Java Code Review Tool

Company Confidential

18

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

Page 19: Hammurapi Java Code Review Tool

Company Confidential

19

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

customrule.jar Inspectors.xml

Page 20: Hammurapi Java Code Review Tool

Company Confidential

20

How to create environment variable

From start menu go to settings>>control panel

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

Go back

Page 21: Hammurapi Java Code Review Tool

Company Confidential

21

Setting JAVA_HOME

Click on New button for user variables You will get following dialog box to add a

variable

Go back

Page 22: Hammurapi Java Code Review Tool

Company Confidential

22

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.