Modify Your Codes with PMD (1)

zhaozj2021-02-16  69

Use an open source PMD tool to check the Java source code and improve the quality of code. This article tells how to use the PMD JBuilder plugin to embed the PMD tool and code check method in the Java team development tool or development process.

1. Necessity for code quality check

I know someone thinks about this

2. Tools that improve code quality can be used in Java project

I know and try the code inspection tool (all open source) that can be used by the Java project:

Findbugs: http://findbugs.sourceforge.net Static Code Analysis Tool

CheckStyle: http://checkstyle.sourceforge.net Static Code Analysis Tool

PMD: http://pmd.sourceforge.net Static Code Analysis Tool

JUnit: http://www.junit.org unit test tool

The above tools can be used in conjunction with ANT and can be integrated into Eclipse.

Findbugs, PMD, Checkstyle provides the possibility of developing team custom check rules. Through the team's own development, these tools can be checked in accordance with the development team itself.

More Luo twice:

Findbugs and PMDs are to analyze the Java code itself as the main function, and CheckStyle is the main analysis format. The definition of code quality in software engineering is often not just the requirements written for code. The annotations in javadoc or code are also the scope of code quality check, with Findbugs, PMD is not ok, use Checkstyle.

From Java code (non-annotated, non-Javadoc), Fingbugs and PMDs are also different:

The object of FINGBUGS analysis is * .class file, and PMD analysis object * .java source code. Of course, the analysis principle is also different: Findbugs is based on the code engineering library. If you want to write it yourself, you need to master the word code design library, this is another open source project (http://jakarta.apache .org / bcel /), the cattle people look. PMD is based on abstract semantic trees (AST, ABSTRACT SYNTAX TREE), ie: the Java source code is based on its semantic node (Node), and PMD is based on the relationship between nodes on AST. Analyze the code in the code or not in line with the specification.

3. Download installation and use PMD

PMD tools and source code can be downloaded on its project home page, and the plugins for various Java IDE are also available, I have a JBuilder's plugin --PMD-JBUILDER 1.2.zip. After downloading 4 * .jar files in ZIP into the JBuilder ... / lib / ext directory, then restart JBuilder.

After restarting JBuilder, JBuilder's toolbar will appear three toolbar buttons for PMD:

"PMD Check File" - Check the current * .java source code file with PMD;

"PMD Check Project" - Check all * .java files in the current Java project with PMD;

"CPD Check Project" - Check the code of COPY / PASTE in the current Java project with the CPD tool in the PMD

Click "PMD Check File" to try, PMD will check the current * .java source code file in the current * .java source code file according to its own rules (PMD, Rule), each place It gives a line number and brief description. Developers can improve or optimize according to their prompts. I checked my nearest code, it's a lot, and some mistakes did cause serious consequences. Thank PMD! 4. Summary

PMD is a tool, not a method. The development team needs such a tool to support the efforts of code quality. PMD not only built a rule (Rule) that built several code inspections, but also itself (as mentioned in Section 2 above) rules. Next time, I will share the whole process of I will develop PMD code check rules. I hope my friends will advise! Thank you!

Series 2: Modify Your Codes with PMD (2)

转载请注明原文地址:https://www.9cbs.com/read-19285.html

New Post(0)