Skip to content

findbugs custom rule

Phinehas Zhi edited this page Mar 4, 2019 · 6 revisions

如何测试规则:
download spotbugs-3.1.3, open bin/spotbugs
spotbugs.bat -textui -include <include.xml> <my.jar or class>
test findbugs rule with filter files
http://findbugs.sourceforge.net/manual/running.html
https://spotbugs.readthedocs.io/en/latest/running.html
new class extend :

OpcodeStackDetector 字节码检测类
实现检测逻辑后,进行规则配置

1:在resources下建立findbugs.xml

<?xml version="1.0" encoding="UTF-8"?>  
<FindbugsPlugin xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
                xsi:noNamespaceSchemaLocation="findbugsplugin.xsd"
                pluginid="my.findbugs.rule">

    <Detector class="class path" reports="PATTERN A" speed="fast" />

    <!-- abbrev 需配置 否则会报错 -->
    <BugPattern abbrev="Dm" type="PATTERN A" category="CORRECTNESS" />
</FindbugsPlugin>

Detector 标签配置对应的规则类, reports属性自定义一个名字(规则名), speed速度

BugPattern 标签对应该规则所在的category(大概是规则类型归类), type和之前的规则名reports需一致. abbrev是该名字的缩写,也可以和其他规则共用,理解为一套规则集.

2: 新建resources/messages.xml

<?xml version="1.0" encoding="UTF-8"?>
<MessageCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:noNamespaceSchemaLocation="messagecollection.xsd">

    <Plugin>
        <ShortDescription>xxx FindBugs Plugin</ShortDescription>
        <Details>This plugin provides original detectors</Details>
    </Plugin>

    <Detector class="规则class PATH">
        <Details>
            <![CDATA[
可以不填
]]>
        </Details>
    </Detector>

    <BugPattern type="PATTERN A">  
        <ShortDescription>cannot ... do </ShortDescription>
        <LongDescription>
...
        </LongDescription>
        <Details>
            <![CDATA[
...
]]>
        </Details>
    </BugPattern>
</MessageCollection>

BugPattern 标签的Description都可以不填, 因为会在sonar插件的配置里填写.

3: mvn package 打成my-findbugs-rule.jar包
4: 集成sonar

基于 sonar-findbugs-plugin-3.8.0的jar包来开发.(github上下的源码工程打包报错,故选择jar包直接开发)

6: 把my-findbugs-rule.jar包放入 \sonar-findbugs-plugin-3.8.0\META-INF\lib 下

7: 修改 ...\org\sonar\plugins\findbugs\rules-findbugs.xml,在最后新增一个rule标签

  <!-- my findbugs-->
  <rule key='PATTERN A' priority='MAJOR'>
    <name>RULE_NAME</name>
    <configKey>PATTERN A</configKey>
    <description>
        
        </description>
    <tag>style</tag>
  </rule>
</rules>

rules-findbugs.xml是自带仓库之一. 为了方便直接添加规则到这个仓库. 配置description里的html的'<'和'>'需要转义.<> configKey 要和 rule的key属性一致,与之前填的 BugPattern一致.. priority填写规则的等级.
*8: 修改 ...\com\sonar\sqale\findbugs-model.xml *

 <!-- my-findbugs-->
	  <chc>
        <rule-repo>findbugs</rule-repo>  
        <rule-key>PATTERN A</rule-key> //需要一致
        <prop>
          <key>remediationFunction</key>
          <txt>CONSTANT_ISSUE</txt>
        </prop>
        <prop>
          <key>remediationFactor</key>
          <val>0.0</val>
          <txt>d</txt>
        </prop>
        <prop>
          <key>offset</key>
          <val>10.0</val>
          <txt>mn</txt>
        </prop>
      </chc>

9: 将jar包内容文件夹打包成zip格式,修改后缀为jar

Clone this wiki locally