全面剖析PMD静态代码扫描工具
PMD是使用JavaCC生成解析器来解析源代码并生成AST(抽象语法树)的,这两天对PMD及自定义规则做了调研及实验,部分说明来自官方说明文档,做了大部分参数的详细描述及测试,少数几个参数不明白含义,有了解的朋友欢迎讨论。
1 调研对象
pmd-bin-6.4.0【PMD可执行版本】
· bin
· designer.bat【界面工具,能将java源代码转化为AST(抽象语法树),个人推荐使用】
· bgastviewer.bat【界面工具,与designer.bat功能相似】
· cpd.bat【用来查找重复代码的工具,命令行版】
· cpdgui.bat【用来查找重复代码的工具,GUI版】
· pmd.bat【Window平台下运行PMD需要使用的文件】
· run.sh【Linux平台下运行PMD需要使用的文件】
· lib【该目录存放PMD运行依赖的jar包,包括第三方jar包和各种语言的模块jar包】
2 基本使用
格式:pmd -d [filename|jar or zip file containing source code|directory]-f [report format] -R [ruleset file]
示例:E:\pmd-bin-6.4.0>bin\pmd.bat -d E:\name -f html -R java-basic,java-design
如果要将生成的结果保存出来可以使用命令行重定向保存到指定路径——
E:\pmd-bin-6.4.0>bin\pmd.bat -d E:\name -f html -R java-basic,java-design>report.html
Option
Description
Required
Applies for language
-rulesets / -R
Comma separated list of ruleset names to use
yes
-dir / -d
Root directory for sources
yes
-format / -f
Report format type. Default format is `text`.
no
-auxclasspath
Specifies the classpath for libraries used by the source code. This is used by the type resolution. Alternatively a `file://` URL to a text file containing path elements on consecutive lines can be specified.
no
-uri / -u
Database URI for sources. If this is given, then you don't need to provide `-dir`.
no
plsql
-filelist
Path to file containing a comma delimited list of files to analyze. If this is given, then you don't need to provide `-dir`.
no
-debug / -verbose / -D / -V
Debug mode. Prints more log output.
no
-help / -h / -H
Display help on usage.
no
-encoding / -e
Specifies the character set encoding of the source code files PMD is reading (i.e. UTF-8). Default is `UTF-8`.
no
-threads / -t
Sets the number of threads used by PMD. Default is `1`. Set threads to '0' to disable multi-threading processing.
no
-benchmark / -b
Benchmark mode - output a benchmark report upon completion; defaults to System.err
no
-stress / -S
Performs a stress test.
no
-shortnames
Prints shortened filenames in the report.
no
-showsuppressed
Report should show suppressed rule violations.
no
-suppressmarker
Specifies the string that marks the line which PMD should ignore; default is `NOPMD`.
no
-minimumpriority / -min
Rule priority threshold; rules with lower priority than configured here won't be used. Default is `5` - which is the lowest priority.
no
-property / -P
`{name}={value}`: Define a property for a report format.
no
-reportfile / -r
Send report output to a file; default to System.out
no
-version / -v
Specify version of a language PMD should use.
no
-language / -l
Specify a language PMD should use.
no
-failOnViolation {true|false}
By default PMD exits with status 4 if violations are found. Disable this option with '-failOnViolation false' to exit with 0 instead and just write the report.
no
-cache
Specify a location for the analysis cache file to use. This can greatly improve analysis performance and is highly recommended.
no
-no-cache
Explicitly disable incremental analysis. This switch turns off suggestions to use Incremental Analysis, and causes the -cacheoption to be discarded if it is provided.
2 规则
2.1规则集
RuleCategories
PMD 自带了很多规则集合,并且分类写入不同的 ruleset 文件,如
Basic 包含每人都必须遵守的代码最佳实践,如EmptyCatchBlock
Braces 关于条件分支的规则,如IfStmtsMustUseBraces
Code Size 关于代码大小的规则,如方法的长度,参数的长度,属性的个数等
Clone 克隆实现的规则,如是否有super.clone()
Controversial 一些有争议的规则,如UnnecessaryConstructor不必要的构造器
Coupling 对象连接有关的规则
Design 可以检查有问题的设计,如SwitchStmtsShouldHaveDefault
Finalizers 使用finalizers时需遵循的规则,如FinalizeOnlyCallsSuperFinalize
Import Statements 和import有关的规则,如DuplicateImports重复import
J2EE 唯一规则UseProperClassLoader,class.getClassLoader()可能不正确,用
Thread.currentThread().getContextClassLoader() 代替
Javabeans 和javabean规范有关的规则,有BeanMembersShouldSerialize属性必须
序列化和MissingSerialVersionUID缺少序列化ID
JUnit Tests 和JUnit测试有关的,如JUnitSpelling拼写检查等
Logging (Java) 检查Logger的一些错误用法,如MoreThanOneLogger多个Logger
Logging (Jakarta) 使用Jakarta Logger的一些规则,有UseCorrectExceptionLogging
异常处理不当和ProperLogger是否正确定义Logger
Migrating JDK 版本移植的规则,如ReplaceVectorWithList用List代替Vector
Naming 和命名有关的规则,名称太短或太长,命名的约定等
2.2参数详情
● -dir/-d扫描目录
● -format/-f报告格式,有xml、xslt、html、text,默认为text
● -rulesets/R使用的规则集
● -auxclasspath
● -uri/-u源文件的数据库uri,使用它就不需要提供-dir
● -filelist一个包含逗号分隔的路径列表的文件,使用它就不需要提供-dir
示例:
命令:E:\pmd-bin-6.4.0>bin\pmd.bat -filelist E:\team-goblin\demo1\list.txt -f html -R myRule.xml
结果:
● -debug / -verbose / -D / -V 打印更详细的日志
● –help 显示用法帮助信息
● -encoding / -e 字符集编码,默认为utf-8
● -threads / -t 设置PMD使用的线程数。默认值是“1”。将线程设置为“0”以禁用多线程处理。
● -benchmark / -b 基准模式-完成后输出基准测试报告
● -stress / -S 进行压力测试
● –shortnames 报告中打印缩短的文件名,只有在文件只有一个目录时是奏效
示例:
使用 –shortnames后:
● –suppressmarker 指定一个字符串使得PMD忽略某行,默认为“NOPMD”。
示例1:
示例2:pmd.bat -d E:\team-goblin\demo1 -f html -RmyRule.xml -showsuppressed -suppressmarker allowint
● – showsuppressesd 报告显示被忽略的违规行为
示例:bin\pmd.bat -d E:\team-goblin\demo1 -f html –R myRule.xml-showsuppressesd>report.html
● –minimumpriority/-min
规则最小优先级阈值,默认为5,规则优先级低于它就不会被使用
示例:
pmd.bat-d E:\team-goblin\demo1 -f html -R myRule.xml -min 4
● –property/-P 为报告格式定义一个属性
● –language/-l 指定pmd使用的语言
【对扫描结果没有任何影响】
● - version 指定PMD使用的语言版本
● – failOnViolation {true|false} PMD默认会在发现违规情况时以状态4退出。使用“-failOnViolation false”禁用此选项,以0退出并且只写报告。
0
Everything is fine, no violations found
1
Couldn't understand command line parameters or PMD exited with an exception
4
At least one violation has been detected
● -cache指定要使用的分析缓存文件的位置。这可以大大提高分析性能,并被强烈推荐。
● –no-cache显式禁用增量分析。这个开关关闭了使用增量分析的建议,并导致-cache选项使用时无效
2.3 自定义规则
编写pmd规则有两种方法:
(1)用xpath,参考产生的AST树写xml
(2)用java code,需要深入了解pmd api,用于一些比较复杂的规则
2.3.1xpath自定义规则
使用PMD自带的designer.bat工具可以快速生成一个xpath rule xml。
(1)打开designer界面工具,输入源代码,输入XPath表达式,点击Go按钮,确认右下方的结果输出正确。
(2)点击左上方File->ExportXpath to rule
(3)在新的页面输入Rulename,Rule msg,Rule desc后,点击Create rule XML按钮,查看输出的结果。
示例:
判断非Integer的引用类型//Type/ReferenceType/ClassOrInterfaceType[@Image!="Integer"]
判断使用Int的基本类型
//Type/PrimitiveType[@Image="int"]
3 扩展
3.1 cpd.bat 重复代码检测
cpd.bat --minimum-tokens 100 --filesE:\team-goblin
--minimum-tokens 大于该值则认为代码重复
--files 扫描文件路径
SupportedLanguages
Apex
C#
C/C++
EcmaScript (JavaScript)
Fortran
Go
Groovy
Java
Jsp
Matlab
Objective-C
Perl
PHP
PL/SQL
Python
Ruby
Scala
Swift
Visualforce
Availableformats
text : Default format
xml
csv
csv_with_linecount_per_file
vs
参考资料:
360Test博客:点击打开链接
官方文档:点击打开链接
全面剖析PMD静态代码扫描工具相关推荐
- 静态代码扫描工具(多种)简要入门介绍-SonarQube社区版-免费、TscanCode工具、PMD工具、flake8工具
SonarQube社区版-免费 适用语言:Java, C#, JavaScript, TypeScript, CloudFormation, Terraform, Kotlin, Ruby, Go, ...
- Android和iOS静态代码扫描工具
Android 名称 来源 功能特点 检查SQL注入 检查NullPointException 支持IDE情况 Android Lint Android SDK 针对Android,检查范围广 否 是 ...
- Android 自定义 Lint 实现静态代码扫描工具
文章目录 关于静态代码扫描工具 Lint的简单使用 一.Lint 与 IDE 的结合使用 二.Lint 与 gradle 命令的结合使用 具体位置如下图: 生成的HTML在浏览器打开如图: 自定义 L ...
- java 静态扫描_静态代码扫描工具 – (八)- 扫描Java项目
静态代码扫描工具 – (八)- 扫描Java项目 1.准备好Java项目代码 只要是java语言实现的项目均可. 比如,自动化测试的代码,测试平台等均可以. 本次案例,使用java语言实现的测试平台来 ...
- 静态代码扫描工具汇总
一.概述 在软件开发过程中,开发团队往往要花费大量的时间和精力发现并修改代码缺陷.传统的代码评审.同事复审,通过人工方式来检查缺陷仍然是一件耗时耗力的事情.而静态代码扫描工具能够在代码构建过程中帮助开 ...
- 一个很好用的静态代码扫描工具 360FireLine
一个很好用的静态代码扫描工具 360FireLine 静态代码扫描工具有很多,Android Studio 自带的Lint,FindBugs,前两者生成的测试结果报告都是英文版的,对于英文不好的童鞋们 ...
- 各语言的静态代码扫描工具
1.go 语言静态扫描工具 go tool ver src go语言自带的扫描工具,安装完go后就有 2. php 静态代码扫描 phpmd 安装方法: mac 版本: 1. 前提先安装brew ...
- 静态代码扫描工具—— TScanCode
一.简介 TscanCode支持以下类型规则扫描: 空指针检查,包含可疑的空指针: 数据越界: 内存泄漏,分配和释放不匹配: 逻辑错误,重复的代码分支,bool类型和INT进行比较,表达式永远True ...
- 静态代码扫描工具(SAST)Xcheck
1 Xcheck介绍 Xcheck是一个由腾讯公司CSIG质量部代码安全检查团队自研的静态应用安全测试(SAST,Static application security testing)工具,致力于挖 ...
最新文章
- burn android images with fastboot
- 浅谈单片机程序设计中的“分层思想”!
- 分享10个实用的超绚CSS3按钮设计
- Android简易实战教程--第四十七话《使用OKhttp回调方式获取网络信息》
- ASP.NET Core 自定义认证方式--请求头认证
- 『雕虫小技』用 Zend Guard 加密 html 页面
- 用 或 || 取代常规 if - else 结构
- zzuoj 10409: D.引水工程
- PanguVR获得400万种子轮融资,提供高效的VR家装解决方案
- java jad_Java反编译工具 jad安装及使用的指南
- Solidity入门学习
- Scala应用场景以及环境部署
- C++编程-牛客网-雀魂启动
- 【金猿人物展】龙盈智达首席数据科学家王彦博:量子科技为AI大数据创新发展注入新动能...
- [Graphics]自组织植物建模方法(ContinousLassoBrush)
- html表格线条不显示,cline处理表格不显示线条
- (详细)《美国节日》:某月的第几个星期几
- 3.2 从关系型数据库中查询数据
- 转转二手手机创专业质检服务,引领市场规范发展
- QT技巧 - 如何让子项目的文件有翻译文件
热门文章
- Flowable(1) docker-compose安装flowable-ui
- 绘画初学者动物怎么画?画动物的基本思路以及画法教程
- 如何设置select,input只读不可编辑且值可传递
- 《WDF USB驱动开发指南》-- USB 设备硬件结构
- 共享网站服务器的优缺点,网站服务器的带宽如何选择?独享带宽还是共享带宽?...
- 4QAM、16QAM 调制与解调仿真电路,观察并分析QAM星座图和误码率曲线【matlab代码】
- Centos 通过yum默认安装mysql,my.cnf的常用配置
- win10 映像备份方法
- ajax洗洁精营销案例,趣闻科普,一滴洗洁精,为何能让小纸船飞快往前冲
- DirectX12(D3D12)基础教程(七)——渲染到纹理、正交投影、UI渲染基础