To improve the quality of your code, in addition to improving logical control and understanding of business processes, there is room for improvement in the code itself, such as some potential problems that can be avoided early. Similar to the content of the coding code, if all rely on the coder to do their own checks, it will undoubtedly require a lot of work, if you can use the static inspection tools to check the code, then greatly improve the efficiency of coding.
This article is the first of a series of articles on improving code quality, which focuses on how to automate the code with the PMD tool to circumvent some potential problems and find logic errors in the code.
1. What is PMD?
PMD is a code-checking tool that examines Java source code to identify potential problems as follows:
® Potential bugs: Empty try/catch/finally/switch statements
® Unused code: Unused local variables, parameters, private methods, and so on
® Optional code: Misuse of String/stringbuffer
® Complex expression: An If statement that is not required, a for loop that can be completed using a while loop
® Duplicate code: Copy/Paste code means copy/paste bugs
Can be at the following address Download PMD, and can be in Rule Set IndexPage to see a preview of all the rules.
At the same time, PMD has been associated with JDeveloper, Eclipse, JEdit, JBuilder, BlueJ, Codeguide, NetBeans, Sun Java Studio enterprise/creator, IntelliJ Idea, TextPad, Maven, Ant, Gel, JCreator, and Emacs are integrated together.
2. Installing the PMD plug-in in eclipse
PMD can be integrated into many popular Ides as Plug-ins, and many plug-ins contain the PMD jar file, which contains the rule set. So while some plug-ins use Rulesets/unusedcode.xml as a parameter to refer to the rule set, they actually use the getResourceAsStream () method to load from the PMD jar file.
Because Eclipse is the more popular open source JAVA/J2EE development IDE, this article focuses on how to use the PMD tool for code checking in eclipse.
The process of installing Eclipse's PMD plug-in is as follows:
® Start eclipse
® Select Help-->software Updates-->find and Install
® Select next to select New remote site
®&nb