Check the Code with PMD

Source: Internet
Author: User
Tags xslt
ArticleDirectory
    • E: \ SRC \ confirmratelimitactivity

I didn't know about PMD before. I recently heard about PC lint before I started to get in touch with it. If such a good tool is used, it can help me a lot better.CodeHabits and norms are of great help ~

Let's see how he works.

Many companies use PC lint as a static code review tool for C/C ++. It is said that Microsoft regards PC lint review with no errors or warnings as the first level of code; while PMD is a tool similar to its function for Java. It is open-source and can be downloaded to the entire open-source project. on its official website, it has an introduction like this (PMD ):

Introduction

PMD scans Java source code and looks for potential problems like:

    • Possible bugs-Empty try/catch/finally/switch statements
    • Dead Code-unused local variables, parameters and private methods
    • Suboptimal code-Wasteful string/stringbuffer usage
    • Overcomplicated expressions-unnecessary if statements, for loops that cocould be while Loops
    • Duplicate code-copied/pasted code means copied/pasted bugs

Translation:

PMD scans Java code and looks for the following potential problems:

1. Possible bug-Empty try/catch/finally/switch statement

2. Invalid code-unused variables, parameters, and private methods

3. unoptimal code-resource-consuming string/stringbuffer usage

4. Overly complex expressions-unnecessary if statements, or a while for Loop

5. duplicate code-copying/pasting code means copying/Pasting a bug

Download

Here you can download the latest version of PMD (pmd-4.2.5), which hasSource codeAnd bin zip. Download the bin compressed package, for example:

During the download process, the download speed is too slow, and the download speed is 10 kb/s ~

Use

After downloading the script, decompress it to any directory, modify the script (. BAT) in the directory, and place it in the bin folder of the extracted directory;

@ EchoOffrem file and path to be checked,It can also be a code directory.
Set filepath = E: \ SRC \ composemessageactivity. Java
Rem report output path set reportpath = E: \ REM get current year month day and hour minute second set nowtime = % Date :~ 0, 10%""% Time :~ 1, 7% REM replacement:-set nowtime = % nowtime: =-% REM synthetic report file path name set outfile1_reportpath1_1_nowtime=.html REM specifies the output format set XSLT =".../Etc/XSLT/corley-pmd-report.xslt"Rem specifies the check rules. This includes all the official Java rules. You can also specify other rules set ruleset ="Rulesets/Internal/all-java.xml"Rem executes the check and outputs the report "PMD % filepath % nicehtml % ruleset %-XSLT %-reportfile % OUTFILE % ".

 

Run this script file to obtain the following report file:

Summary
Files Total Qrank Level 1 Qrank Level 2 Qrank level 3 Qrank level 4 Qrank Level 5 Qrank level 6 Qrank level 7 Qrank Level 8
1 18 0 0 18 0 0 0 0 0
E: \ SRC \ confirmratelimitactivity
Prio Begin line Method Description
Total number of violations for this class: 18 (Click anywhere on this row to see/hide details)
3 42 Found non-transient, non-static member. Please mark as transient or provide accessors.
3 43 Found non-transient, non-static member. Please mark as transient or provide accessors.
3 44 Found non-transient, non-static member. Please mark as transient or provide accessors.
3 44 To be compliant to J2EE, A webapp shocould not use any thread.
3 47 Oncreate Avoid excessively long variable names like savedinstancestate
3 47 Oncreate Parameter 'savedinstancestate' is not assigned and cocould be declared final
3 55 Onclick Avoid variables with short names like V
3 55 Onclick Parameter 'V' is not assigned and cocould be declared final
3 62 Onclick Avoid variables with short names like V
3 62 Onclick Parameter 'V' is not assigned and cocould be declared final
3 68 Oncreate To be compliant to J2EE, A webapp shocould not use any thread.
3 71 Run Log. V callshocould be protected by checking config. logv first
3 84 Onresume Local variable 'delay' cocould be declared final
3 99 Onpause Super shoshould be called at the end of the Method
3 105 Onkeydown Parameter 'event' is not assigned and cocould be declared final
3 105 Onkeydown Parameter 'keycode' is not assigned and cocould be declared final
3 113 Doanswer Parameter 'answer' is not assigned and cocould be declared final
3 114 Doanswer Local variable 'intent' cocould be declared final


Generated by PMD4.2.5On-14:49:43.

(1) The report generation interface is very friendly. It is clear at what level each class has.

(2). The suggestions on how to modify the existing problems are also pointed out one by one, for example

Line: 84 method: onresume recommended: local variable'Delay'Cocould be declared final means that it is best to add the final keyword to the variable delay in this function ~

The next step is to follow the prompts to make changes one by one. I believe the modified Code can be appreciated by myself, because most of the Rules checked in are JavaProgramAst provided by members (abstract semantic tree, you don't need to understand what it means, it can be used as a check rule), it is prone to problems from the program, and non-standard design.

It is important to stick to it for a long time. Such a good programming habit should be unconsciously formed. To be honest, PMD is really strict and demanding, therefore, many companies filter rules when using tools such as PMD and PC lint. Otherwise, there are too many errors ~~~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.