File: codenarc-configuring-rules.html

package info (click to toggle)
codenarc 0.23-5
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster
  • size: 11,312 kB
  • sloc: java: 2,530; xml: 1,088; makefile: 21
file content (414 lines) | stat: -rw-r--r-- 30,883 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!-- Generated by Apache Maven Doxia Site Renderer 1.4 at 18 Feb 2015 -->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>CodeNarc - CodeNarc - Configuring Rules</title>
    <style type="text/css" media="all">
      @import url("./css/maven-base.css");
      @import url("./css/maven-theme.css");
      @import url("./css/site.css");
    </style>
    <link rel="stylesheet" href="./css/print.css" type="text/css" media="print" />
    <meta name="Date-Revision-yyyymmdd" content="20150218" />
    <meta http-equiv="Content-Language" content="en" />
                  
        </head>
  <body class="composite">
    <div id="banner">
                                              <a href="./" id="bannerLeft">
                                                <img src="images/codenarc-logo.png" alt="CodeNarc" />
                </a>
                              <a href="http://github.com/CodeNarc" id="bannerRight">
                                                <img src="images/forkme_right_red_aa0000.png" alt="Fork me on GitHub" />
                </a>
            <div class="clear">
        <hr/>
      </div>
    </div>
    <div id="breadcrumbs">
            
                    
                <div class="xleft">
        <span id="publishDate">Last Published: 18 Feb 2015</span>
                  &nbsp;| <span id="projectVersion">Version: 0.23</span>
                      </div>
            <div class="xright">        
                    
      </div>
      <div class="clear">
        <hr/>
      </div>
    </div>
    <div id="leftColumn">
      <div id="navcolumn">
             
                    
                                <h5>General</h5>
                  <ul>
                  <li class="none">
                          <a href="index.html" title="Home">Home</a>
            </li>
                  <li class="none">
                          <a href="https://sourceforge.net/project/showfiles.php?group_id=250145" class="externalLink" title="Downloads">Downloads</a>
            </li>
                  <li class="none">
                          <a href="apidocs/index.html" title="Javadocs">Javadocs</a>
            </li>
                  <li class="none">
                          <a href="http://sourceforge.net/mail/?group_id=250145" class="externalLink" title="Mailing Lists">Mailing Lists</a>
            </li>
                  <li class="none">
                          <a href="http://sourceforge.net/tracker/?group_id=250145" class="externalLink" title="Bug Tracker">Bug Tracker</a>
            </li>
                  <li class="none">
                          <a href="http://sourceforge.net/projects/codenarc" class="externalLink" title="SourceForge Project">SourceForge Project</a>
            </li>
                  <li class="none">
                          <a href="http://github.com/CodeNarc" class="externalLink" title="GitHub Project">GitHub Project</a>
            </li>
          </ul>
                       <h5>Running</h5>
                  <ul>
                  <li class="none">
                          <a href="codenarc-ant-task.html" title="Ant Task Usage">Ant Task Usage</a>
            </li>
                  <li class="none">
                          <a href="codenarc-command-line.html" title="Command-Line">Command-Line</a>
            </li>
                  <li class="none">
                          <a href="codenarc-run-as-a-test.html" title="Run as a Test">Run as a Test</a>
            </li>
                  <li class="none">
                          <a href="codenarc-other-tools-frameworks.html" title="Other Tools/Frameworks">Other Tools/Frameworks</a>
            </li>
          </ul>
                       <h5>Using</h5>
                  <ul>
                  <li class="none">
                          <a href="codenarc-creating-ruleset.html" title="Creating a RuleSet">Creating a RuleSet</a>
            </li>
                  <li class="none">
                          <a href="codenarc-creating-rule.html" title="Creating a Rule">Creating a Rule</a>
            </li>
                  <li class="none">
            <strong>Configuring Rules</strong>
          </li>
                  <li class="none">
                          <a href="StarterRuleSet-AllRulesByCategory.groovy.txt" title="Starter RuleSet (All)">Starter RuleSet (All)</a>
            </li>
          </ul>
                       <h5>Report Types</h5>
                  <ul>
                  <li class="none">
                          <a href="codenarc-HtmlReportWriter.html" title="HTML Report">HTML Report</a>
            </li>
                  <li class="none">
                          <a href="codenarc-XmlReportWriter.html" title="XML Report">XML Report</a>
            </li>
                  <li class="none">
                          <a href="codenarc-TextReportWriter.html" title="Text and IDE Reports">Text and IDE Reports</a>
            </li>
          </ul>
                       <h5>Sample Reports</h5>
                  <ul>
                  <li class="none">
                          <a href="SampleCodeNarcHtmlReport.html" title="Sample HTML Report">Sample HTML Report</a>
            </li>
                  <li class="none">
                          <a href="SampleCodeNarcXmlReport.xml" title="Sample XML Report">Sample XML Report</a>
            </li>
          </ul>
                       <h5>Rules</h5>
                  <ul>
                  <li class="none">
                          <a href="codenarc-rule-index.html" title="Rule Index">Rule Index</a>
            </li>
                  <li class="none">
                          <a href="codenarc-rules-basic.html" title="Basic Rules">Basic Rules</a>
            </li>
                  <li class="none">
                          <a href="codenarc-rules-braces.html" title="Braces Rules">Braces Rules</a>
            </li>
                  <li class="none">
                          <a href="codenarc-rules-concurrency.html" title="Concurrency Rules">Concurrency Rules</a>
            </li>
                  <li class="none">
                          <a href="codenarc-rules-convention.html" title="Convention Rules">Convention Rules</a>
            </li>
                  <li class="none">
                          <a href="codenarc-rules-design.html" title="Design Rules">Design Rules</a>
            </li>
                  <li class="none">
                          <a href="codenarc-rules-dry.html" title="DRY Rules">DRY Rules</a>
            </li>
                  <li class="none">
                          <a href="codenarc-rules-enhanced.html" title="Enhanced Rules">Enhanced Rules</a>
            </li>
                  <li class="none">
                          <a href="codenarc-rules-exceptions.html" title="Exceptions Rules">Exceptions Rules</a>
            </li>
                  <li class="none">
                          <a href="codenarc-rules-formatting.html" title="Formatting Rules">Formatting Rules</a>
            </li>
                  <li class="none">
                          <a href="codenarc-rules-generic.html" title="Generic Rules">Generic Rules</a>
            </li>
                  <li class="none">
                          <a href="codenarc-rules-grails.html" title="Grails Rules">Grails Rules</a>
            </li>
                  <li class="none">
                          <a href="codenarc-rules-groovyism.html" title="Groovyism Rules">Groovyism Rules</a>
            </li>
                  <li class="none">
                          <a href="codenarc-rules-imports.html" title="Imports Rules">Imports Rules</a>
            </li>
                  <li class="none">
                          <a href="codenarc-rules-jdbc.html" title="JDBC Rules">JDBC Rules</a>
            </li>
                  <li class="none">
                          <a href="codenarc-rules-junit.html" title="JUnit Rules">JUnit Rules</a>
            </li>
                  <li class="none">
                          <a href="codenarc-rules-logging.html" title="Logging Rules">Logging Rules</a>
            </li>
                  <li class="none">
                          <a href="codenarc-rules-naming.html" title="Naming Rules">Naming Rules</a>
            </li>
                  <li class="none">
                          <a href="codenarc-rules-size.html" title="Size/Complexity Rules">Size/Complexity Rules</a>
            </li>
                  <li class="none">
                          <a href="codenarc-rules-security.html" title="Security Rules">Security Rules</a>
            </li>
                  <li class="none">
                          <a href="codenarc-rules-serialization.html" title="Serialization Rules">Serialization Rules</a>
            </li>
                  <li class="none">
                          <a href="codenarc-rules-unnecessary.html" title="Unnecessary Rules">Unnecessary Rules</a>
            </li>
                  <li class="none">
                          <a href="codenarc-rules-unused.html" title="Unused Rules">Unused Rules</a>
            </li>
          </ul>
                       <h5>Developing</h5>
                  <ul>
                  <li class="none">
                          <a href="codenarc-developer-guide.html" title="Developer Guide">Developer Guide</a>
            </li>
          </ul>
                       <h5>Project Documentation</h5>
                  <ul>
                                                                    <li class="collapsed">
                          <a href="project-info.html" title="Project Information">Project Information</a>
                  </li>
                                                                                                                          <li class="collapsed">
                          <a href="project-reports.html" title="Project Reports">Project Reports</a>
                  </li>
          </ul>
                                                                                                                               <a href="http://sourceforge.net" title="Hosted on SourceForge.net" class="poweredBy">
        <img class="poweredBy"  alt="Hosted on SourceForge.net" src="http://sflogo.sourceforge.net/sflogo.php?group_id=208647&type=2"     />
      </a>
                                                                                                                      <a href="http://maven.apache.org" title="Build with Maven 2" class="poweredBy">
        <img class="poweredBy"  alt="Build with Maven 2" src="images/logos/maven-feather.png"     />
      </a>
                       
                    
            </div>
    </div>
    <div id="bodyColumn">
      <div id="contentBox">
        <div class="section">
<h2>CodeNarc - Configuring Rules<a name="CodeNarc_-_Configuring_Rules"></a></h2><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><b>Contents</b></p>
<ul>
<li><a href="#Configuring_Rules_Within_a_RuleSet_File">Configuring Rules Within a RuleSet File</a></li>
<li><a href="#Configuring_Rules_Using_a_Properties_File">Configuring Rules Using a Properties File</a></li>
<li><a href="#Standard_Properties_for_Configuring_Rules">Standard Properties for Configuring Rules</a></li>
<li><a href="#Turning_Off_A_Rule">Turning Off A Rule</a></li>
<li><a href="#Suppressing_A_Rule_From_Within_Source_Code">Suppressing A Rule From Within Source Code</a></li>
<li><a href="#Customizing_Rule_Descriptions_Shown_in_the_HTML_Report">Customizing Rule Descriptions Shown in the HTML Report</a></li></ul>
<p>You can configure rules within the <i>RuleSet</i> file or within the &quot;codenarc.properties&quot; file. Both of these approaches are described in the sections below.</p>
<p><b>NOTE</b>: Keep in mind that <i>RuleSet</i> files can be nested (to any depth). That, along with the &quot;codenarc.properties&quot; file support, allows multiple layers of rule configuration. The hierarchical layering of configuration can come in handy within organizations comprised of multiple teams or projects. For instance, you can define an organization <i>RuleSet</i> file. Then each team can define its own <i>RuleSet</i> file and/or a &quot;codenarc.properties&quot; file, customizing the rules from the top-level file.</p>
<div class="section">
<h3><a name="Configuring_Rules_Within_a_RuleSet_File">Configuring Rules Within a RuleSet File</a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><a href="./codenarc-custom-ruleset.html">Creating a RuleSet</a> describes how to create a new <i>RuleSet</i> and configure the rules within it.</p>
<p>Here is an example of a Groovy <i>RuleSet</i> file that configures all three rules that it contains:</p>
<div class="source">
<pre>ruleset {
    CyclomaticComplexity {
        maxMethodComplexity = 1
    }

    ConfusingTernary(priority:3)

    StatelessClass {
        name = 'StatelessDao'
        applyToClassNames = '*Dao'
    }
}
</pre></div>
<p>Here is another example of a Groovy <i>RuleSet</i> file. This one includes another <i>RuleSet</i>, and configures a couple of the rules that it contains:</p>
<div class="source">
<pre>ruleset {
    ruleset('rulesets/basic.xml') {
        CatchThrowable {
            priority = 1
        }
        EqualsAndHashCode priority:3
    }
}
</pre></div>
<p>If you have an XML <i>RuleSet</i>, then you can configure rules when you include a whole <i>RuleSet</i> using <tt>&lt;ruleset-ref&gt;</tt>, as in the following example:</p>
<div class="source">
<pre>    &lt;ruleset-ref path='rulesets/size.xml'&gt;
        &lt;rule-config name='ClassSize'&gt;
            &lt;property name='maxLines' value='500'/&gt;
        &lt;/rule-config&gt;
    &lt;/ruleset-ref&gt;
</pre></div>
<p>Or you can configure <i>Rules</i> that you include individually using <tt>&lt;rule&gt;</tt>, as in the following example:</p>
<div class="source">
<pre>    &lt;rule class='org.codenarc.rule.naming.VariableNameRule'&gt;
        &lt;property name=&quot;finalRegex&quot; value=&quot;F_[A-Z0-9]*&quot;/&gt;
        &lt;property name='priority' value='1'/&gt;
    &lt;/rule&gt;
</pre></div></div>
<div class="section">
<h3><a name="Configuring_Rules_Using_a_Properties_File">Configuring Rules Using a Properties File</a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p><b>CodeNarc</b> reads the properties file named &quot;codenarc.properties&quot;, if found on the classpath, and applies the property values to any matching <i>Rules</i>. You can optionally override the location of the properties file by setting the &quot;codenarc.properties.file&quot; system property to the path or URL of the properties file. That can include a &quot;file:&quot; URL to load from a relative or absolute path on the filesystem (e.g., &quot;file:relative/path/override-codenarc.properties&quot;). If the properties file is not found, then do nothing.</p>
<p>For each properties entry of the form <i>[rule-name].[property-name]=[property-value]</i>, the named property for the rule matching <i>rule-name</i> is set to the specified <i>property-value</i>. Properties entries not of this form or specifying rule names not within the current <i>RuleSet</i> are ignored.</p>
<p>The following example &quot;codenarc.properties&quot; file configures several rules. Note that Fields with <tt>Integer</tt>, <tt>String</tt> and <tt>Boolean</tt> values are configured.</p>
<div class="source">
<pre># Sample RuleSet configuration
# Entries should be of the form:    [rule-name].[property-name]=[property-value]

CatchThrowable.doNotApplyToClassNames=BaseController,AbstractTest
CatchThrowable.priority = 1

ReturnFromFinallyBlock.priority = 1

# Turn off this rule
AbstractClassName.enabled=false

# Unset special naming for final fields -- use base 'regex' value for all names
FieldName.finalRegex=
# Make sure all field names are prefixed with '_'
FieldName.regex=_[a-z][a-zA-Z0-9]*
</pre></div>
<p>Note that you cannot add new rules using the &quot;codenarc.properties&quot; file, though you can disable (turn off) rules.</p></div>
<div class="section">
<h3><a name="Standard_Properties_for_Configuring_Rules">Standard Properties for Configuring Rules</a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>The rules included with <b>CodeNarc</b> (as well as any subclasses of <tt>AbstractRule</tt>) provide several standard properties that can be used to configure how rules are applied to source files.</p>
<div class="section">
<h4>Applying Rules Based on Class Name and/or Package Name<a name="Applying_Rules_Based_on_Class_Name_andor_Package_Name"></a></h4><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>The <tt>applyToClassNames</tt> and <tt>doNotApplyToClassNames</tt> properties enable filtering the classes to which the rule is applied.</p>
<p>If <tt>applyToClassNames</tt> is not empty or null, then the rule is only applied to classes matching one of the specified class names. Likewise, if <tt>doNotApplyToClassNames</tt> is not empty or null, then the rule is NOT applied to classes matching any of the specified class names.</p>
<p>Both of these properties can specify either a single class name or a comma-separated list of names. The names may optionally contain wildcard characters ('*' or '?'). The wildcard character '*' matches a sequence of zero or more characters in the input string. The wildcard character '?' matches exactly one character in the input string.</p>
<p>Each class name(s) can be either a fully-qualified class name (including the package) or else a class name without a package.</p>
<p>For instance, in the following example &quot;codenarc.properties&quot; excerpt, the <i>CatchThrowable</i> rule is NOT applied to classes named &quot;BaseController&quot; or &quot;AbstractTest&quot;, no matter what package they are in (if any). The <i>FieldName</i> rule, on the other hand, is only applied to the <tt>org.codenarc.CodeNarc</tt> class or any of the classes within <tt>org.codenarc.extra</tt> package.</p>
<div class="source">
<pre>CatchThrowable.doNotApplyToClassNames=BaseController,AbstractTest

FieldName.applyToClassNames=org.codenarc.CodeNarc,org.codenarc.extra.*
</pre></div>
<p>Both properties default to <tt>null</tt>.</p>
<p>These properties are available only to subclasses of <tt>AbstractAstVisitorRule</tt>, but this includes almost all of the rules provided with <b>CodeNarc</b> -- i.e., all of the rules that deal with the Groovy AST (Abstract Syntax Tree) of a source file. This includes any rule that processes source code elements such as a package, class, method, field, parameter, etc..</p></div>
<div class="section">
<h4>Applying Rules Based on File Path or Name<a name="Applying_Rules_Based_on_File_Path_or_Name"></a></h4><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<div class="section">
<h5>Matching Either Filename or Pathname, With Optional Wildcards<a name="Matching_Either_Filename_or_Pathname_With_Optional_Wildcards"></a></h5><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>The <tt>applyToFileNames</tt> and <tt>doNotApplyToFileNames</tt> properties enable filtering the files to which the rule is applied by specifying a comma-separated list of filenames (with optional path) of source files. If <tt>applyToFileNames</tt> is not empty or null, then the rule is only applied to source files matching one of the specified filenames. Likewise, if <tt>doNotApplyToFilesMatching</tt> is not empty or null, then the rule is NOT applied to source files matching any of the specified filenames.</p>
<p>Both properties may optionally specify a path (i.e., if the value contains at least one '/' path separator). In that case, the value is matched against the full pathname of each source file. If no path is specified, then only the filename of each source file is compared (i.e., its path is ignored).</p>
<p>Both properties may optionally contain wildcard characters ('*' or '?'). The wildcard character '*' matches a sequence of zero or more characters in the input string. The wildcard character '?' matches exactly one character in the input string. Both properties default to <tt>null</tt>.</p></div>
<div class="section">
<h5>Matching File Pathnames Against a Regular Expression<a name="Matching_File_Pathnames_Against_a_Regular_Expression"></a></h5><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>The <tt>applyToFilesMatching</tt> and <tt>doNotApplyToFilesMatching</tt> properties enable filtering the files to which the rule is applied by matching a regular expression against the <b>full pathname</b> of each source file. If <tt>applyToFilesMatching</tt> is not empty or null, then the rule is only applied to source files with matching paths. Likewise, if <tt>doNotApplyToFilesMatching</tt> is not empty or null, then the rule is NOT applied to source files with matching paths. Both properties default to <tt>null</tt>.</p>
<ul>
<li><b>NOTE:</b> File separator characters within pathnames are normalized to forward slashes (/). On Windows, for instance, that means that the path &quot;c:\dir\file.txt&quot; is normalized to &quot;c:/dir/file.txt&quot;. If your regular expressions contain file separator characters, be sure that they are all the standard '/', no matter what operating system.</li></ul></div></div>
<div class="section">
<h4>Other Standard Rule Properties<a name="Other_Standard_Rule_Properties"></a></h4><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>The <tt>enabled</tt> boolean property allows a rule to be completely turned off (by setting it to <tt>false</tt>). It defaults to <tt>true</tt>.</p>
<p>The <tt>violationMessage</tt> property enables overriding the rule violation message. If not null, this is used as the message for all violations of this rule, overriding any message generated by the rule itself. This property defaults to <tt>null</tt>. Note that setting this to an empty string &quot;hides&quot; the message, if any, generated by the actual rule.</p>
<p>The <tt>description</tt> property enables overriding the rule description text. If not null, this value is used as the description text for this rule, overriding any description text found in the i18n resource bundles. This property defaults to <tt>null</tt>. Also see <a href="#Include_Substitutable_Message_Parameters_in_a_Rule_Description">Include Substitutable Message Parameters in a Rule Description</a></p>
<p>You can also override the default <tt>name</tt> or <tt>priority</tt> properties for each rule.</p></div></div>
<div class="section">
<h3><a name="Turning_Off_A_Rule">Turning Off A Rule</a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>You can turn off a rule by filtering the containing ruleset to exclude the rule. See <a href="./codenarc-creating-ruleset.html#Filtering_Rules_Within_a_RuleSet">Filtering Rules Within a RuleSet</a>.</p>
<p>Alternately, as mentioned above, you can turn off (disable) a rule by setting its <tt>enabled</tt> boolean property to <tt>false</tt> (assuming it is a subclass of <tt>AbstractRule</tt>), as shown in the following &quot;codenarc.properties&quot; excerpt.</p>
<div class="source">
<pre># Turn off this rule
AbstractClassName.enabled=false
</pre></div>
<p>And here is an example of disabling a rule within a Groovy <i>RuleSet</i> by setting its <tt>enabled</tt> attribute to <tt>false</tt>.</p>
<div class="source">
<pre>ruleset {
    ruleset('rulesets/basic.xml') {
        CatchThrowable(enabled:false)
    }
}
</pre></div></div>
<div class="section">
<h3><a name="Suppressing_A_Rule_From_Within_Source_Code">Suppressing A Rule From Within Source Code</a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>You can use the <tt>@SuppressWarnings</tt> annotation on a <i>class</i>, <i>method</i>, <i>constructor</i>, <i>field</i> or <i>property</i> to suppress one or more <b>CodeNarc</b> rules within its source code.</p>
<p>Specify one or more rule name as the parameter of the <tt>@SuppressWarnings</tt> annotation. Note that this is the rule <i>name</i>, not the <i>class</i> name (i.e., without the &quot;Rule&quot; suffix). Specify multiple rule names by passing a <i>List</i> of rule names as a single parameter, e.g. <tt>@SuppressWarnings(['IfStatementBraces', 'ThrowException'])</tt></p>
<p>NOTE: The <tt>@SuppressWarnings</tt> annotation only works for rule classes that subclass <tt>AbstractAstVisitorRule</tt>.</p>
<p>For example, the following code suppresses (prevents) violations of the <b>DuplicateStringLiteral</b> rule within the <tt>MyClass</tt> class, as well as suppressing the <i>IfStatementBraces</i> and <i>ThrowException</i> rules within its <tt>getCount()</tt> method.</p>
<div class="source">
<pre>    @SuppressWarnings('DuplicateStringLiteral')
    class MyClass {
        def y = 'x'
        def z = 'x'

        @SuppressWarnings(['IfStatementBraces', 'ThrowException'])
        int getCount() {
            if (!ready) throw new Exception('Not ready')
        }
    }
</pre></div></div>
<div class="section">
<h3><a name="Customizing_Rule_Descriptions_Shown_in_the_HTML_Report">Customizing Rule Descriptions Shown in the HTML Report</a></h3><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>The descriptions for rules used in a <b>CodeNarc</b> report are shown at the bottom of the resulting HTML report. Those rule descriptions are retrieved from a <i>ResourceBundle</i>. This <i>ResourceBundle</i> consists of a base (parent) bundle, provided with the <b>CodeNarc</b> distribution, and an optional custom (child) bundle, which may be provided by the user.</p>
<p>Because this is based on the standard Java <i>ResourceBundle</i> facility, you can customize (override) or translate these descriptions by providing your own locale-specific message properties file on the classpath. For example, a German one would be named &quot;codenarc-messages_de.properties&quot;.</p>
<p>The rule description message key is of the form &quot;<b>&lt;rule-name&gt;.description.html</b>&quot;. Examples include &quot;BooleanInstantiation.description.html&quot; and &quot;CatchThrowable.description.html&quot;.</p>
<p>(Note that the rule description included in the XML report is specified by the key <b>&lt;rule-name&gt;.description</b>).</p>
<p>See the javadoc for the <tt>java.util.ResourceBundle</tt> class for more information.</p>
<div class="section">
<h4><a name="Include_Substitutable_Message_Parameters_in_a_Rule_Description">Include Substitutable Message Parameters in a Rule Description</a></h4><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>You can optionally include message parameters within a rule description, whether that description is specified in a ResourceBundle properties file or through the <tt>description</tt> property of the rule. Use the standard Groovy template parameter syntax: <tt>${..}</tt>. You can reference rule property values through the &quot;rule&quot; object within the template parameter closure. For example, this description includes the rule <tt>priority</tt> within the rule description: <tt>&quot;Description for rule with priority ${rule.priority}&quot;</tt>.</p></div>
<div class="section">
<h4>The Base Messages Resource Bundle<a name="The_Base_Messages_Resource_Bundle"></a></h4><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>The base <i>ResourceBundle</i> provided with the <b>CodeNarc</b> distribution has a basename of &quot;codenarc-base-messages&quot;. The rules provided with <b>CodeNarc</b> all have associated descriptions within the &quot;codenarc-base-messages.properties&quot; file.</p>
<p>You can provide a locale-specific variant of this file on the classpath if you wish, but in most scenarios, the recommended approach for customizing rule descriptions is to provide a custom messages bundle file, as described below.</p></div>
<div class="section">
<h4>The Custom Messages Resource Bundle<a name="The_Custom_Messages_Resource_Bundle"></a></h4><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>You can optionally provide descriptions for new rules or override existing rule descriptions by placing a &quot;codenarc-messages.properties&quot; file on the classpath. This is also a <i>ResourceBundle</i> file, so it can have locale-specific variants, if needed. If this bundle (file) is present, it is searched first. If the desired message key is not found, only then is the base message bundle checked.</p>
<p>You can provide rule description entries within this file for custom rules that you create. You can also include rule description entries for existing rules, in which case the descriptions in this file override the default descriptions.</p></div>
<div class="section">
<h4>Customizing the Name and Description for an Existing Rule<a name="Customizing_the_Name_and_Description_for_an_Existing_Rule"></a></h4><!-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<p>You may wish to customize both the <i>name</i> and the description for an existing rule. For example, you may want to have multiple instances of a generic rule, each configured differently. Or you may just want to make the rule name more clearly denote its specific purpose.</p>
<p>For example, in the following excerpt from a <i>RuleSet</i> file, because the <i>name</i> property is customized for the <b>IllegalRegexRule</b>, the new rule name &quot;AuthorTagNotAllowed&quot; will show up at the bottom of the HTML report, instead of the default rule name &quot;IllegalRegex&quot;.</p>
<div class="source">
<pre>    &lt;rule class='org.codenarc.rule.generic.IllegalRegexRule'&gt;
        &lt;property name=&quot;name&quot; value=&quot;AuthorTagNotAllowed&quot;/&gt;
        &lt;property name='regex' value='\@author'/&gt;
    &lt;/rule&gt;
</pre></div>
<p>The rule description must be added to the custom messages resource bundle file (&quot;codenarc-messages.properties&quot;). The message key will be &quot;AuthorTagNotAllowed.description.html&quot; to set the description used in the HTML report, and &quot;AuthorTagNotAllowed.description&quot; for the description used in the XML report.</p></div></div></div>
      </div>
    </div>
    <div class="clear">
      <hr/>
    </div>
    <div id="footer">
      <div class="xright">
              Copyright &#169;                   2015.
          All Rights Reserved.      
                    
                  </div>
      <div class="clear">
        <hr/>
      </div>
    </div>
  </body>
</html>