PMD_Spike.docx - Google Code

moodusroundoΛογισμικό & κατασκευή λογ/κού

15 Αυγ 2012 (πριν από 5 χρόνια και 1 μήνα)

292 εμφανίσεις

PMD_Spike




PMD Spike

by Mevesh

Introduction

PMD is an application
that can scan Java source code
, analyse it

and looks for potential
problems.
PMD comes with a number of ready
-
to
-
run rules that one can run on his/her own source
code to find unused
variables, unnecessary object creation, empty catch blocks, and so forth.
Examples of code errors that can be checked are listed below:



Possible bugs
-

empty try/catch/finally/switch statements



Dead code
-

unused local variables, parameters and private me
thods



Suboptimal code
-

wasteful String/
String Buffer

usage



Overcomplicated expressions
-

unnecessary if statements, for loops that could be
while loops



Duplicate code
-

copied/pasted code means copied/pasted bugs

PMD can be run through command or as a

plug
-
in

in a developing tool. PMD provides a set
of rule that we can change for checking our code as of our requirement. In this spike we are
using PMD as a
plug
-
in

in NetbeanIDE.

Installation
Process

You can download PMD in either a binary release or wi
th all of the source code; both are
available in .zip files on the PMD web site. Assuming you've downloaded the latest PMD
binary release, unzip the archive to any directory. Then it's up to you how to use it
--
if you
simply want to run PMD on a directory o
f Java source files, you can run it from a command
line like this (the command should be all on one line):

C:
\
data
\
pmd
\
pmd>java
-
jar lib
\
pmd
-
1.02.jar c:
\
j2sdk1.4.1_01
\
src
\
java
\
util


text rulesets/unusedcode.xml


c:
\
j2sdk1.4.1_01
\
src
\
java
\
util
\
AbstractMa
p.java 650


Avoid unused local variables such as 'v'


c:
\
j2sdk1.4.1_01
\
src
\
java
\
util
\
Date.java 438


Avoid unused local variables such as 'millis'


// etc, etc, remaining errors skipped

Since our project

is developed
using

NetbeanIDE, we need to download and install the PMD
for NetbeanIDE:

http://sourceforge.net/project/showfiles.php?group_id=56262&package_id=6362
1&release
_id=665883



After downloading we need to add PMD as a plugging to net beans.

1. Start
Net Beans
.

2. Go to Tools
--
> Plugging

3. Switch to 'Downloaded' tab

4. Press 'Add plugging'

5. Go to the directory where you downloaded pmd
-
netbean
s and c
hoose the file "pmd.nbm"

6.
Press OK

7. Press Install

8. Accept the license

9. Confirm installation of an unsigned module


Use PMD

1. Right click on one or more folders or Java files.

2. Choose Tools
-
> PMD and watch the tool, find your flaws.

3. The tool is also located under the global Tools menu.

4. Double click on the error messages in the output pane to go to the line in the

5. Java source file to correct the problem PMD discovered.

Change the rules

1. Start
Net Beans


2. Go to Tools
--
>

Options

3. Navigate to Miscellaneous section and expand PMD node

4. Click on "Rules"

5. Click on the ... button

6. Select the different rules to see information and examples of the rule

7. Use the <, <<, >, >>, buttons to choose which rules to use.

8. Press OK to save the configuration

Custom Rule Sets

1. Start
Net Beans


2. Go to Tools
--
> Options 3. Navigate to Miscellaneous section and expand PMD node

4. Click on "Rule sets"

5. Click "Add Rule Set" to locate the rule set XML file you want to in
clude

6. Click "Add Rule jar" to locate the jar containing the rules

7. Check "Include PMD standard rules" to include the rules from the pmd distribution

8. Press OK to save the configuration

9. Go to Rules to select the new rules.

Real time scanning

1. Start
Net Beans


2. Go to Tools
--
> Options

3. Navigate to Miscellaneous section and expand PMD node

4. Select "Enable scan"