Home | History | Annotate | Download | only in manual
      1 <html><head>
      2       <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
      3    <title>Chapter&nbsp;8.&nbsp;Filter Files</title><meta name="generator" content="DocBook XSL Stylesheets V1.76.1"><link rel="home" href="index.html" title="FindBugs&#8482; Manual"><link rel="up" href="index.html" title="FindBugs&#8482; Manual"><link rel="prev" href="eclipse.html" title="Chapter&nbsp;7.&nbsp;Using the FindBugs&#8482; Eclipse plugin"><link rel="next" href="analysisprops.html" title="Chapter&nbsp;9.&nbsp;Analysis Properties"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Chapter&nbsp;8.&nbsp;Filter Files</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="eclipse.html">Prev</a>&nbsp;</td><th width="60%" align="center">&nbsp;</th><td width="20%" align="right">&nbsp;<a accesskey="n" href="analysisprops.html">Next</a></td></tr></table><hr></div><div class="chapter" title="Chapter&nbsp;8.&nbsp;Filter Files"><div class="titlepage"><div><div><h2 class="title"><a name="filter"></a>Chapter&nbsp;8.&nbsp;Filter Files</h2></div></div></div><div class="toc"><p><b>Table of Contents</b></p><dl><dt><span class="sect1"><a href="filter.html#d0e1838">1. Introduction to Filter Files</a></span></dt><dt><span class="sect1"><a href="filter.html#d0e1888">2. Types of Match clauses</a></span></dt><dt><span class="sect1"><a href="filter.html#d0e2136">3. Java element name matching</a></span></dt><dt><span class="sect1"><a href="filter.html#d0e2161">4. Caveats</a></span></dt><dt><span class="sect1"><a href="filter.html#d0e2191">5. Examples</a></span></dt><dt><span class="sect1"><a href="filter.html#d0e2249">6. Complete Example</a></span></dt></dl></div><p>
      4 Filter files may be used to include or exclude bug reports for particular classes
      5 and methods.  This chapter explains how to use filter files.
      6 
      7 </p><div class="note" title="Planned Features" style="margin-left: 0.5in; margin-right: 0.5in;"><table border="0" summary="Note: Planned Features"><tr><td rowspan="2" align="center" valign="top" width="25"><img alt="[Note]" src="note.png"></td><th align="left">Planned Features</th></tr><tr><td align="left" valign="top"><p>
      8   Filters are currently only supported by the Command Line interface.
      9   Eventually, filter support will be added to the GUI.
     10 </p></td></tr></table></div><p>
     11 </p><div class="sect1" title="1.&nbsp;Introduction to Filter Files"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="d0e1838"></a>1.&nbsp;Introduction to Filter Files</h2></div></div></div><p>
     12 Conceptually, a filter matches bug instances against a set of criteria.
     13 By defining a filter, you can select bug instances for special treatment;
     14 for example, to exclude or include them in a report.
     15 </p><p>
     16 A filter file is an <a class="ulink" href="http://www.w3.org/XML/" target="_top">XML</a> document with a top-level <code class="literal">FindBugsFilter</code> element
     17 which has some number of <code class="literal">Match</code> elements as children.  Each <code class="literal">Match</code>
     18 element represents a predicate which is applied to generated bug instances.
     19 Usually, a filter will be used to exclude bug instances.  For example:
     20 
     21 </p><pre class="screen">
     22 <code class="prompt">$ </code><span class="command"><strong>findbugs -textui -exclude <em class="replaceable"><code>myExcludeFilter.xml</code></em> <em class="replaceable"><code>myApp.jar</code></em></strong></span>
     23 </pre><p>
     24 
     25 However, a filter could also be used to select bug instances to specifically
     26 report:
     27 
     28 </p><pre class="screen">
     29 <code class="prompt">$ </code><span class="command"><strong>findbugs -textui -include <em class="replaceable"><code>myIncludeFilter.xml</code></em> <em class="replaceable"><code>myApp.jar</code></em></strong></span>
     30 </pre><p>
     31 </p><p>
     32 <code class="literal">Match</code> elements contain children, which are conjuncts of the predicate.
     33 In other words, each of the children must be true for the predicate to be true.
     34 </p></div><div class="sect1" title="2.&nbsp;Types of Match clauses"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="d0e1888"></a>2.&nbsp;Types of Match clauses</h2></div></div></div><div class="variablelist"><dl><dt><span class="term"><code class="literal">&lt;Bug&gt;</code></span></dt><dd><p>
     35             This element specifies a particular bug pattern or patterns to match.
     36             The <code class="literal">pattern</code> attribute is a comma-separated list of
     37             bug pattern types.  You can find the bug pattern types for particular
     38             warnings by looking at the output produced by the <span class="command"><strong>-xml</strong></span>
     39             output option (the <code class="literal">type</code> attribute of <code class="literal">BugInstance</code>
     40             elements), or from the <a class="ulink" href="../bugDescriptions.html" target="_top">bug
     41             descriptions document</a>.
     42    </p><p>
     43                For more coarse-grained matching, use <code class="literal">code</code> attribute. It takes
     44                a comma-separated list of bug abbreviations. For most-coarse grained matching use
     45                <code class="literal">category</code> attriute, that takes a comma separated list of bug category names:
     46                <code class="literal">CORRECTNESS</code>, <code class="literal">MT_CORRECTNESS</code>,
     47                <code class="literal">BAD_PRACTICICE</code>, <code class="literal">PERFORMANCE</code>, <code class="literal">STYLE</code>.
     48    </p><p>
     49                If more than one of the attributes mentioned above are specified on the same
     50                <code class="literal">&lt;Bug&gt;</code> element, all bug patterns that match either one of specified
     51                pattern names, or abreviations, or categories will be matched.
     52    </p><p>
     53                As a backwards compatibility measure, <code class="literal">&lt;BugPattern&gt;</code> and
     54                <code class="literal">&lt;BugCode&gt;</code> elements may be used instead of
     55                <code class="literal">&lt;Bug&gt;</code> element. Each of these uses a
     56                <code class="literal">name</code> attribute for specifying accepted values list. Support for these
     57                elements may be removed in a future release.
     58    </p></dd><dt><span class="term"><code class="literal">&lt;Confidence&gt;</code></span></dt><dd><p>
     59             This element matches warnings with a particular bug confidence.
     60             The <code class="literal">value</code> attribute should be an integer value:
     61             1 to match high-confidence warnings, 2 to match normal-confidence warnings,
     62             or 3 to match low-confidence warnings. &lt;Confidence&gt; replaced
     63             &lt;Priority&gt; in 2.0.0 release.
     64         </p></dd><dt><span class="term"><code class="literal">&lt;Priority&gt;</code></span></dt><dd><p>
     65             Same as <code class="literal">&lt;Confidence&gt;</code>, exists for backward compatibility.
     66         </p></dd><dt><span class="term"><code class="literal">&lt;Rank&gt;</code></span></dt><dd><p>
     67             This element matches warnings with a particular bug rank.
     68             The <code class="literal">value</code> attribute should be an integer value
     69             between 1 and 20, where 1 to 4 are scariest, 5 to 9 scary, 10 to 14 troubling,
     70             and 15 to 20 of concern bugs.
     71         </p></dd><dt><span class="term"><code class="literal">&lt;Package&gt;</code></span></dt><dd><p>
     72             This element matches warnings associated with classes within the package specified
     73             using <code class="literal">name</code> attribute. Nested packages are not included (along the
     74             lines of Java import statement). However matching multiple packages can be achieved
     75             easily using regex name match.
     76         </p></dd><dt><span class="term"><code class="literal">&lt;Class&gt;</code></span></dt><dd><p>
     77             This element matches warnings associated with a particular class. The
     78             <code class="literal">name</code> attribute is used to specify the exact or regex match pattern
     79             for the class name.
     80         </p><p>
     81             As a backward compatibility measure, instead of element of this type, you can use
     82              <code class="literal">class</code> attribute on a <code class="literal">Match</code> element to specify
     83              exact an class name or <code class="literal">classregex</code> attribute to specify a regular
     84              expression to match the class name against.
     85         </p><p>
     86             If the <code class="literal">Match</code> element contains neither a <code class="literal">Class</code> element,
     87             nor a <code class="literal">class</code> / <code class="literal">classregex</code> attribute, the predicate will apply
     88             to all classes. Such predicate is likely to match more bug instances than you want, unless it is
     89             refined further down with apropriate method or field predicates.
     90         </p></dd><dt><span class="term"><code class="literal">&lt;Method&gt;</code></span></dt><dd><p>This element specifies a method.  The <code class="literal">name</code> is used to specify
     91    the exact or regex match pattern for the method name.
     92    The <code class="literal">params</code> attribute is a comma-separated list
     93    of the types of the method's parameters.  The <code class="literal">returns</code> attribute is
     94    the method's return type.  In <code class="literal">params</code> and <code class="literal">returns</code>, class names
     95    must be fully qualified. (E.g., "java.lang.String" instead of just
     96    "String".) If one of the latter attributes is specified the other is required for creating a method signature.
     97    Note that you can provide either <code class="literal">name</code> attribute or <code class="literal">params</code>
     98    and <code class="literal">returns</code> attributes or all three of them. This way you can provide various kinds of
     99    name and signature based matches.
    100    </p></dd><dt><span class="term"><code class="literal">&lt;Field&gt;</code></span></dt><dd><p>This element specifies a field. The <code class="literal">name</code> attribute is is used to specify
    101    the exact or regex match pattern for the field name. You can also filter fields according to their signature -
    102    use <code class="literal">type</code> attribute to specify fully qualified type of the field. You can specify eiter or both
    103    of these attributes in order to perform name / signature based matches.
    104    </p></dd><dt><span class="term"><code class="literal">&lt;Local&gt;</code></span></dt><dd><p>This element specifies a local variable. The <code class="literal">name</code> attribute is is used to specify
    105    the exact or regex match pattern for the local variable name. Local variables are variables defined within a method.
    106    </p></dd><dt><span class="term"><code class="literal">&lt;Or&gt;</code></span></dt><dd><p>
    107    This element combines <code class="literal">Match</code> clauses as disjuncts.  I.e., you can put two
    108    <code class="literal">Method</code> elements in an <code class="literal">Or</code> clause in order to match either method.
    109    </p></dd><dt><span class="term"><code class="literal">&lt;And&gt;</code></span></dt><dd><p>
    110    This element combines <code class="literal">Match</code> clauses which both must evaluate to true.  I.e., you can put
    111    <code class="literal">Bug</code> and <code class="literal">Priority</code> elements in an <code class="literal">And</code> clause in order
    112    to match specific bugs with given priority only.
    113    </p></dd><dt><span class="term"><code class="literal">&lt;Not&gt;</code></span></dt><dd><p>
    114    This element inverts the included child <code class="literal">Match</code>. I.e., you can put a
    115    <code class="literal">Bug</code> element in a <code class="literal">Not</code> clause in order to match any bug
    116    excluding the given one.
    117    </p></dd></dl></div></div><div class="sect1" title="3.&nbsp;Java element name matching"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="d0e2136"></a>3.&nbsp;Java element name matching</h2></div></div></div><p>
    118 If the <code class="literal">name</code> attribute of <code class="literal">Class</code>, <code class="literal">Method</code> or
    119 <code class="literal">Field</code> starts with the ~ character the rest of attribute content is interpreted as
    120 a Java regular expression that is matched against the names of the Java element in question.
    121 </p><p>
    122 Note that the pattern is matched against whole element name and therefore .* clauses need to be used
    123 at pattern beginning and/or end to perform substring matching.
    124 </p><p>
    125 See <a class="ulink" href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/regex/Pattern.html" target="_top"><code class="literal">java.util.regex.Pattern</code></a>
    126 documentation for pattern syntax.
    127 </p></div><div class="sect1" title="4.&nbsp;Caveats"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="d0e2161"></a>4.&nbsp;Caveats</h2></div></div></div><p>
    128 <code class="literal">Match</code> clauses can only match information that is actually contained in the
    129 bug instances.  Every bug instance has a class, so in general, excluding
    130 bugs by class will work.
    131 </p><p>
    132 Some bug instances have two (or more) classes.  For example, the DE (dropped exception)
    133 bugs report both the class containing the method where the dropped exception
    134 happens, and the class which represents the type of the dropped exception.
    135 Only the <span class="emphasis"><em>first</em></span> (primary) class is matched against <code class="literal">Match</code> clauses.
    136 So, for example, if you want to suppress IC (initialization circularity)
    137 reports for classes "com.foobar.A" and "com.foobar.B", you would use
    138 two <code class="literal">Match</code> clauses:
    139 
    140 </p><pre class="programlisting">
    141    &lt;Match&gt;
    142       &lt;Class name="com.foobar.A" /&gt;
    143       &lt;Bug code="IC" /&gt;
    144    &lt;/Match&gt;
    145 
    146    &lt;Match&gt;
    147       &lt;Class name="com.foobar.B" /&gt;
    148       &lt;Bug code="IC" /&gt;
    149    &lt;/Match&gt;
    150 </pre><p>
    151 
    152 By explicitly matching both classes, you ensure that the IC bug instance will be
    153 matched regardless of which class involved in the circularity happens to be
    154 listed first in the bug instance.  (Of course, this approach might accidentally
    155 supress circularities involving "com.foobar.A" or "com.foobar.B" and a third
    156 class.)
    157 </p><p>
    158 Many kinds of bugs report what method they occur in.  For those bug instances,
    159 you can put <code class="literal">Method</code> clauses in the <code class="literal">Match</code> element and they should work
    160 as expected.
    161 </p></div><div class="sect1" title="5.&nbsp;Examples"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="d0e2191"></a>5.&nbsp;Examples</h2></div></div></div><p>
    162   1. Match all bug reports for a class.
    163 
    164 </p><pre class="programlisting">
    165 
    166      &lt;Match&gt;
    167        &lt;Class name="com.foobar.MyClass" /&gt;
    168      &lt;/Match&gt;
    169 
    170 </pre><p>
    171 
    172 </p><p>
    173   2. Match certain tests from a class by specifying their abbreviations.
    174 </p><pre class="programlisting">
    175 
    176      &lt;Match&gt;
    177        &lt;Class name="com.foobar.MyClass"/ &gt;
    178        &lt;Bug code="DE,UrF,SIC" /&gt;
    179      &lt;/Match&gt;
    180 
    181 </pre><p>
    182 </p><p>
    183   3. Match certain tests from all classes by specifying their abbreviations.
    184 
    185 </p><pre class="programlisting">
    186 
    187      &lt;Match&gt;
    188        &lt;Bug code="DE,UrF,SIC" /&gt;
    189      &lt;/Match&gt;
    190 
    191 </pre><p>
    192 </p><p>
    193   4. Match certain tests from all classes by specifying their category.
    194 
    195 </p><pre class="programlisting">
    196 
    197      &lt;Match&gt;
    198        &lt;Bug category="PERFORMANCE" /&gt;
    199      &lt;/Match&gt;
    200 
    201 </pre><p>
    202 </p><p>
    203   5. Match bug types from specified methods of a class by their abbreviations.
    204 
    205 </p><pre class="programlisting">
    206 
    207      &lt;Match&gt;
    208        &lt;Class name="com.foobar.MyClass" /&gt;
    209        &lt;Or&gt;
    210          &lt;Method name="frob" params="int,java.lang.String" returns="void" /&gt;
    211          &lt;Method name="blat" params="" returns="boolean" /&gt;
    212        &lt;/Or&gt;
    213        &lt;Bug code="DC" /&gt;
    214      &lt;/Match&gt;
    215 
    216 </pre><p>
    217 </p><p>
    218     6. Match a particular bug pattern in a particular method.
    219 
    220 </p><pre class="programlisting">
    221 
    222     &lt;!-- A method with an open stream false positive. --&gt;
    223     &lt;Match&gt;
    224       &lt;Class name="com.foobar.MyClass" /&gt;
    225       &lt;Method name="writeDataToFile" /&gt;
    226       &lt;Bug pattern="OS_OPEN_STREAM" /&gt;
    227     &lt;/Match&gt;
    228 
    229 </pre><p>
    230 </p><p>
    231     7. Match a particular bug pattern with a given priority in a particular method.
    232 
    233 </p><pre class="programlisting">
    234 
    235     &lt;!-- A method with a dead local store false positive (medium priority). --&gt;
    236     &lt;Match&gt;
    237       &lt;Class name="com.foobar.MyClass" /&gt;
    238       &lt;Method name="someMethod" /&gt;
    239       &lt;Bug pattern="DLS_DEAD_LOCAL_STORE" /&gt;
    240       &lt;Priority value="2" /&gt;
    241     &lt;/Match&gt;
    242 
    243 </pre><p>
    244 </p><p>
    245     8. Match minor bugs introduced by AspectJ compiler (you are probably not interested in these unless
    246     you are an AspectJ developer).
    247 
    248 </p><pre class="programlisting">
    249 
    250     &lt;Match&gt;
    251       &lt;Class name="~.*\$AjcClosure\d+" /&gt;
    252       &lt;Bug pattern="DLS_DEAD_LOCAL_STORE" /&gt;
    253       &lt;Method name="run" /&gt;
    254     &lt;/Match&gt;
    255     &lt;Match&gt;
    256       &lt;Bug pattern="UUF_UNUSED_FIELD" /&gt;
    257       &lt;Field name="~ajc\$.*" /&gt;
    258     &lt;/Match&gt;
    259 
    260 </pre><p>
    261 </p><p>
    262     9. Match bugs in specific parts of the code base
    263 
    264 </p><pre class="programlisting">
    265 
    266     &lt;!-- match unused fields warnings in Messages classes in all packages --&gt;
    267     &lt;Match&gt;
    268       &lt;Class name="~.*\.Messages" /&gt;
    269       &lt;Bug code="UUF" /&gt;
    270     &lt;/Match&gt;
    271     &lt;!-- match mutable statics warnings in all internal packages --&gt;
    272     &lt;Match&gt;
    273       &lt;Package name="~.*\.internal" /&gt;
    274       &lt;Bug code="MS" /&gt;
    275     &lt;/Match&gt;
    276     &lt;!-- match anonymoous inner classes warnings in ui package hierarchy --&gt;
    277     &lt;Match&gt;
    278       &lt;Package name="~com\.foobar\.fooproject\.ui.*" /&gt;
    279       &lt;Bug pattern="SIC_INNER_SHOULD_BE_STATIC_ANON" /&gt;
    280     &lt;/Match&gt;
    281 
    282 </pre><p>
    283 </p><p>
    284     10. Match bugs on fields or methods with specific signatures
    285 </p><pre class="programlisting">
    286 
    287     &lt;!-- match System.exit(...) usage warnings in void main(String[]) methods in all classes --&gt;
    288     &lt;Match&gt;
    289       &lt;Method returns="void" name="main" params="java.lang.String[]" /&gt;
    290       &lt;Bug pattern="DM_EXIT" /&gt;
    291     &lt;/Match&gt;
    292     &lt;!-- match UuF warnings on fields of type com.foobar.DebugInfo on all classes --&gt;
    293     &lt;Match&gt;
    294       &lt;Field type="com.foobar.DebugInfo" /&gt;
    295       &lt;Bug code="UuF" /&gt;
    296     &lt;/Match&gt;
    297 
    298 </pre><p>
    299 </p><p>
    300     11. Match bugs using the Not filter operator
    301 </p><pre class="programlisting">
    302 
    303 &lt;!-- ignore all bugs in test classes, except for those bugs specifically relating to JUnit tests --&gt;
    304 &lt;!-- i.e. filter bug if ( classIsJUnitTest &amp;&amp; ! bugIsRelatedToJUnit ) --&gt;
    305 &lt;Match&gt;
    306   &lt;!-- the Match filter is equivalent to a logical 'And' --&gt;
    307 
    308   &lt;Class name="~.*\.*Test" /&gt;
    309   &lt;!-- test classes are suffixed by 'Test' --&gt;
    310 
    311   &lt;Not&gt;
    312       &lt;Bug code="IJU" /&gt; &lt;!-- 'IJU' is the code for bugs related to JUnit test code --&gt;
    313   &lt;/Not&gt;
    314 &lt;/Match&gt;
    315 
    316 </pre><p>
    317 </p></div><div class="sect1" title="6.&nbsp;Complete Example"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="d0e2249"></a>6.&nbsp;Complete Example</h2></div></div></div><pre class="programlisting">
    318 
    319 &lt;FindBugsFilter&gt;
    320      &lt;Match&gt;
    321        &lt;Class name="com.foobar.ClassNotToBeAnalyzed" /&gt;
    322      &lt;/Match&gt;
    323 
    324      &lt;Match&gt;
    325        &lt;Class name="com.foobar.ClassWithSomeBugsMatched" /&gt;
    326        &lt;Bug code="DE,UrF,SIC" /&gt;
    327      &lt;/Match&gt;
    328 
    329      &lt;!-- Match all XYZ violations. --&gt;
    330      &lt;Match&gt;
    331        &lt;Bug code="XYZ" /&gt;
    332      &lt;/Match&gt;
    333 
    334      &lt;!-- Match all doublecheck violations in these methods of "AnotherClass". --&gt;
    335      &lt;Match&gt;
    336        &lt;Class name="com.foobar.AnotherClass" /&gt;
    337        &lt;Or&gt;
    338          &lt;Method name="nonOverloadedMethod" /&gt;
    339          &lt;Method name="frob" params="int,java.lang.String" returns="void" /&gt;
    340          &lt;Method name="blat" params="" returns="boolean" /&gt;
    341        &lt;/Or&gt;
    342        &lt;Bug code="DC" /&gt;
    343      &lt;/Match&gt;
    344 
    345      &lt;!-- A method with a dead local store false positive (medium priority). --&gt;
    346      &lt;Match&gt;
    347        &lt;Class name="com.foobar.MyClass" /&gt;
    348        &lt;Method name="someMethod" /&gt;
    349        &lt;Bug pattern="DLS_DEAD_LOCAL_STORE" /&gt;
    350        &lt;Priority value="2" /&gt;
    351      &lt;/Match&gt;
    352 
    353      &lt;!-- All bugs in test classes, except for JUnit-specific bugs --&gt;
    354      &lt;Match&gt;
    355       &lt;Class name="~.*\.*Test" /&gt;
    356       &lt;Not&gt;
    357           &lt;Bug code="IJU" /&gt;
    358       &lt;/Not&gt;
    359      &lt;/Match&gt;
    360 
    361 &lt;/FindBugsFilter&gt;
    362 
    363 </pre></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="eclipse.html">Prev</a>&nbsp;</td><td width="20%" align="center">&nbsp;</td><td width="40%" align="right">&nbsp;<a accesskey="n" href="analysisprops.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter&nbsp;7.&nbsp;Using the <span class="application">FindBugs</span>&#8482; Eclipse plugin&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top">&nbsp;Chapter&nbsp;9.&nbsp;Analysis Properties</td></tr></table></div></body></html>